<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.paperName || '-' }}</text></view>
        <view class="info-row">
          <text class="label">论文类型</text>
          <text class="value">{{ getDictLabel('sci_paper_type', form.paperType) }}</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.publicationName || '-' }}</text></view>
        <view class="info-row">
          <text class="label">期刊类型</text>
          <text class="value">{{ getDictLabel('sci_publication_range', form.publicationRange) }}</text>
        </view>
        <view class="info-row">
          <text class="label">中科院分区</text>
          <text class="value">{{ getDictLabel('sci_paper_subTreasury', form.subTreasury) }}</text>
        </view>
        <view class="info-row"><text class="label">IF</text><text class="value">{{ form.impactFactors || '-' }}</text></view>
        <view class="info-row"><text class="label">DOI号/PMID</text><text class="value">{{ form.doi || '-' }}</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.yearNum || '' }}/{{ form.volNum || '' }}/{{ form.issueNum || '' }}/{{ form.pageRange || '' }}</text>
        </view>
        <view class="info-row">
          <text class="label">本院署名</text>
          <text class="value">{{ getDictLabel('sci_paper_signType', form.signType) }}</text>
        </view>
        <view class="info-row"><text class="label">ISSN号</text><text class="value">{{ form.issnNum || '-' }}</text></view>
      </view>

      <!-- 作者信息 -->
      <view class="common-section-card mt20" v-if="form.authorList?.length">
        <view class="section-title">作者信息</view>
        <view class="member-list">
          <view class="member-item" v-for="(row, index) in form.authorList" :key="index">
            <view class="member-header">
              <view class="name-box">
                <text class="m-name">{{ row.memberName }}</text>
                <text class="m-tag">{{ getDictLabel('sci_paper_author_type', row.authorType) }}</text>
              </view>
              <text class="m-type-tag">{{ getDictLabel('sci_paper_member_type', 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.technicalTitle"><text class="l">职称：</text><text class="v">{{ row.technicalTitle }}</text></view>
            </view>
          </view>
        </view>
      </view>

      <!-- 标注经济来源 -->
      <view class="common-section-card mt20" v-if="form.projList?.length">
        <view class="section-title">标注经济来源</view>
        <view class="achievement-card" v-for="(item, index) in form.projList" :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.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_paper_type', 
  'sci_publication_range', 
  'sci_paper_subTreasury', 
  'sci_paper_signType', 
  'sci_paper_author_type', 
  'sci_paper_member_type'
);
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.paperResultList) list.push(...form.value.paperResultList.map((item: any) => ({ ...item, fileType: '论文全文' })));
  if (form.value.includedProofList) list.push(...form.value.includedProofList.map((item: any) => ({ ...item, fileType: '收录证明' })));
  if (form.value.ethicsApprovalList) list.push(...form.value.ethicsApprovalList.map((item: any) => ({ ...item, fileType: '伦理批件' })));
  if (form.value.rawDataList) list.push(...form.value.rawDataList.map((item: any) => ({ ...item, fileType: '原始数据' })));
  return list;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  // 提取 code 中的业务 ID
  const paperCode = props.code.includes('-') ? props.code.split('-')[1] : props.code;
  const [err, res] = await to(documentApi.getPaperByCode(paperCode));
  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>
