<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.softwareName" />
        <CommonInfoRow label="著作权人" :value="form.applicant" />
        <CommonInfoRow label="所属单位" :value="form.organization" />
        <CommonInfoRow label="版本号" :value="form.version" />
        <CommonInfoRow label="开发完成日期" :value="form.developmentFinishDate ? formatDate(form.developmentFinishDate, 'YYYY-MM-DD') : '-'" />
        <CommonInfoRow label="首次发表日期" :value="form.firstPublishDate ? formatDate(form.firstPublishDate, 'YYYY-MM-DD') : '-'" />
        <CommonInfoRow label="成果归属单位" :value="getDictLabel('achievement_owner_unit', form.achievementOwnerUnit) || form.achievementOwnerUnit || '-'" />
        <CommonInfoRow label="权利范围" :value="getDictLabel('rights_scope', form.rightsScope) || form.rightsScope || '-'" />
        <CommonInfoRow label="权利取得方式" :value="getDictLabel('rights_acquire_method', form.rightsAcquireMethod) || form.rightsAcquireMethod || '-'" />
        <CommonInfoRow label="开发方式" :value="getDictLabel('development_method', form.developmentMethod) || form.developmentMethod || '-'" />
        
        <!-- 合作单位（仅开发方式为合作开发时显示） -->
        <CommonInfoRow v-if="form.developmentMethod === '20'" label="合作单位" :value="form.partnerUnit || '-'" />
        
        <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 :file="attachment" title="佐证附件" />

      <!-- 审批记录 -->
      <CommonSection title="审批记录" v-if="form.id">
        <FlowTable :id="form.id" :businessCode="String(form.id)" defCode="sci_achievement_software" />
      </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('achievement_owner_unit', 'rights_scope', 'rights_acquire_method', 'development_method');
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 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>
