<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.evalName" />
        <CommonInfoRow label="评价号" :value="form.evalCode" />
        <CommonInfoRow label="评价部门" :value="form.evalDeptName" />
        <CommonInfoRow label="评价日期" :value="form.evalDate ? formatDate(form.evalDate) : '-'" />
        <CommonInfoRow label="评价结果" :value="getDictLabel('sci_achieve_evaluation', form.evalResult)" />
        <CommonInfoRow label="完成方式" :value="form.completeWay === '10' ? '独立完成' : '与外单位合作'" />
        <CommonInfoRow v-if="form.completeWay === '20'" label="合作单位名称" :value="form.coWorkDeptName" />
        <CommonInfoRow label="成果所属单位" :value="form.belongDeptName" />
        <CommonInfoRow label="所属年份" :value="form.belongYear" />
        <CommonInfoRow label="学校署名" :value="form.schoolSigned === '10' ? '第一单位' : '非第一单位'" />
        <CommonInfoRow label="完成单位" :value="form.completeDeptName" />
        <CommonInfoRow label="所属平台">
          <view class="platform-tags">
            <template v-if="platformList.length > 0">
              <text v-for="(p, index) in platformList" :key="index" class="platform-tag">
                {{ p.platformName }}
              </text>
            </template>
            <text v-else>-</text>
          </view>
        </CommonInfoRow>
        <CommonInfoRow label="备注" :value="form.remark" isColumn />
      </CommonSection>

      <!-- 完成人信息 -->
      <CommonSection title="完成人信息" v-if="form.personList?.length">
        <view class="member-list">
          <view class="member-item" v-for="(row, index) in form.personList" :key="index">
            <view class="member-header">
              <view class="m-left">
                <text class="m-name">{{ row.personName }}</text>
                <text class="m-type-tag blue">{{ getDictLabel('enacted_person_type', row.personType) }}</text>
              </view>
              <text class="m-tag" v-if="row.contributionRate">{{ row.contributionRate }}% 贡献率</text>
            </view>
            <view class="m-body">
              <view class="m-line" v-if="row.workUnitName"><text class="l">工作单位：</text><text class="v">{{ row.workUnitName }}</text></view>
              <view class="m-line"><text class="l">学位/职称：</text><text class="v">{{ getDictLabel('sci_academic_degree', row.personDegree) || '-' }} / {{ getDictLabel('sci_discipline_job_title', row.personJobTitle) || '-' }}</text></view>
            </view>
          </view>
        </view>
      </CommonSection>

      <!-- 标注经济来源 -->
      <CommonSection title="标注经济来源" v-if="form.projectList?.length">
        <view class="achievement-card" v-for="(item, index) in form.projectList" :key="index">
          <view class="a-row">
            <text class="al">关联类型：</text>
            <text class="av">{{ item.sourceType === '10' ? '项目' : '学科' }}</text>
          </view>
          <view class="a-row">
            <text class="al">关联名称：</text>
            <text class="av">{{ item.projectName || '-' }}</text>
          </view>
        </view>
      </CommonSection>

      <!-- 附件信息 -->
      <AttachmentList :list="mergedFileList" 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 AttachmentList from './AttachmentList.vue';
import CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.vue';

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

const { getDictLabel } = useDict('enacted_person_type', 'sci_academic_degree', 'sci_achieve_evaluation', 'sci_discipline_job_title');
const documentApi = useDocumentApi();
const form = ref<any>(null);
const loading = ref(false);

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 mergedFileList = computed(() => {
  if (!form.value) return [];
  const list: any[] = [];
  
  if (form.value.evalCertificates) {
    try {
      const file = JSON.parse(form.value.evalCertificates);
      list.push({ fileName: file.name || '评价证书', fileUrl: file.url, fileType: '评价证书' });
    } catch (e) {}
  }
  
  if (form.value.coWorkFile) {
    try {
      const file = JSON.parse(form.value.coWorkFile);
      list.push({ fileName: file.name || '合作单位文件', fileUrl: file.url, fileType: '合作文件' });
    } catch (e) {}
  }
  
  return list;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getEvaluationByCode(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>
