<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载校内项目详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 基本信息 -->
      <view class="common-section-card">
        <view class="section-title">基本信息</view>
        <view class="info-row"><text class="label">项目名称</text><text class="value">{{ form.projectName || '-' }}</text></view>
        <view class="info-row"><text class="label">项目分类</text><text class="value">{{ form.projectClazzName || '-' }}</text></view>
        <view class="info-row"><text class="label">项目来源</text><text class="value">{{ form.projectSource || '-' }}</text></view>
        <view class="info-row"><text class="label">项目日期</text><text class="value">{{ form.startDate || '-' }} 至 {{ form.endDate || '-' }}</text></view>
        <view class="info-row"><text class="label">所属科室</text><text class="value">{{ form.deptName || '-' }}</text></view>
        <view class="info-row"><text class="label">统计年度</text><text class="value">{{ form.statisticalYear || '-' }}</text></view>
        <view class="info-row"><text class="label">项目负责人</text><text class="value">{{ form.projectLeaderName || '-' }}</text></view>
        <view class="info-row">
          <text class="label">负责人类型</text>
          <text class="value">{{ getDictLabel('sci_leader_type', form.projectLeaderType) }}</text>
        </view>
        <view class="info-row"><text class="label">负责人电话</text><text class="value">{{ form.projectLeaderPhone || '-' }}</text></view>
        <view class="info-row"><text class="label">负责人邮箱</text><text class="value">{{ form.projectLeaderMail || '-' }}</text></view>
        <view class="info-row column">
          <text class="label">研究类别</text>
          <text class="value remark">{{ form.researchCategory || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">预期成果</text>
          <text class="value remark">{{ form.expectedResults || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">备注</text>
          <text class="value remark">{{ form.remark || '-' }}</text>
        </view>
      </view>

      <!-- 成员信息 -->
      <view class="common-section-card mt20" v-if="form.memberList?.length">
        <view class="section-title">成员信息</view>
        <view class="member-list">
          <view class="member-item" v-for="(row, index) in form.memberList" :key="index">
            <view class="member-header">
              <view class="name-box">
                <text class="m-name">{{ row.memberName }}</text>
                <text class="m-tag" :class="{ leader: row.projectRole === '10' }">
                  {{ row.projectRole === '10' ? '负责人' : row.projectRole === '20' ? '主要参与人' : '一般参与人' }}
                </text>
              </view>
              <text class="m-type-tag" v-if="row.memberType">{{ getMemberTypeLabel(row.memberType) }}</text>
            </view>
            <view class="m-body">
              <view class="m-line" v-if="row.deptName"><text class="l">单位：</text><text class="v">{{ row.deptName }}</text></view>
              <view class="m-line" v-if="row.degree"><text class="l">学位：</text><text class="v">{{ getDictLabel('sci_academic_degree', row.degree) }}</text></view>
              <view class="m-line" v-if="row.technicalTitle"><text class="l">职称：</text><text class="v">{{ row.technicalTitle }}</text></view>
              <view class="m-line" v-if="row.documentNum">
                <text class="l">{{ row.documentType === '10' ? '身份证' : '证件' }}：</text>
                <text class="v">{{ row.documentNum }}</text>
              </view>
              <view class="m-line column" v-if="row.responsibleContent">
                <text class="l">负责内容：</text>
                <text class="v remark">{{ row.responsibleContent }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 附件信息 -->
      <view class="common-section-card mt20" v-if="form.fileList?.length">
        <view class="section-title">附件资料</view>
        <view class="common-file-list">
          <view class="file-item" v-for="(file, index) in form.fileList" :key="index" @click="previewFile(file.fileUrl, file.fileName)">
            <view class="file-info">
              <text class="f-name">{{ file.fileName }}</text>
              <view class="f-meta">
                <text class="f-type">{{ file.fileType }}</text>
                <text class="f-required" v-if="file.required">*</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
import { previewFile } from '@/utils/file';
import to from 'await-to-js';

const props = defineProps<{
  code: string;
}>();

const { getDictLabel } = useDict('sci_leader_type', 'sci_academic_degree');
const documentApi = useDocumentApi();

const form = ref<any>(null);
const loading = ref(false);

const getMemberTypeLabel = (type: string) => {
  const map: Record<string, string> = {
    '10': '本院人员',
    '20': '非本院人员',
    '30': '研究生'
  };
  return map[type] || type;
};

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getSpontaneityByCode(props.code));
  if (!err && res?.data) {
    form.value = res.data;
  }
  loading.value = false;
};

onMounted(() => {
  fetchData();
});

watch(() => props.code, () => {
  fetchData();
});
</script>

<style lang="scss" scoped>
@import "./common.scss";
</style>
