<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载校内项目详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 基本信息 -->
      <CommonSection title="基本信息" :isFirst="true">
        <CommonInfoRow label="项目名称" :value="form.projectName" />
        <CommonInfoRow label="项目分类" :value="form.projectClazzName" />
        <CommonInfoRow label="项目来源" :value="form.projectSource" />
        <CommonInfoRow label="项目日期" :value="formatDate(form.startDate) + ' 至 ' + formatDate(form.endDate)" />
        <CommonInfoRow label="所属科室" :value="form.deptName" />
        <CommonInfoRow label="统计年度" :value="form.statisticalYear" />
        <CommonInfoRow label="项目负责人" :value="form.projectLeaderName" />
        <CommonInfoRow label="负责人类型" :value="getDictLabel('sci_leader_type', form.projectLeaderType)" />
        <CommonInfoRow label="负责人电话" :value="form.projectLeaderPhone" />
        <CommonInfoRow label="负责人邮箱" :value="form.projectLeaderMail" />
        <CommonInfoRow label="所属平台">
          <view class="platform-tags">
            <template v-if="platformList.length > 0">
              <uv-tags v-for="(p, index) in platformList" :key="index" :text="p.platformName === '其他' ? '其他' : (p.platformType ? p.platformName + ' (' + p.platformType + ')' : p.platformName)" type="primary" plain size="mini" class="mr5"></uv-tags>
            </template>
            <text v-else>-</text>
          </view>
        </CommonInfoRow>
        <CommonInfoRow label="研究类别" :value="form.researchCategory" isColumn />
        <CommonInfoRow label="预期成果" :value="form.expectedResults" isColumn />
        <CommonInfoRow label="备注" :value="form.remark" isColumn />
      </CommonSection>

      <!-- 成员信息 -->
      <CommonSection title="成员信息" v-if="form.memberList?.length">
        <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 blue" 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>
      </CommonSection>

      <!-- 附件信息 -->
      <AttachmentList :list="form.fileList" title="附件资料" />
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed } from 'vue';
import { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
import { formatDate } from '@/utils/date';
import to from 'await-to-js';
import CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.vue';
import AttachmentList from './AttachmentList.vue';

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 platformList = computed(() => {
  if (form.value?.belongPlatform) {
    try {
      const data = JSON.parse(form.value.belongPlatform);
      return Array.isArray(data) ? data : [];
    } catch (e) {
      if (form.value.belongPlatform === '其他') return [{ platformName: '其他' }];
      return [{ platformName: form.value.belongPlatform }];
    }
  }
  return [];
});

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>
