<template>
  <view class="document-form">
    <uv-loading-icon v-if="state.loading" mode="circle" text="正在加载项目计划详情..."></uv-loading-icon>
    <template v-else-if="state.form">
      <!-- 基本信息 -->
      <CommonSection title="基本信息" :isFirst="true">
        <CommonInfoRow label="项目名称" :value="state.form.projectName" />
        <CommonInfoRow label="项目级别" :value="getDictLabel('sci_pjt_level', state.form.projectLevel)" />
        <CommonInfoRow label="项目分类" :value="state.form.projectClazzName" />
        <CommonInfoRow label="项目来源" :value="state.form.projectSource || '-'" />
        <CommonInfoRow label="计划日期" :value="formatDateRange(state.form.planStartDate, state.form.planEndDate)" />
        <CommonInfoRow label="研究类型" :value="getDictLabel('sci_pjt_type', state.form.studyType)" />
        <CommonInfoRow label="所属科室" :value="state.form.deptName" />
        <CommonInfoRow label="项目负责人" :value="state.form.projectLeaderName" />
        <CommonInfoRow label="负责人电话" :value="state.form.projectLeaderPhone || '-'" />
        <CommonInfoRow label="负责人邮箱" :value="state.form.projectLeaderMail || '-'" />
        <CommonInfoRow label="所属平台">
          <view class="platform-tags">
            <template v-if="platformList.length > 0">
              <text v-for="(p, index) in platformList" :key="index" class="platform-tag">
                {{ p.platformName }}
              </text>
            </template>
            <text v-else>-</text>
          </view>
        </CommonInfoRow>
        <CommonInfoRow label="备注" :value="state.form.remark || '-'" isColumn />
      </CommonSection>

      <!-- 成员信息 -->
      <CommonSection title="成员信息" v-if="state.form.memberList?.length">
        <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" :class="{ 'leader': row.projectRole === '10' }">
                {{ 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>
      </CommonSection>

      <!-- 预算信息 -->
      <CommonSection title="预算信息" v-if="state.form.fundsList?.length">
        <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>
      </CommonSection>

      <!-- 附件信息 -->
      <AttachmentList :list="state.form.fileList" title="附件资料" />
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { reactive, onMounted, watch, nextTick, computed } from 'vue';
import { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
import { formatDate } from '@/utils/date';
import { formatAmount } from '@/utils/format';
import AttachmentList from './AttachmentList.vue';
import CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.vue';
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 platformList = computed(() => {
  if (state.form?.belongPlatform) {
    try {
      const data = JSON.parse(state.form.belongPlatform);
      return Array.isArray(data) ? data : [];
    } catch (e) {
      if (state.form.belongPlatform === '其他') return [{ platformName: '其他' }];
      return [{ platformName: state.form.belongPlatform }];
    }
  }
  return [];
});

const formatDateRange = (start: string, end: string) => {
  if (!start && !end) return '-';
  return `${formatDate(start)} 至 ${formatDate(end)}`;
};

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;
    // 数据标准化
    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;
};

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

defineExpose({
  initForm
});
</script>

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

.member-header {
  .leader {
    color: #f59e0b !important;
    background: #fff7e6 !important;
    border: 1rpx solid #ffd591 !important;
  }
}
</style>
