<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载成果转化详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 成果基本信息 (根据成果类型显示) -->

      <!-- 专利类 -->
      <template v-if="form.achievementType === '10'">
        <CommonSection title="成果信息 (专利)" :isFirst="true">
          <CommonInfoRow label="专利名称" :value="form.patentName" />
          <CommonInfoRow label="专利号" :value="form.patentNumber" />
          <CommonInfoRow label="专利类型" :value="getDictLabel('patent_class', form.patentClass)" />
          <CommonInfoRow label="专利范围" :value="getDictLabel('patent_scope', form.patentScope)" />
          <CommonInfoRow label="权利人" :value="form.patentObligee" />
          <CommonInfoRow label="所属单位" :value="form.deptName" />
          <CommonInfoRow label="所属平台" isColumn>
            <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.belongTeam || '-'" />
          <CommonInfoRow label="授权号" :value="form.patentAccreditCode" />
          <CommonInfoRow label="授权日期" :value="formatDate(form.effectiveTime)" />
          <CommonInfoRow label="是否职务专利" :value="form.patentOffice === '10' ? '是' : form.patentOffice === '20' ? '否' : '-'" />
          <CommonInfoRow label="是否PCT专利" :value="form.patentCt === '10' ? '是' : form.patentCt === '20' ? '否' : '-'" />
          <CommonInfoRow label="专利简介" :value="form.patentDesc" isColumn />
        </CommonSection>

        <!-- 发明(设计)人信息 -->
        <CommonSection title="发明(设计)人信息" v-if="form.memberList?.length">
          <view class="member-list">
            <view class="member-item" v-for="(member, index) in form.memberList" :key="index">
              <view class="member-header">
                <view class="m-left">
                  <text class="m-name">{{ member.memberName }}</text>
                  <text class="m-type-tag blue">{{ getDictLabel('paper_member_type', member.memberType) }}</text>
                </view>
                <text class="m-tag" v-if="member.contributionRate">{{ member.contributionRate }}%</text>
              </view>
              <view class="m-body">
                <view class="m-line"><text class="l">学位：</text><text class="v">{{ getDictLabel('academic_degree', member.degree) || '-' }}</text></view>
                <view class="m-line"><text class="l">职称：</text><text class="v">{{ getDictLabel('discipline_job_title', member.jobTitle) || '-' }}</text></view>
              </view>
            </view>
          </view>
        </CommonSection>
      </template>

      <!-- 软件类 -->
      <template v-else-if="form.achievementType === '20'">
        <CommonSection title="成果信息 (软件)" :isFirst="true">
          <CommonInfoRow label="软件名称" :value="form.softwareName" />
          <CommonInfoRow label="版本号" :value="form.version" />
          <CommonInfoRow label="著作权人" :value="form.applicant" />
          <CommonInfoRow label="权利取得方式" :value="getDictLabel('rights_acquire_method', form.rightsAcquireMethod)" />
          <CommonInfoRow label="所属单位" :value="form.organization" />
          <CommonInfoRow label="所属平台" isColumn>
            <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.belongTeam || '-'" />
          <CommonInfoRow label="开发完成日期" :value="formatDate(form.developmentFinishDate)" />
          <CommonInfoRow label="首次发表日期" :value="formatDate(form.firstPublishDate)" />
          <CommonInfoRow label="成果权属" :value="getDictLabel('achievement_owner_unit', form.achievementOwnerUnit)" />
          <CommonInfoRow label="权利范围" :value="getDictLabel('rights_scope', form.rightsScope)" />
          <CommonInfoRow label="开发方式" :value="getDictLabel('development_method', form.developmentMethod)" />
          <CommonInfoRow label="合作单位" :value="form.partnerUnit" />
          <CommonInfoRow label="备注" :value="form.remark" isColumn />
        </CommonSection>
      </template>

      <!-- 其他类 -->
      <template v-else>
        <CommonSection title="成果信息" :isFirst="true">
          <CommonInfoRow label="成果名称" :value="form.achievementName" />
          <CommonInfoRow label="成果类型" :value="getDictLabel('other_achieve_type', form.achievementType)" />
          <CommonInfoRow label="获得日期" :value="formatDate(form.acquireTime)" />
          <CommonInfoRow label="所属单位" :value="form.organization" />
          <CommonInfoRow label="所属平台" isColumn>
            <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.belongTeam || '-'" />
          <CommonInfoRow label="成果权属" :value="getDictLabel('achievement_owner_unit', form.achievementOwnerUnit)" />
          <CommonInfoRow label="备注" :value="form.remark" isColumn />
        </CommonSection>
      </template>

      <!-- 转化信息 -->
      <CommonSection title="转化详情">
        <CommonInfoRow label="转化时间" :value="formatDate(form.conversionDate)" />
        <CommonInfoRow label="转化金额" :value="'￥' + (form.conversionAmount || 0)" isAmount />
        <CommonInfoRow label="转化单位" :value="form.conversionUnit" />
        <CommonInfoRow label="转化方式" :value="formatDictList('conversion_type', form.conversionType)" />
        <CommonInfoRow label="成熟度" :value="formatDictList('trl_level', form.trlLevel)" />
        <CommonInfoRow label="成果水平" :value="getDictLabel('achievement_level', form.achievementLevel)" />
        <CommonInfoRow label="成果体现形式" :value="getDictLabel('results_display_form', form.displayForm)" />
        <CommonInfoRow label="国民经济领域" :value="formatDictList('economy_sector', form.economySector)" />
        <CommonInfoRow label="战略布局领域" :value="formatDictList('strategic_domain', form.strategicDomain)" />
        <CommonInfoRow label="制造业集群" :value="formatDictList('manufacturing_cluster', form.manufacturingCluster)" />
        <CommonInfoRow label="是否成立企业" :value="form.isCompany === '10' ? '是' : form.isCompany === '20' ? '否' : '-'" />
        <CommonInfoRow v-if="form.isCompany === '10'" label="企业名称" :value="form.companyName" />
        <CommonInfoRow label="成果简介" :value="form.remark" isColumn />
      </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(
  'patent_class', 'patent_scope', 'rights_acquire_method', 'other_achieve_type',
  'conversion_type', 'trl_level', 'achievement_level', 'results_display_form',
  'economy_sector', 'strategic_domain', 'manufacturing_cluster',
  'paper_member_type', 'academic_degree', 'discipline_job_title',
  'achievement_owner_unit', 'development_method', 'rights_scope'
);

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 formatDictList = (dictType: string, values: any) => {
  if (!values) return '-';
  const valList = Array.isArray(values) ? values : String(values).split(',');
  return valList.map(v => getDictLabel(dictType, v)).join(', ') || '-';
};

const mergedFileList = computed(() => {
  if (!form.value) return [];
  const list: any[] = [];
  
  // 成果附件
  const achieveFiles = form.value.fileList || form.value.uploadList || [];
  if (Array.isArray(achieveFiles)) {
    achieveFiles.forEach(f => list.push({ fileName: f.name, fileUrl: f.url, fileType: '成果附件' }));
  }
  
  // 转化附件
  const conversionFiles = form.value.conversionList || [];
  if (Array.isArray(conversionFiles)) {
    conversionFiles.forEach(f => list.push({ fileName: f.name, fileUrl: f.url, fileType: '转化附件' }));
  }
  
  return list;
});

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