<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.awardCode || '-'" />
        <CommonInfoRow label="获奖项目名称" :value="form.projectSource || '-'" />
        <CommonInfoRow label="获奖类别"
          :value="form.awardType === '10' ? '教学成果' : form.awardType === '20' ? '科研奖项' : '-'" />
        <CommonInfoRow label="获奖等级" :value="getDictLabel('sci_awards_grade', form.awardGrade)" />
        <CommonInfoRow label="获奖级别" :value="getDictLabel('sci_awards_level', form.awardLevel)" />
        <CommonInfoRow label="奖项所属年度" :value="form.awardYear || '-'" />
        <CommonInfoRow label="批文年月日" :value="formatDate(form.awardDate)" />
        <CommonInfoRow label="完成单位" :value="form.completionUnit || '-'" />
        <CommonInfoRow label="颁奖机构" :value="form.awardIssueAuthority || '-'" />
        <CommonInfoRow label="获奖金额(万元)" :value="form.awardAmount || '-'" />
        <CommonInfoRow label="所属平台" isColumn>
          <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>

      <!-- 获奖人信息 -->
      <CommonSection title="获奖人信息">
        <view class="member-list" v-if="form.memberList?.length">
          <view class="member-item" v-for="(row, index) in form.memberList" :key="index">
            <view class="member-header">
              <view class="name-box">
                <text class="m-name">{{ row.memberName }}</text>
                <text class="m-tag">{{ row.memberType === '10' ? '负责人' : '成员' }}</text>
              </view>
            </view>
            <view class="m-body">
              <view class="m-line" v-if="row.deptName"><text class="l">所属部门：</text><text class="v">{{ row.deptName
                  }}</text>
              </view>
            </view>
          </view>
        </view>
        <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
      </CommonSection>

      <!-- 附件信息 -->
      <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 { formatDate } from '@/utils/date';
import to from 'await-to-js';
import AttachmentList from './AttachmentList.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?.fileList?.length) return [];
  return form.value.fileList.map((item: any) => ({
    fileName: item.name,
    fileUrl: item.url,
    fileType: '附件'
  }));
});

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();
});
</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>
