<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.patentName || '-' }}</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">{{ getDictLabel('sci_patent_class', form.patentClass) }}</text>
        </view>
        <view class="info-row">
          <text class="label">专利范围</text>
          <text class="value">{{ getDictLabel('sci_patent_scope', form.patentScope) }}</text>
        </view>
        <view class="info-row">
          <text class="label">专利状态</text>
          <text class="value">{{ getDictLabel('sci_patent_condition', form.patentCondition) }}</text>
        </view>
        <view class="info-row"><text class="label">专利（申请）号</text><text class="value">{{ form.patentNumber || '-' }}</text></view>
        <view class="info-row"><text class="label">申请人</text><text class="value">{{ form.applicantName || '-' }}</text></view>
        <view class="info-row"><text class="label">第几参与人</text><text class="value">{{ form.patentApplicationCode || '-' }}</text></view>
        <view class="info-row"><text class="label">申请日期</text><text class="value">{{ form.patentApplicationDate || '-' }}</text></view>
        <view class="info-row"><text class="label">发明人</text><text class="value">{{ showMembers(form.memberList) }}</text></view>
        <view class="info-row"><text class="label">分类号</text><text class="value">{{ form.classNum || '-' }}</text></view>
        <view class="info-row"><text class="label">公开号</text><text class="value">{{ form.patentPublicCode || '-' }}</text></view>
        <view class="info-row"><text class="label">公开日期</text><text class="value">{{ form.patentPublicDate || '-' }}</text></view>
        <view class="info-row"><text class="label">授权号</text><text class="value">{{ form.patentAccreditCode || '-' }}</text></view>
        <view class="info-row"><text class="label">授权日期</text><text class="value">{{ form.effectiveTime || '-' }}</text></view>
        <view class="info-row">
          <text class="label">署名</text>
          <text class="value">{{ getDictLabel('sci_circuit_signature', form.patentSchoolSignature) }}</text>
        </view>
        <view class="info-row"><text class="label">专利权人</text><text class="value">{{ form.patentObligee || '-' }}</text></view>
        <view class="info-row"><text class="label">专利代理机构</text><text class="value">{{ form.entrustUnit || '-' }}</text></view>
        <view class="info-row"><text class="label">代理人</text><text class="value">{{ form.agencyUserName || '-' }}</text></view>
        <view class="info-row"><text class="label">代理人联系电话</text><text class="value">{{ form.agencyPhone || '-' }}</text></view>
        <view class="info-row"><text class="label">专利文献出版日</text><text class="value">{{ form.publicationDate || '-' }}</text></view>
        <view class="info-row"><text class="label">所属年份</text><text class="value">{{ form.patentYeat || '-' }}</text></view>
        <view class="info-row">
          <text class="label">是否失效</text>
          <text class="value">{{ form.patentEffectiveness === '10' ? '是' : '否' }}</text>
        </view>
        <view class="info-row">
          <text class="label">合作类型</text>
          <text class="value">{{ getDictLabel('sci_cooperation', form.patentCooperation) }}</text>
        </view>
        <view class="info-row">
          <text class="label">是否职务专利</text>
          <text class="value">{{ form.patentOffice === '10' ? '是' : '否' }}</text>
        </view>
        <view class="info-row">
          <text class="label">是否为PCT专利</text>
          <text class="value">{{ form.patentCt === '10' ? '是' : '否' }}</text>
        </view>
        
        <!-- 专利转化特有字段 -->
        <template v-if="code.includes('专利转化')">
          <view class="info-row">
            <text class="label">转化时间</text>
            <text class="value">{{ form.invertTime || '-' }}</text>
          </view>
          <view class="info-row">
            <text class="label">转化金额</text>
            <text class="value primary-color">{{ form.invertAmount || '0' }} 元</text>
          </view>
          <view class="info-row">
            <text class="label">转化单位</text>
            <text class="value">{{ form.invertUnit || '-' }}</text>
          </view>
          <view class="info-row">
            <text class="label">知识产权交易类型</text>
            <text class="value">{{ form.tradeType === '10' ? '转让' : form.tradeType === '20' ? '许可' : '-' }}</text>
          </view>
        </template>

        <view class="info-row column">
          <text class="label">专利简介</text>
          <text class="value remark">{{ form.patentDesc || '-' }}</text>
        </view>
      </view>

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

      <!-- 附件信息 -->
      <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 to from 'await-to-js';
import AttachmentList from './AttachmentList.vue';

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

const { getDictLabel } = useDict(
  'sci_patent_class', 
  'sci_patent_scope', 
  'sci_patent_condition', 
  'sci_circuit_signature', 
  'sci_cooperation'
);
const documentApi = useDocumentApi();

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

const mergedFileList = computed(() => {
  if (!form.value) return [];
  const list: any[] = [];
  if (form.value.patentFile) {
    try {
      const file = JSON.parse(form.value.patentFile);
      list.push({ ...file, fileType: '专利附件' });
    } catch (e) {}
  }
  if (form.value.agencyContractFile) {
    try {
      const file = JSON.parse(form.value.agencyContractFile);
      list.push({ ...file, fileType: '委托合同' });
    } catch (e) {}
  }
  if (form.value.invertUrl) {
    try {
      const file = JSON.parse(form.value.invertUrl);
      list.push({ ...file, fileType: '转化附件' });
    } catch (e) {}
  }
  return list;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const patentCode = props.code.includes('-') ? props.code.split('-')[1] : props.code;
  const [err, res] = await to(documentApi.getPatentByCode(patentCode));
  if (!err && res?.data) {
    form.value = res.data;
  }
  loading.value = false;
};

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

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

const showMembers = (list: any[]) => {
  return list?.map(m => m.memberName).join(', ') || '-';
};
</script>

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