<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.projectSource || '-' }}</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.awardDate || '-' }}</text></view>
        <view class="info-row"><text class="label">奖项所属年度</text><text class="value">{{ form.awardYear || '-' }}</text></view>
        <view class="info-row">
          <text class="label">获奖类别</text>
          <text class="value">{{ form.awardType === '10' ? '教学成果' : '科研奖项' }}</text>
        </view>
        <view class="info-row">
          <text class="label">获奖等级</text>
          <text class="value">{{ getDictLabel('sci_awards_grade', form.awardGrade) }}</text>
        </view>
        <view class="info-row">
          <text class="label">获奖级别</text>
          <text class="value">{{ getDictLabel('sci_awards_level', form.awardLevel) }}</text>
        </view>
        <view class="info-row"><text class="label">获奖人（备案人）</text><text class="value">{{ showMembers(form.memberList) }}</text></view>
        <view class="info-row"><text class="label">颁奖机构</text><text class="value">{{ form.awardIssueAuthority || '-' }}</text></view>
        <view class="info-row"><text class="label">完成单位</text><text class="value">{{ form.completionUnit || '-' }}</text></view>
        <view class="info-row">
          <text class="label">获奖金额(万元)</text>
          <text class="value primary-color">{{ form.awardAmount || 0 }}</text>
        </view>
        <view class="info-row column">
          <text class="label">全部获奖完成人（按获奖顺序排列）</text>
          <text class="value remark">{{ form.remark || '-' }}</text>
        </view>
      </view>

      <!-- 附件信息 -->
      <AttachmentList :list="mergedFileList" title="电子附件" />
    </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';
import AttachmentList from './AttachmentList.vue';

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

const { getDictLabel } = useDict('sci_awards_grade', 'sci_awards_level');
const documentApi = useDocumentApi();

const form = ref<any>(null);
const loading = ref(false);

const mergedFileList = computed(() => {
  if (!form.value?.awardCertificate) return [];
  try {
    const list = JSON.parse(form.value.awardCertificate);
    if (Array.isArray(list)) {
      const types = ['获奖批文', '获奖证书', '获奖金额证明'];
      return list.map((item, index) => ({
        ...item,
        fileType: types[index] || '其他附件'
      }));
    }
    return [];
  } catch (e) {
    return [];
  }
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const awardCode = props.code.includes('-') ? props.code.split('-')[1] : props.code;
  const [err, res] = await to(documentApi.getAwardsByCode(awardCode));
  if (!err && res?.data) {
    form.value = res.data;
  }
  loading.value = false;
};

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

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

const showMembers = (list: any[]) => {
  return list?.map(m => m.memberName).join(', ') || '-';
};
</script>

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