<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 1. 任务批次信息 -->
      <view class="common-section-card">
        <view class="section-title">中检批次信息</view>
        <view class="info-row">
          <text class="label">中检名称</text>
          <text class="value">{{ form.batchName || '-' }}</text>
        </view>
        <view class="info-row" v-if="form.projectType === '20'">
          <text class="label">中检周期</text>
          <text class="value">{{ stateBatchDate || '-' }}</text>
        </view>
      </view>

      <!-- 2. 基本信息 (纵向/自发) -->
      <template v-if="form.projectType === '10' || form.projectType === '30'">
        <view class="common-section-card mt20">
          <view class="section-title">基本信息</view>
          <view class="info-row">
            <text class="label">项目名称</text>
            <text class="value">{{ form.projectTitle || '-' }}</text>
          </view>
          <view class="info-row">
            <text class="label">项目实施周期</text>
            <text class="value">{{ form.implementCycle || '-' }} 月</text>
          </view>
          <view class="info-row">
            <text class="label">进行时间</text>
            <text class="value">{{ form.implementExecute || '-' }} 月</text>
          </view>
          <view class="info-row">
            <text class="label">项目完成占比</text>
            <text class="value">{{ form.implementCycleProp || '0' }} %</text>
          </view>
          <view class="info-row">
            <text class="label">项目经费总额</text>
            <text class="value red-color font-bold">{{ formatAmount(form.fundsTotal) }} 万元</text>
          </view>
        </view>
      </template>

      <!-- 2. 基本信息 (横向) -->
      <template v-else-if="form.projectType === '20'">
        <view class="common-section-card mt20">
          <view class="section-title">基本信息</view>
          <view class="info-row">
            <text class="label">项目名称</text>
            <text class="value">{{ form.projectTitle || '-' }}</text>
          </view>
          <view class="info-row">
            <text class="label">起止日期</text>
            <text class="value">{{ formatDate(form.projectStartDate) }} ~ {{ formatDate(form.projectEndDate) }}</text>
          </view>
          <view class="info-row">
            <text class="label">实施周期</text>
            <text class="value">{{ form.implCycle || '-' }}</text>
          </view>
          <view class="info-row">
            <text class="label">完成占比</text>
            <text class="value">{{ form.implementCycleProp || '0' }} %</text>
          </view>
          <view class="info-row">
            <text class="label">{{ form.isClinicalTrial === '10' ? '合同经费' : '科研经费' }}</text>
            <text class="value red-color font-bold">{{ formatAmount(form.fundsTotal) }} 万元</text>
          </view>
          <view class="info-row">
            <text class="label">经费使用总额</text>
            <text class="value red-color">{{ formatAmountV2(form.fundsUsed) }} 元</text>
          </view>
          <view class="info-row">
            <text class="label">费用使用占比</text>
            <text class="value">{{ form.fundsProp || '0' }} %</text>
          </view>
          <view class="info-row">
            <text class="label">项目进展</text>
            <text class="value">{{ form.projectProgress || '-' }}</text>
          </view>
        </view>

        <!-- 3. 费用经费支出 (横向特有) -->
        <view class="common-section-card mt20" v-if="form.expenseList?.length">
          <view class="section-title">费用经费支出</view>
          <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">{{ 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 red-color">{{ formatAmount(item.contractAmount) }} 万元</text>
              </view>
              <view class="ex-grid mt20">
                <view class="eg-item">
                  <text class="egl">支出金额</text>
                  <text class="egv red-color">￥{{ formatAmountV2(item.amount) }}</text>
                </view>
                <view class="eg-item">
                  <text class="egl">财政拨款</text>
                  <text class="egv red-color">￥{{ formatAmountV2(item.projectAmount) }}</text>
                </view>
                <view class="eg-item">
                  <text class="egl">匹配经费</text>
                  <text class="egv red-color">￥{{ formatAmountV2(item.otherAmount) }}</text>
                </view>
                <view class="eg-item">
                  <text class="egl">自筹经费</text>
                  <text class="egv red-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>
        </view>
      </template>

      <!-- 附件信息 (公共) -->
      <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>
              <text class="f-meta">{{ file.fileType }}{{ file.required ? ' *' : '' }}</text>
            </view>
          </view>
        </view>
      </view>
    </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 { 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 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: #f8f9fc;
    border-radius: 12rpx;
    padding: 24rpx;
    margin-bottom: 24rpx;
    &:last-child { margin-bottom: 0; }
    
    .ex-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16rpx;
      padding-bottom: 12rpx;
      border-bottom: 1rpx solid #eef0f5;
      .ex-no { font-size: 26rpx; color: #333; font-weight: 500; }
      .ex-type { font-size: 20rpx; background: #e6f7ff; color: #1890ff; padding: 2rpx 12rpx; border-radius: 4rpx; }
    }
    
    .ex-row {
      display: flex;
      justify-content: space-between;
      font-size: 24rpx;
      margin-top: 8rpx;
      .el { color: #999; }
      .ev { color: #555; }
    }
    
    .ex-grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 16rpx;
      .eg-item {
        background: #fff;
        padding: 12rpx 16rpx;
        border-radius: 8rpx;
        display: flex;
        flex-direction: column;
        .egl { font-size: 20rpx; color: #999; margin-bottom: 4rpx; }
        .egv { font-size: 24rpx; font-weight: bold; }
      }
    }
  }
}

.font-bold { font-weight: bold; }
</style>
