<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.disciplineTitle || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">学科编号</text>
          <text class="value">{{ form.disciplineCode || '-' }}</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">{{ getDictLabel('sci_pjt_level', form.declarationLevel) }}</text>
        </view>
        <view class="info-row">
          <text class="label">申报类别</text>
          <text class="value">{{ getDictLabel('sci_pjt_type', form.declarationType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">开始日期</text>
          <text class="value">{{ formatDate(form.startDate, 'YYYY-MM-DD') }}</text>
        </view>
        <view class="info-row">
          <text class="label">完成日期</text>
          <text class="value">{{ formatDate(form.endDate, 'YYYY-MM-DD') }}</text>
        </view>
        <view class="info-row">
          <text class="label">研究方向</text>
          <text class="value">{{ form.researchDirection || '-' }}</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.disciplineTitle || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">终验时间</text>
          <text class="value">{{ formatDate(form.finalDate, 'YYYY-MM-DD HH:mm') }}</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.finalReport">
        <view class="section-title">附件资料</view>
        <view class="common-file-list">
          <view class="file-item" @click="previewFile(form.finalReport, '终验报告')">
            <view class="file-info">
              <uv-icon name="file-text" size="40" color="#1c9bfd"></uv-icon>
              <view class="f-content">
                <text class="f-name">终验报告</text>
                <text class="f-meta">点击预览附件</text>
              </view>
            </view>
            <uv-icon name="arrow-right" size="32" color="#ccc"></uv-icon>
          </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 { formatDate } from '@/utils/date';
import to from 'await-to-js';

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.getDisciplineFinalByCode(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>
