<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.achievementName" />
        <CommonInfoRow label="申请人" :value="form.applicant" />
        <CommonInfoRow label="所属单位" :value="form.organization" />
        <CommonInfoRow label="成果类型" :value="getDictLabel('sci_achievement_other_type', form.achievementType)" />
        <CommonInfoRow label="获得日期" :value="form.acquireTime ? formatDate(form.acquireTime, 'YYYY-MM-DD') : '-'" />
        <CommonInfoRow label="成果归属单位" :value="getDictLabel('achievement_owner_unit', form.achievementOwnerUnit) || form.achievementOwnerUnit || '-'" />
        <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>
      <!-- 附件 -->
      <AttachmentList :file="attachment" title="佐证附件" />

      <!-- 审批记录 -->
      <CommonSection title="审批记录" v-if="form.id">
        <FlowTable :id="form.id" :businessCode="String(form.id)" defCode="sci_achievement_other" />
      </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_achievement_other_type', 'achievement_owner_unit');
const documentApi = useDocumentApi();
const form = ref<any>(null);
const loading = ref(false);

const attachment = computed(() => {
  if (form.value?.fileUrl) {
    return {
       fileName: form.value.fileName || '佐证附件',
       fileUrl: form.value.fileUrl
    };
  }
  return null;
});

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 fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getOtherAchievementById(props.code));
  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";


</style>
