<template>
  <view class="document-form">
    <uv-loading-icon v-if="state.loading" mode="circle" text="正在加载项目计划详情..."></uv-loading-icon>
    <template v-else-if="state.form">
      <!-- 基本信息 -->
      <view class="common-section-card">
        <view class="section-title">基本信息</view>
        <view class="info-row"><text class="label">项目名称</text><text class="value">{{ state.form.projectName }}</text></view>
        <view class="info-row">
          <text class="label">项目级别</text>
          <text class="value">{{ getDictLabel('sci_pjt_level', state.form.projectLevel) }}</text>
        </view>
        <view class="info-row"><text class="label">项目来源</text><text class="value">{{ state.form.projectSource || '-' }}</text></view>
        <view class="info-row">
          <text class="label">计划日期</text>
          <text class="value">{{ formatDate(state.form.planStartDate) }} 至 {{ formatDate(state.form.planEndDate) }}</text>
        </view>
        <view class="info-row">
          <text class="label">研究类型</text>
          <text class="value">{{ getDictLabel('sci_pjt_type', state.form.studyType) }}</text>
        </view>
        <view class="info-row"><text class="label">所属科室</text><text class="value">{{ state.form.deptName }}</text></view>
        <view class="info-row"><text class="label">项目负责人</text><text class="value">{{ state.form.projectLeaderName }}</text></view>
        <view class="info-row"><text class="label">负责人电话</text><text class="value">{{ state.form.projectLeaderPhone || '-' }}</text></view>
        <view class="info-row"><text class="label">负责人邮箱</text><text class="value">{{ state.form.projectLeaderMail || '-' }}</text></view>
        <view class="info-row"><text class="label">备注</text><text class="value">{{ state.form.remark || '-' }}</text></view>
      </view>

      <!-- 成员信息 -->
      <view class="common-section-card mt20" v-if="state.form.memberList?.length">
        <view class="section-title">成员信息</view>
        <view class="member-list">
          <view class="member-item" v-for="(row, index) in state.form.memberList" :key="index">
            <view class="member-header">
              <view class="m-left">
                <text class="m-name">{{ row.memberName }}</text>
                <text class="m-type-tag" v-if="row.memberType">
                  {{ row.memberType === '10' ? '本院人员' : row.memberType === '20' ? '非本院人员' : '研究生' }}
                </text>
              </view>
              <text class="m-tag">{{ row.projectRole === '10' ? '负责人' : row.projectRole === '20' ? '主要参与人' : '一般参与人' }}</text>
            </view>
            <view class="m-body">
              <view class="m-line" v-if="row.deptName"><text class="l">所属科室：</text><text class="v">{{ row.deptName }}</text></view>
              <view class="m-line" v-if="row.degree || row.technicalTitle">
                <text class="l">职称学位：</text>
                <text class="v">{{ getDictLabel('sci_technical_title', row.technicalTitle) || '-' }} / {{ getDictLabel('sci_academic_degree', row.degree) || '-' }}</text>
              </view>
              <view class="m-line" v-if="row.responsibleContent"><text class="l">负责内容：</text><text class="v">{{ row.responsibleContent }}</text></view>
              <view class="m-line" v-if="row.serialNum"><text class="l">签署顺序：</text><text class="v">{{ row.serialNum }}</text></view>
            </view>
          </view>
        </view>
      </view>

      <!-- 预算信息 -->
      <view class="common-section-card mt20" v-if="state.form.fundsList?.length">
        <view class="section-title">预算信息</view>
        <view class="funds-list">
          <view class="funds-item" v-for="(item, index) in state.form.fundsList" :key="index">
            <view class="f-row">
              <text class="f-name">{{ item.fundsSubjName }}</text>
              <text class="f-class">{{ item.fundsClass === '10' ? '直接费用' : '间接费用/管理费' }}</text>
            </view>
            <view class="f-grid">
              <view class="g-item"><text class="gl">财政拨款(万元)</text><text class="gv">{{ formatAmount(item.projectFundsAmount) }}</text></view>
              <view class="g-item"><text class="gl">匹配经费(万元)</text><text class="gv">{{ formatAmount(item.otherFundsAmount) }}</text></view>
              <view class="g-item"><text class="gl">自筹经费(万元)</text><text class="gv">{{ formatAmount(item.raiseFundsAmount) }}</text></view>
              <view class="g-item highlight"><text class="gl">预算金额(万元)</text><text class="gv">{{ formatAmount(item.totalFundsAmount) }}</text></view>
            </view>
          </view>
        </view>
      </view>

      <!-- 附件信息 -->
      <view class="common-section-card mt20" v-if="state.form.fileList?.length">
        <view class="section-title">附件资料</view>
        <view class="common-file-list">
          <view class="file-item" v-for="(file, index) in state.form.fileList" :key="index" @click="previewFile(file.fileUrl, file.fileName)">
            <view class="file-info">
              <text class="f-name">{{ file.fileName }}</text>
              <view class="f-meta">
                <text class="f-type">{{ file.fileType || '附件' }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { reactive, onMounted, watch, nextTick } from 'vue';
import { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
import { formatDate } from '@/utils/date';
import { formatAmount } from '@/utils/format';
import { previewFile } from '@/utils/file';
import to from 'await-to-js';

const props = defineProps<{
  code: string;
}>();

const { getDictLabel } = useDict('sci_pjt_level', 'sci_pjt_type', 'sci_academic_degree', 'sci_technical_title');
const documentApi = useDocumentApi();

const state = reactive({
  form: null as any,
  loading: false
});

const initForm = async (code: string) => {
  if (!code) return;
  state.loading = true;
  const [err, res] = await to(documentApi.getVerticalByCode(code));
  if (!err && res?.data) {
    await nextTick();
    state.form = res.data;
    // 数据标准化，确保列表字段不为 null
    state.form.fileList = state.form.fileList || [];
    state.form.fundsList = state.form.fundsList || [];
    state.form.memberList = state.form.memberList || [];
    state.form.unitsList = state.form.unitsList || [];
  }
  state.loading = false;
};

onMounted(() => {
  // 已经在 watch immediate 中调用，这里可以省略或保持一致
});

watch(() => props.code, (val) => {
  initForm(val);
}, { immediate: true });

defineExpose({
  initForm
});
</script>

<style lang="scss" scoped>
@import "./common.scss";

.member-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12rpx;
  
  .m-left {
    display: flex;
    align-items: center;
    gap: 12rpx;
  }
  
  .m-type-tag {
    font-size: 20rpx;
    padding: 2rpx 8rpx;
    background-color: #f0f7ff;
    color: #007aff;
    border-radius: 4rpx;
    border: 1px solid #d0e7ff;
  }
}

.m-line {
  display: flex;
  font-size: 26rpx;
  line-height: 44rpx;
  
  .l {
    color: #909399;
    flex-shrink: 0;
    width: 140rpx;
  }
  
  .v {
    color: #303133;
    word-break: break-all;
  }
}
</style>

