<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载结转详情..."></uv-loading-icon>
    <template v-else-if="form">
      <CommonSection title="项目信息" :isFirst="true">
        <CommonInfoRow label="项目名称" :value="form.projectName" />
        <CommonInfoRow label="项目类型" :value="getProjectTypeName(form.projectType)" />
        <CommonInfoRow label="项目编号" :value="form.projectNo" />
        <CommonInfoRow label="负责人" :value="form.projectIncharge" />
        <CommonInfoRow label="可结转金额" :value="form.projectBudgetAmount / 10000" suffix=" 万元" isAmount />
        <CommonInfoRow label="结转经费卡" :value="form.conclusionCardName" />
        <CommonInfoRow label="结题报告" v-if="form.conclusionReport" >
          <text class="value link-text" @click="previewFile(form.conclusionReport, '结题报告文件')">查看文件</text>
        </CommonInfoRow>
        <CommonInfoRow label="说明" :value="form.remark" isColumn />
      </CommonSection>
    </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 { previewFile } from '@/utils/file';
import to from 'await-to-js';
import CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.vue';

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

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

const getProjectTypeName = (val: string) => {
  const map: Record<string, string> = {
    '10': '纵向项目',
    '20': '横向项目',
    '30': '内部项目',
    '40': '重点学科',
  };
  return map[val] || val;
};

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getFundCarryoverById(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";
.link-text { color: #3b82f6; font-weight: 600; }
</style>
