<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.applicationNo" />
        <CommonInfoRow label="申请人" :value="form.applicantName" />
        <CommonInfoRow label="申请时间" :value="formatDate(form.createdTime)" />
        <CommonInfoRow label="所属部门" :value="form.departmentName" />
        <CommonInfoRow label="项目名称" :value="form.projectName" />
        <CommonInfoRow label="项目类型" :value="getProjectTypeLabel(form.projectType)" />
        <CommonInfoRow label="经费卡号" :value="form.cardNo" />
        <CommonInfoRow label="费用科目" :value="form.expenseSubjectName" />
        <CommonInfoRow label="暂借金额" :value="form.totalAmount" isAmount />
        <CommonInfoRow label="审核状态" :value="getApproveStatusLabel(form.approveStatus)" />
        <CommonInfoRow label="报销状态" :value="getReimbursementStatusLabel(form.reimbursementStatus)" />
        <CommonInfoRow label="暂借事由" :value="form.description" isColumn />
      </CommonSection>

      <!-- 支付信息 -->
      <CommonSection title="支付信息" v-if="form.payment?.length">
        <view class="achievement-card" v-for="(pay, index) in form.payment" :key="index">
          <view class="a-row">
            <text class="al">支付方式：</text>
            <text class="av">{{ getPayTypeLabel(pay.payType) }}</text>
          </view>
          <view class="a-row">
            <text class="al">收款人/单位：</text>
            <text class="av">{{ pay.receiver || '-' }}</text>
          </view>
          <view class="a-row">
            <text class="al">收款类型：</text>
            <text class="av">{{ getReceiverTypeLabel(pay.receiverType) }}</text>
          </view>
          <view class="a-row">
            <text class="al">金额：</text>
            <text class="av red-color">￥{{ formatAmount(pay.amount) }}</text>
          </view>
        </view>
      </CommonSection>

      <!-- 附件 -->
      <AttachmentList :list="form.attachments" />
    </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 { formatAmount } from '@/utils/format';
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 getProjectTypeLabel = (type: string) => {
  const typeMap: Record<string, string> = {
    '10': '上级拨款',
    '20': '匹配经费',
    '30': '横向经费',
    '40': '平台经费',
    '50': '人才经费',
    '60': '学科经费'
  }
  return typeMap[type] || type || '-';
}

const getApproveStatusLabel = (status: string) => {
  const statusMap: Record<string, string> = {
    '10': '待提交',
    '20': '审核中',
    '30': '审核通过',
    '40': '审核驳回',
    '50': '已取消',
    '60': '已撤回'
  }
  return statusMap[status] || status || '-';
}

const getReimbursementStatusLabel = (status: string) => {
  const statusMap: Record<string, string> = {
    '10': '未转报销',
    '20': '报销审核中',
    '30': '已报销',
    '40': '报销审核驳回'
  }
  return statusMap[status] || status || '-';
}

const getPayTypeLabel = (type: string) => {
  const typeMap: Record<string, string> = {
    '10': '银行转账',
    '20': '现金支付',
    '30': '微信支付',
    '40': '支付宝'
  }
  return typeMap[type] || type || '-';
}

const getReceiverTypeLabel = (type: string) => {
  const typeMap: Record<string, string> = {
    '10': '个人',
    '20': '单位'
  }
  return typeMap[type] || type || '-';
}

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