<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.projectName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">项目类型</text>
          <text class="value">{{ getProjectTypeName(form.projectType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">项目编号</text>
          <text class="value">{{ form.projectNo || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">负责人</text>
          <text class="value">{{ form.projectIncharge || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">预算金额</text>
          <text class="value red-color">{{ formatAmount(form.projectBudgetAmount) }} 万元</text>
        </view>
        <view class="info-row">
          <text class="label">已分割绩效</text>
          <text class="value red-color">{{ formatAmount(form.projectIssuedAmount) }} 万元</text>
        </view>
        <view class="info-row">
          <text class="label">未分割绩效</text>
          <text class="value red-color">{{ formatAmount(form.projectNotIssuedAmount) }} 万元</text>
        </view>
        <view class="info-row">
          <text class="label">项目到账</text>
          <text class="value red-color">{{ formatAmount(form.projectReceivedAmount) }} 万元</text>
        </view>
        <view class="info-row">
          <text class="label">绩效总额</text>
          <text class="value red-color font-bold">{{ formatAmount(form.projectRewardAmount) }} 万元</text>
        </view>
        <view class="info-row">
          <text class="label">备注摘要</text>
          <text class="value">{{ form.remark || '-' }}</text>
        </view>
      </view>

      <!-- 2. 分割明细 -->
      <view class="common-section-card mt20" v-if="form.detail?.length">
        <view class="section-title">分割明细</view>
        <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 red-color">￥{{ item.amount }} 元</text>
            </view>
            <view class="m-row">
              <text class="ml">创建时间</text>
              <text class="pv">{{ formatDate(item.createdTime) }}</text>
            </view>
          </view>
        </view>
      </view>

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

const formatAmount = (num: number | string) => {
  if (!num && num !== 0) return '0.00';
  return Number(num).toFixed(2);
};

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: #f8f9fc;
    border-radius: 8rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;
    &:last-child { margin-bottom: 0; }
    
    .m-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12rpx;
      padding-bottom: 12rpx;
      border-bottom: 1rpx solid #eee;
      .m-name { font-size: 28rpx; color: #333; font-weight: bold; }
      .m-no { font-size: 24rpx; color: #999; }
    }
    
    .m-row {
      display: flex;
      justify-content: space-between;
      margin-top: 8rpx;
      font-size: 24rpx;
      .ml { color: #999; }
      .pv { color: #555; }
    }
  }
}

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