<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.applicationNo || '-' }}</text></view>
        <view class="info-row"><text class="label">申请人</text><text class="value">{{ form.applicantName || '-' }}</text></view>
        <view class="info-row"><text class="label">申请时间</text><text class="value">{{ form.createdTime || '-' }}</text></view>
        <view class="info-row"><text class="label">所属部门</text><text class="value">{{ form.departmentName || '-' }}</text></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">{{ getProjectTypeLabel(form.projectType) }}</text>
        </view>
        <view class="info-row"><text class="label">经费卡号</text><text class="value">{{ form.cardNo || '-' }}</text></view>
        <view class="info-row"><text class="label">费用科目</text><text class="value">{{ form.expenseSubjectName || '-' }}</text></view>
        <view class="info-row">
          <text class="label">暂借金额(元)</text>
          <text class="value red-color">￥{{ formatMoney(form.totalAmount) }}</text>
        </view>
        <view class="info-row">
          <text class="label">审核状态</text>
          <text class="value">{{ getApproveStatusLabel(form.approveStatus) }}</text>
        </view>
        <view class="info-row">
          <text class="label">报销状态</text>
          <text class="value">{{ getReimbursementStatusLabel(form.reimbursementStatus) }}</text>
        </view>
        <view class="info-row column">
          <text class="label">暂借事由</text>
          <text class="value remark">{{ form.description || '-' }}</text>
        </view>
      </view>

      <!-- 支付信息 -->
      <view class="common-section-card mt20" v-if="form.payment?.length">
        <view class="section-title">支付信息</view>
        <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">￥{{ formatMoney(pay.amount) }}</text>
          </view>
        </view>
      </view>

      <!-- 附件 -->
      <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 to from 'await-to-js';
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 formatMoney = (val: any) => {
  if (val === undefined || val === null || val === '') return '0.00';
  return Number(val).toLocaleString('zh-CN', { minimumFractionDigits: 2 });
};

const formatFileSize = (size: number) => {
  if (!size) return '0B';
  const units = ['B', 'KB', 'MB', 'GB'];
  let index = 0;
  while (size >= 1024 && index < units.length - 1) {
    size /= 1024;
    index++;
  }
  return `${size.toFixed(2)}${units[index]}`;
};

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>
