<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">{{ getDictLabel('sci_pjt_level', form.declarationLevel) }}</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_type', form.declarationType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">开始日期</text>
          <text class="value">{{ form.startDate || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">完成日期</text>
          <text class="value">{{ form.endDate || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">研究方向</text>
          <text class="value">{{ form.researchDirection || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">评分模板</text>
          <text class="value">{{ currentTemplateName || '-' }}</text>
        </view>
      </view>
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed } from 'vue';
import { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
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 tmplOptions = ref<any[]>([]);
const loading = ref(false);

const currentTemplateName = computed(() => {
  if (form.value?.gradeTemplateName) return form.value.gradeTemplateName;
  if (!form.value?.gradeTemplateId) return '';
  const item = tmplOptions.value.find(t => t.id === form.value.gradeTemplateId);
  return item ? item.name : form.value.gradeTemplateId;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  
  // 1. 获取学科详情
  const [err, res] = await to(documentApi.getDisciplineByCode(props.code));
  if (!err && res?.data) {
    form.value = res.data;
  }
  
  // 2. 获取评价模板列表（用于翻译ID）
  const [tErr, tRes] = await to(documentApi.getDisciplineTmplList());
  if (!tErr && tRes?.data?.list) {
    tmplOptions.value = tRes.data.list;
  }
  
  loading.value = false;
};

onMounted(() => {
  fetchData();
});

watch(() => props.code, () => {
  fetchData();
});
</script>

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