<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 1. 正常结题/验收 (纵向/自发项目) -->
      <template v-if="form.projectType === '10' || form.projectType === '30'">
        <CommonSection title="基本信息" :isFirst="true">
          <CommonInfoRow label="实际执行周期" :value="form.actualExecutionCycle + ' 月'" />
          <CommonInfoRow label="经费执行比例" :value="form.fundsExecRatio + ' %'" />
          <CommonInfoRow label="结题申请日期" :value="formatDate(form.conclusionDate)" />
        </CommonSection>
      </template>

      <!-- 2. 项目终止 (横向项目) -->
      <template v-else-if="form.projectType === '20'">
        <CommonSection title="终止信息" :isFirst="true">
          <CommonInfoRow label="项目终止时间" :value="formatDate(form.conclusionDate)" />
          <CommonInfoRow label="项目终止说明" :value="form.conclusionDesc" isColumn />
        </CommonSection>
      </template>

      <!-- 3. 附件信息 (公共) -->
      <AttachmentList :list="form.fileList" title="附件资料" />
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { useDocumentApi } from '@/api/document';
import { formatDate } from '@/utils/date';
import to from 'await-to-js';
import CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.vue';
import AttachmentList from './AttachmentList.vue';

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

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

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