<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.achievementName || '-' }}</text></view>
        <view class="info-row"><text class="label">申请人</text><text class="value">{{ form.applicant || '-' }}</text></view>
        <view class="info-row"><text class="label">所属单位</text><text class="value">{{ form.organization || '-' }}</text></view>
        <view class="info-row">
          <text class="label">成果类型</text>
          <text class="value">{{ getDictLabel('sci_achievement_other_type', form.achievementType) }}</text>
        </view>
        <view class="info-row"><text class="label">获得日期</text><text class="value">{{ form.acquireTime ? formatDate(form.acquireTime, 'YYYY-MM-DD') : '-' }}</text></view>
        <view class="info-row">
          <text class="label">成果归属单位</text>
          <text class="value">{{ getDictLabel('achievement_owner_unit', form.achievementOwnerUnit) || form.achievementOwnerUnit || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">备注</text>
          <text class="value remark">{{ form.remark || '-' }}</text>
        </view>
      </view>

      <!-- 附件 -->
      <AttachmentList :file="attachment" 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';

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