<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载奖项荣誉详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 基本信息 -->
      <CommonSection title="基本信息" :isFirst="true">
        <CommonInfoRow label="获奖项目名称" :value="form.projectSource" />
        <CommonInfoRow label="所在科室" :value="form.deptName" />
        <CommonInfoRow label="批文年月日" :value="formatDate(form.awardDate)" />
        <CommonInfoRow label="奖项所属年度" :value="form.awardYear" />
        <CommonInfoRow label="获奖类别" :value="form.awardType === '10' ? '教学成果' : '科研奖项'" />
        <CommonInfoRow label="获奖等级" :value="getDictLabel('sci_awards_grade', form.awardGrade)" />
        <CommonInfoRow label="获奖级别" :value="getDictLabel('sci_awards_level', form.awardLevel)" />
        <CommonInfoRow label="获奖人（备案人）" :value="showMembers(form.memberList)" />
        <CommonInfoRow label="颁奖机构" :value="form.awardIssueAuthority" />
        <CommonInfoRow label="完成单位" :value="form.completionUnit" />
        <CommonInfoRow label="获奖金额(万元)" :value="form.awardAmount" isAmount />
        <CommonInfoRow label="所属平台">
          <view class="platform-tags">
            <template v-if="platformList.length > 0">
              <uv-tags v-for="(p, index) in platformList" :key="index" :text="p.platformName === '其他' ? '其他' : (p.platformType ? p.platformName + ' (' + p.platformType + ')' : p.platformName)" type="primary" plain size="mini" class="mr5"></uv-tags>
            </template>
            <text v-else>-</text>
          </view>
        </CommonInfoRow>
        <CommonInfoRow label="所属团队" :value="form.belongTeam || '-'" />
        <CommonInfoRow label="全部获奖完成人" :value="form.remark" isColumn />
      </CommonSection>

      <!-- 附件信息 -->
      <AttachmentList :list="mergedFileList" title="电子附件" />

      <!-- 审批记录 -->
      <CommonSection title="审批记录" v-if="form.id">
        <FlowTable :id="form.id" :businessCode="'奖项荣誉-' + String(form.awardCode)" defCode="sci_academic_achievement" />
      </CommonSection>
    </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 { formatDate } from '@/utils/date';
import to from 'await-to-js';
import AttachmentList from './AttachmentList.vue';
import FlowTable from '@/pages/project/components/detail/FlowTable.vue';
import CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.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 platformList = computed(() => {
  if (form.value?.belongPlatform) {
    try {
      const data = JSON.parse(form.value.belongPlatform);
      return Array.isArray(data) ? data : [];
    } catch (e) {
      if (form.value.belongPlatform === '其他') return [{ platformName: '其他' }];
      return [{ platformName: form.value.belongPlatform }];
    }
  }
  return [];
});

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";

.platform-tags {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8rpx;
  
  .platform-tag {
    font-size: 20rpx;
    padding: 2rpx 12rpx;
    background-color: #f6f8fa;
    color: #64748b;
    border-radius: 4rpx;
    border: 1rpx solid #e2e8f0;
  }
}
</style>
