<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.standardName || '-' }}</text></view>
        <view class="info-row"><text class="label">标准编号</text><text class="value">{{ form.standardCode || '-' }}</text></view>
        <view class="info-row"><text class="label">成果所属单位</text><text class="value">{{ form.achievementBelongUnitName || '-' }}</text></view>
        <view class="info-row"><text class="label">提交部门</text><text class="value">{{ form.commitDeptName || '-' }}</text></view>
        <view class="info-row"><text class="label">提交日期</text><text class="value">{{ formatDate(form.commitDate, 'YYYY-MM-DD') }}</text></view>
        <view class="info-row"><text class="label">是否已发布</text><text class="value">{{ form.isPublished === '10' ? '是' : '否' }}</text></view>
        <view class="info-row"><text class="label">发布日期</text><text class="value">{{ formatDate(form.publishDate, 'YYYY-MM-DD') }}</text></view>
        <view class="info-row"><text class="label">单位排名</text><text class="value">{{ form.unitRanking || '-' }}</text></view>
        <view class="info-row column">
          <text class="label">所有制定单位</text>
          <text class="value remark">{{ form.belongEnactedUnit || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">所有参与人员</text>
          <text class="value remark">{{ form.belongEngagePersonal || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">标准类型</text>
          <text class="value">{{ getDictLabel('achievement_standard_type', form.standardType) }}</text>
        </view>
        <view class="info-row"><text class="label">归属年度</text><text class="value">{{ form.belongYear || '-' }}</text></view>
        <view class="info-row"><text class="label">归属团队</text><text class="value">{{ form.belongTeam || '-' }}</text></view>
        <view class="info-row"><text class="label">归属平台</text><text class="value">{{ form.belongPlatform || '-' }}</text></view>
        <view class="info-row column">
          <text class="label">备注</text>
          <text class="value remark">{{ form.remark || '-' }}</text>
        </view>
      </view>

      <!-- 制定人信息 -->
      <view class="common-section-card mt20" v-if="form.enactedPersonal?.length">
        <view class="section-title">制定人信息</view>
        <view class="achievement-card" v-for="(item, index) in form.enactedPersonal" :key="index">
          <view class="a-header">
            <text class="a-name">{{ item.enactedPersonName }}</text>
            <text class="a-tag">{{ getDictLabel('enacted_person_type', item.enactedPersonType) }}</text>
          </view>
          <view class="a-body">
            <view class="a-row">
              <text class="al">学位：</text>
              <text class="av">{{ getDictLabel('sci_academic_degree', item.enactedPersonDegree) }}</text>
            </view>
            <view class="a-row">
              <text class="al">贡献率：</text>
              <text class="av">{{ item.contributionRate }}%</text>
            </view>
            <view class="a-row">
              <text class="al">工作单位：</text>
              <text class="av">{{ item.workUnitName || '-' }}</text>
            </view>
          </view>
        </view>
      </view>

      <!-- 附件 -->
      <AttachmentList :file="attachment" 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';

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

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

const attachment = computed(() => {
  if (form.value?.standardFile) {
    try {
      return JSON.parse(form.value.standardFile);
    } catch (e) {
      return null;
    }
  }
  return null;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  // 直接通过 ID 获取以对齐 PC 逻辑
  const [err, res] = await to(documentApi.getAchievementStandardById(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>
