<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" :text="'正在加载' + title + '详情...'"></uv-loading-icon>
    <template v-else-if="form">
      <view class="common-section-card">
        <view class="section-title">{{ 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.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.applyDate }}</text></view>
        <view class="info-row"><text class="label">审核年份</text><text class="value">{{ form.auditYear }}</text></view>
        <view class="info-row"><text class="label">进度说明</text><text class="value">{{ form.progressDesc || '-' }}</text></view>
        <view class="info-row"><text class="label">备注</text><text class="value">{{ form.remark || '-' }}</text></view>
      </view>

      <!-- 附件 -->
      <view class="common-section-card mt20" v-if="form.fileList?.length">
        <view class="section-title">相关附件</view>
        <view class="common-file-list">
          <view class="file-item" v-for="(file, index) in 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">附件</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue';
import { useDocumentApi } from '@/api/document';
import { previewFile } from '@/utils/file';
import to from 'await-to-js';

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

const title = computed(() => props.defCode === 'sci_project_inspection' ? '中期检查' : '结项审核');
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.getProjectAuditById(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>
