<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.batchName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">考核方案</text>
          <text class="value">{{ currentSchemeLabel || '-' }}</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.remark || '-' }}</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 { useDocumentApi } from '@/api/document';
import to from 'await-to-js';

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

const documentApi = useDocumentApi();
const form = ref<any>(null);
const schemeList = ref<any[]>([]);
const loading = ref(false);

const currentSchemeLabel = computed(() => {
  if (form.value?.schemeName) return form.value.schemeName;
  if (!form.value?.schemeId) return '';
  const item = schemeList.value.find(s => s.id === form.value.schemeId);
  return item ? item.schemeName : form.value.schemeId;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  
  // 1. 获取详情
  const [err, res] = await to(documentApi.getAssessmentBatchById(props.code));
  if (!err && res?.data) {
    form.value = res.data;
  }
  
  // 2. 获取方案列表（对齐PC logic, 用于翻译方案ID）
  const [sErr, sRes] = await to(documentApi.getAssessmentSchemeList());
  if (!sErr && sRes?.data?.list) {
    schemeList.value = sRes.data.list;
  }
  
  loading.value = false;
};

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

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

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