<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.projectName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">项目类型</text>
          <text class="value">{{ getProjectTypeName(form.projectType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">项目编号</text>
          <text class="value">{{ form.projectNo || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">负责人</text>
          <text class="value">{{ form.projectIncharge || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">可结转金额</text>
          <text class="value red-color font-bold">{{ formatAmount(form.projectBudgetAmount) }} 万元</text>
        </view>
        <view class="info-row">
          <text class="label">结转经费卡</text>
          <text class="value">{{ form.conclusionCardName || '-' }}</text>
        </view>
        <view class="info-row" v-if="form.conclusionReport">
          <text class="label">附件查看</text>
          <text class="value link-text" @click="previewFile(form.conclusionReport, '结题报告文件')">结题报告文件</text>
        </view>
        <view class="info-row">
          <text class="label">说明</text>
          <text class="value">{{ form.remark || '-' }}</text>
        </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 { 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 getProjectTypeName = (val: string) => {
  const map: Record<string, string> = {
    '10': '纵向项目',
    '20': '横向项目',
    '30': '内部项目',
    '40': '重点学科',
  };
  return map[val] || val;
};

const formatAmount = (num: number | string) => {
  if (!num && num !== 0) return '0.00';
  return (Number(num) / 10000).toFixed(2);
};

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: #007aff; text-decoration: underline; }
</style>
