<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.softwareName || '-' }}</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">{{ form.version || '-' }}</text></view>
        <view class="info-row"><text class="label">开发完成日期</text><text class="value">{{ form.developmentFinishDate ? formatDate(form.developmentFinishDate, 'YYYY-MM-DD') : '-' }}</text></view>
        <view class="info-row"><text class="label">首次发表日期</text><text class="value">{{ form.firstPublishDate ? formatDate(form.firstPublishDate, '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">
          <text class="label">权利范围</text>
          <text class="value">{{ getDictLabel('rights_scope', form.rightsScope) || form.rightsScope || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">权利取得方式</text>
          <text class="value">{{ getDictLabel('rights_acquire_method', form.rightsAcquireMethod) || form.rightsAcquireMethod || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">开发方式</text>
          <text class="value">{{ getDictLabel('development_method', form.developmentMethod) || form.developmentMethod || '-' }}</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('achievement_owner_unit', 'rights_scope', 'rights_acquire_method', 'development_method');
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.getSoftwareAchievementById(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>
