<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.projectName }}</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.isClinicalTrial === '10' ? '临床试验' : '横向其他' }}</text>
        </view>
        <view class="info-row"><text class="label">项目来源</text><text class="value">{{ form.projectSource || '-' }}</text></view>
        <view class="info-row">
          <text class="label">项目{{ form.isClinicalTrial === '10' ? '级别' : '类别' }}</text>
          <text class="value">{{ getDictLabel(form.isClinicalTrial === '10' ? 'sci_proj_type' : 'sci_pjt_class', form.projectClass) }}</text>
        </view>
        <view class="info-row"><text class="label">立项单位</text><text class="value">{{ form.deptName }}</text></view>
        <view class="info-row"><text class="label">主要研究者</text><text class="value">{{ form.projectLeaderName }}</text></view>
        <view class="info-row"><text class="label">负责人电话</text><text class="value">{{ form.projectLeaderPhone || '-' }}</text></view>
        <view class="info-row"><text class="label">项目时间</text><text class="value">{{ form.startDate }} 至 {{ form.endDate }}</text></view>
        <view class="info-row"><text class="label">签订时间</text><text class="value">{{ form.signDate || '-' }}</text></view>
        <view class="info-row">
          <text class="label">经费(元)</text>
          <text class="value primary-color">￥{{ form.contractFunds || 0 }}</text>
        </view>
        <view class="info-row" v-if="form.isClinicalTrial === '10'"><text class="label">招募人数</text><text class="value">{{ form.memberNum || 0 }}</text></view>
      </view>

      <!-- 三方信息 -->
      <view class="common-section-card mt20">
        <view class="section-title">三方信息</view>
        <view class="info-row"><text class="label">甲方名称</text><text class="value">{{ form.firstPartyName || '-' }}</text></view>
        <view class="info-row"><text class="label">甲方负责人</text><text class="value">{{ form.firstPartyBehalf || '-' }}</text></view>
        <view class="info-row"><text class="label">乙方名称</text><text class="value">{{ form.secondPartyName || '-' }}</text></view>
        <view class="info-row"><text class="label">乙方负责人</text><text class="value">{{ form.secondPartyBehalf || '-' }}</text></view>
        <view class="info-row" v-if="form.thirdPartyName"><text class="label">丙方名称</text><text class="value">{{ form.thirdPartyName }}</text></view>
      </view>

      <!-- 成员信息 -->
      <view class="common-section-card mt20" v-if="form.member?.length">
        <view class="section-title">成员信息</view>
        <view class="member-list">
          <view class="member-item" v-for="(row, index) in form.member" :key="index">
            <view class="member-header">
              <text class="m-name">{{ row.memberName }}</text>
              <text class="m-tag">{{ row.projectRole === '10' ? '负责人' : '参与人' }}</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.technicalTitle"><text class="l">职称：</text><text class="v">{{ row.technicalTitle }}</text></view>
              <view class="m-line" v-if="row.degree"><text class="l">学位：</text><text class="v">{{ getDictLabel('sci_academic_degree', row.degree) }}</text></view>
            </view>
          </view>
        </view>
      </view>

      <!-- 附件信息 -->
      <view class="common-section-card mt20" v-if="form.files?.length">
        <view class="section-title">附件资料</view>
        <view class="common-file-list">
          <view class="file-item" v-for="(file, index) in form.files" :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 { ref, onMounted, watch } from 'vue';
import { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
import { previewFile } from '@/utils/file';
import to from 'await-to-js';

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

const { getDictLabel } = useDict('sci_proj_type', 'sci_pjt_class', 'sci_academic_degree');
const documentApi = useDocumentApi();

const form = ref<any>(null);
const loading = ref(false);

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