<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 1. 任务批次信息 -->
      <CommonSection title="中检批次信息" :isFirst="true">
        <CommonInfoRow label="中检名称" :value="form.batchName" />
        <CommonInfoRow label="中检周期" v-if="form.projectType === '20'" :value="stateBatchDate" />
      </CommonSection>

      <!-- 2. 基本信息 (纵向/自发) -->
      <template v-if="form.projectType === '10' || form.projectType === '30'">
        <CommonSection title="基本信息">
          <CommonInfoRow label="项目名称" :value="form.projectTitle" />
          <CommonInfoRow label="项目实施周期" :value="form.implementCycle + ' 月'" />
          <CommonInfoRow label="进行时间" :value="form.implementExecute + ' 月'" />
          <CommonInfoRow label="项目完成占比" :value="form.implementCycleProp + ' %'" />
          <CommonInfoRow label="项目经费总额" :value="form.fundsTotal" suffix=" 万元" isAmount />
        </CommonSection>
      </template>

      <!-- 2. 基本信息 (横向) -->
      <template v-else-if="form.projectType === '20'">
        <CommonSection title="基本信息">
          <CommonInfoRow label="项目名称" :value="form.projectTitle" />
          <CommonInfoRow label="起止日期" :value="formatDate(form.projectStartDate) + ' ~ ' + formatDate(form.projectEndDate)" />
          <CommonInfoRow label="实施周期" :value="form.implCycle" />
          <CommonInfoRow label="完成占比" :value="form.implementCycleProp + ' %'" />
          <CommonInfoRow :label="form.isClinicalTrial === '10' ? '合同经费' : '科研经费'" :value="form.fundsTotal" suffix=" 万元" isAmount />
          <CommonInfoRow label="经费使用总额" :value="form.fundsUsed" isAmount />
          <CommonInfoRow label="费用使用占比" :value="form.fundsProp + ' %'" />
          <CommonInfoRow label="项目进展" :value="form.projectProgress" />
        </CommonSection>

        <!-- 3. 费用经费支出 (横向特有) -->
        <CommonSection title="费用经费支出" v-if="form.expenseList?.length">
          <view class="expense-list">
            <view class="expense-item" v-for="(item, index) in form.expenseList" :key="index">
              <view class="ex-header">
                <text class="ex-no">No.{{ item.expenseNo }}</text>
                <text class="ex-type m-type-tag blue">{{ item.expenseType === '10' ? '直接费用' : '间接费用' }}</text>
              </view>
              <view class="ex-row">
                <text class="el">支出科目</text>
                <text class="ev">{{ item.subject || '-' }}</text>
              </view>
              <view class="ex-row">
                <text class="el">合同金额</text>
                <text class="ev primary-color">{{ formatAmount(item.contractAmount) }} 万元</text>
              </view>
              <view class="ex-grid mt20">
                <view class="eg-item">
                  <text class="egl">支出金额</text>
                  <text class="egv primary-color">￥{{ formatAmountV2(item.amount) }}</text>
                </view>
                <view class="eg-item">
                  <text class="egl">财政拨款</text>
                  <text class="egv primary-color">￥{{ formatAmountV2(item.projectAmount) }}</text>
                </view>
                <view class="eg-item">
                  <text class="egl">匹配经费</text>
                  <text class="egv primary-color">￥{{ formatAmountV2(item.otherAmount) }}</text>
                </view>
                <view class="eg-item">
                  <text class="egl">自筹经费</text>
                  <text class="egv primary-color">￥{{ formatAmountV2(item.raiseAmount) }}</text>
                </view>
              </view>
              <view class="ex-row mt20">
                <text class="el">收款人</text>
                <text class="ev">{{ item.receiver || '-' }}</text>
              </view>
            </view>
          </view>
        </CommonSection>
      </template>

      <!-- 附件信息 (公共) -->
      <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, computed } from 'vue';
import { useDocumentApi } from '@/api/document';
import { formatAmount } from '@/utils/format';
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 stateBatchDate = computed(() => {
  if (!form.value?.batchStartDate || !form.value?.batchEndDate) return '';
  return `${formatDate(form.value.batchStartDate)} ~ ${formatDate(form.value.batchEndDate)}`;
});

const formatAmountV2 = (num: number | string) => {
  if (!num && num !== 0) return '0.00';
  return Number(num).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
};

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

.expense-list {
  .expense-item {
    background: #f8fafc;
    border-radius: 12rpx;
    padding: 24rpx;
    margin-bottom: 24rpx;
    border: 1rpx solid #f1f5f9;
    &:last-child { margin-bottom: 0; }
    
    .ex-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16rpx;
      padding-bottom: 16rpx;
      border-bottom: 1rpx dashed #e2e8f0;
      .ex-no { font-size: 28rpx; color: #1e293b; font-weight: 600; }
    }
    
    .ex-row {
      display: flex;
      justify-content: space-between;
      font-size: 26rpx;
      margin-top: 12rpx;
      .el { color: #64748b; }
      .ev { color: #334155; font-weight: 500; }
    }
    
    .ex-grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 16rpx;
      .eg-item {
        background: #fff;
        padding: 16rpx;
        border-radius: 8rpx;
        display: flex;
        flex-direction: column;
        border: 1rpx solid #f1f5f9;
        .egl { font-size: 22rpx; color: #64748b; margin-bottom: 8rpx; }
        .egv { font-size: 26rpx; font-weight: 800; font-family: 'Inter', sans-serif; }
      }
    }
  }
}
</style>
