<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.applyName || '-' }}</text></view>
        <view class="info-row"><text class="label">申报单位</text><text class="value">{{ state.form.applyUnit || '-' }}</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>

      <!-- 附件信息 -->
      <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 { previewFile } from '@/utils/file';
import to from 'await-to-js';

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

const { getDictLabel } = useDict('sci_pjt_level', 'sci_pjt_type');
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.getDeclarationById(code));
  if (!err && res?.data) {
    await nextTick();
    const data = res.data.entity || res.data.data || res.data;
    state.form = data;
    
    // 兼容附件处理
    if (!state.form.fileList && state.form.weedFile) {
        state.form.fileList = [{
            fileType: '申报书',
            fileName: state.form.fileName || '附件',
            fileUrl: state.form.weedFile,
        }];
    }
    state.form.fileList = state.form.fileList || [];
  }
  state.loading = false;
};

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

defineExpose({
  initForm
});
</script>

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