<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.projectSource || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">计划日期</text>
          <text class="value">{{ formatDate(form.startDate, 'YYYY-MM-DD') }} 至 {{ formatDate(form.endDate, 'YYYY-MM-DD') }}</text>
        </view>
        <view class="info-row">
          <text class="label">研究类型</text>
          <text class="value">{{ getDictLabel('sci_pjt_type', form.studyType) }}</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.statisticalYear || '-' }}</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.projectLeaderMail || '-' }}</text>
        </view>
        <view class="info-row" v-if="form.isEthics === '10'">
          <text class="label">伦理时间</text>
          <text class="value">{{ form.ethicsTime || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">研究内容</text>
          <text class="value remark">{{ form.researchSection || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">研究方向</text>
          <text class="value remark">{{ form.researchDirection || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">预期成果</text>
          <text class="value remark">{{ form.expectedResults || '-' }}</text>
        </view>
      </view>

      <!-- 附件信息 -->
      <AttachmentList :list="form.fileList" />
    </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 { formatDate } from '@/utils/date';
import to from 'await-to-js';
import AttachmentList from './AttachmentList.vue';

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

const { getDictLabel } = useDict('sci_pjt_level', 'sci_pjt_type');
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.getSpontaneityPlanByCode(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";

.file-list {
  .file-item {
    padding: 20rpx;
    background: #f8f9fc;
    border-radius: 12rpx;
    margin-bottom: 16rpx;
    .m-header {
      margin-bottom: 8rpx;
      font-size: 24rpx;
      .m-type { color: #888; }
      .m-required { color: #ff4d4f; margin-left: 5rpx; }
    }
    .m-name {
      font-size: 28rpx;
      color: var(--primary-color);
      word-break: break-all;
    }
  }
}
</style>
