<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.projectName" />
        <CommonInfoRow label="项目类型" :value="getProjectTypeName(form.projectType)" />
        <CommonInfoRow label="项目编号" :value="form.projectNo" />
        <CommonInfoRow label="负责人" :value="form.projectIncharge" />
        <CommonInfoRow label="预算金额" :value="form.projectBudgetAmount" suffix=" 万元" isAmount />
        <CommonInfoRow label="已分割绩效" :value="form.projectIssuedAmount" suffix=" 万元" isAmount />
        <CommonInfoRow label="未分割绩效" :value="form.projectNotIssuedAmount" suffix=" 万元" isAmount />
        <CommonInfoRow label="项目到账" :value="form.projectReceivedAmount" suffix=" 万元" isAmount />
        <CommonInfoRow label="绩效总额" :value="form.projectRewardAmount" suffix=" 万元" isAmount />
        <CommonInfoRow label="备注摘要" :value="form.remark" isColumn />
      </CommonSection>

      <!-- 2. 分割明细 -->
      <CommonSection title="分割明细" v-if="form.detail?.length">
        <view class="member-list">
          <view class="member-item" v-for="(item, index) in form.detail" :key="index">
            <view class="m-header">
              <text class="m-name">{{ item.memberName }}</text>
              <text class="m-no">{{ item.memberNo }}</text>
            </view>
            <view class="m-row">
              <text class="ml">工作单位</text>
              <text class="pv">{{ item.unit || '-' }}</text>
            </view>
            <view class="m-row">
              <text class="ml">分割金额</text>
              <text class="pv primary-color">￥{{ item.amount }} 元</text>
            </view>
            <view class="m-row">
              <text class="ml">创建时间</text>
              <text class="pv">{{ formatDate(item.createdTime) }}</text>
            </view>
          </view>
        </view>
      </CommonSection>

      <!-- 3. 附件 -->
      <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 } from 'vue';
import { useDocumentApi } from '@/api/document';
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 getProjectTypeName = (val: string) => {
  const map: Record<string, string> = {
    '10': '纵向项目',
    '20': '横向项目',
    '30': '内部项目',
    '40': '重点学科',
  };
  return map[val] || val;
};

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

.member-list {
  .member-item {
    background: #f8fafc;
    border-radius: 12rpx;
    padding: 24rpx;
    margin-bottom: 20rpx;
    border: 1rpx solid #f1f5f9;
    &:last-child { margin-bottom: 0; }
    
    .m-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16rpx;
      padding-bottom: 16rpx;
      border-bottom: 1rpx dashed #e2e8f0;
      .m-name { font-size: 28rpx; color: #1e293b; font-weight: 700; }
      .m-no { font-size: 24rpx; color: #64748b; }
    }
    
    .m-row {
      display: flex;
      justify-content: space-between;
      margin-top: 10rpx;
      font-size: 26rpx;
      .ml { color: #64748b; }
      .pv { color: #334155; font-weight: 500; }
    }
  }
}
</style>
