<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 1. 基本信息 -->
      <view class="common-section-card">
        <view class="section-title">基本信息</view>
        <view class="info-row">
          <text class="label">论文名称</text>
          <text class="value">{{ form.paperTitle || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">投稿刊物名称</text>
          <text class="value">{{ form.pubName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">备注</text>
          <text class="value">{{ form.remark || '-' }}</text>
        </view>
      </view>

      <!-- 2. 单位排名 -->
      <view class="common-section-card mt20">
        <view class="section-title">单位排名</view>
        <view class="info-row">
          <text class="label">第一名</text>
          <text class="value">{{ form.unitRankFirst || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">第二名</text>
          <text class="value">{{ form.unitRankSecond || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">第三名</text>
          <text class="value">{{ form.unitRankThird || '-' }}</text>
        </view>
      </view>

      <!-- 3. 标注经济来源 -->
      <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 border-bottom mb10 pb10">
            <text class="al">关联类型：</text>
            <text class="av font-bold">{{ item.sourceType === '10' ? '项目' : '学科' }}</text>
          </view>
          <view class="a-row">
            <text class="al">关联项目/学科：</text>
            <text class="av">{{ item.projectSource || '-' }}</text>
          </view>
        </view>
      </view>

      <!-- 4. 作者信息 -->
      <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="(author, index) in form.authorList" :key="index">
            <view class="member-header">
              <view class="name-box">
                <text class="m-name">{{ author.memberName }}</text>
                <text class="m-tag">{{ getDictLabel('sci_paper_author_type', author.authorType) }}</text>
              </view>
              <text class="m-type-tag">{{ getDictLabel('sci_paper_member_type', author.memberType) }}</text>
            </view>
            <view class="m-body">
              <view class="m-line">
                <text class="l">所属科室：</text>
                <text class="v">{{ author.deptName || '-' }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 5. 附件信息 (投稿登记表 & 科研诚信表) -->
      <view class="common-section-card mt20" v-if="uploadList.length">
        <view class="section-title">附件资料</view>
        <view class="common-file-list">
          <view class="file-item" v-for="(file, index) in uploadList" :key="index" @click="previewFile(file.url, file.name)">
            <view class="file-info">
              <text class="f-name">{{ file.name }}</text>
              <text class="f-meta">{{ file.type }}</text>
            </view>
          </view>
        </view>
      </view>
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed } from 'vue';
import { useDocumentApi } from '@/api/document';
import { useDict } from '@/hooks/useDict';
import { previewFile } from '@/utils/file';
import to from 'await-to-js';

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

const { getDictLabel } = useDict('sci_paper_author_type', 'sci_paper_member_type');
const documentApi = useDocumentApi();
const form = ref<any>(null);
const loading = ref(false);

const uploadList = computed(() => {
  if (!form.value) return [];
  const list = [];
  if (form.value.fromUrl) {
    list.push({ name: '下载', url: form.value.fromUrl, type: '投稿登记表' });
  }
  if (form.value.honestUrl) {
    list.push({ name: '下载', url: form.value.honestUrl, type: '科研诚信表' });
  }
  return list;
});

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

.achievement-card {
  background: #f8f9fc;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
  &:last-child { margin-bottom: 0; }
  .a-row {
    display: flex;
    font-size: 26rpx;
    line-height: 1.5;
    .al { color: #999; flex-shrink: 0; }
    .av { color: #333; }
  }
}

.border-bottom { border-bottom: 1rpx solid #eef0f5; }
.pb10 { padding-bottom: 10rpx; }
.mb10 { margin-bottom: 10rpx; }
.font-bold { font-weight: bold; }
</style>
