<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'">
        <view class="common-section-card">
          <view class="section-title">基本信息</view>
          <view class="info-row">
            <text class="label">实际执行周期</text>
            <text class="value">{{ form.actualExecutionCycle || '-' }} 月</text>
          </view>
          <view class="info-row">
            <text class="label">经费执行比例</text>
            <text class="value">{{ form.fundsExecRatio || '-' }} %</text>
          </view>
          <view class="info-row">
            <text class="label">结题申请日期</text>
            <text class="value">{{ formatDate(form.conclusionDate) }}</text>
          </view>
        </view>
      </template>

      <!-- 2. 项目终止 (横向项目) -->
      <template v-else-if="form.projectType === '20'">
        <view class="common-section-card">
          <view class="section-title">终止信息</view>
          <view class="info-row">
            <text class="label">项目终止时间</text>
            <text class="value">{{ formatDate(form.conclusionDate) }}</text>
          </view>
          <view class="info-row">
            <text class="label">项目终止说明</text>
            <text class="value">{{ form.conclusionDesc || '-' }}</text>
          </view>
        </view>
      </template>

      <!-- 3. 附件信息 (公共) -->
      <view class="common-section-card mt20" v-if="form.fileList?.length">
        <view class="section-title">附件资料</view>
        <view class="common-file-list">
          <view class="file-item" v-for="(file, index) in form.fileList" :key="index" @click="previewFile(file.fileUrl, file.fileName)">
            <view class="file-info">
              <text class="f-name">{{ file.fileName }}</text>
              <view class="f-meta">
                <text class="f-type">{{ file.fileType }}{{ file.required ? ' *' : '' }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </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 { previewFile } from '@/utils/file';
import to from 'await-to-js';

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>
