<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.workName || '-' }}</text></view>
        <view class="info-row">
          <text class="label">著作类别</text>
          <text class="value">{{ getDictLabel('sci_work_class', form.workClass) }}</text>
        </view>
        <view class="info-row"><text class="label">出版单位</text><text class="value">{{ form.workPublisher || '-' }}</text></view>
        <view class="info-row">
          <text class="label">出版社类型</text>
          <text class="value">{{ getDictLabel('sci_work_publisherType', form.workPublisherType) }}</text>
        </view>
        <view class="info-row"><text class="label">出版时间</text><text class="value">{{ form.workPublicationDate || '-' }}</text></view>
        <view class="info-row"><text class="label">所属科室</text><text class="value">{{ form.deptName || '-' }}</text></view>
        <view class="info-row"><text class="label">CIP号</text><text class="value">{{ form.cip || '-' }}</text></view>
        <view class="info-row"><text class="label">字数</text><text class="value">{{ form.wordsNum || '-' }}</text></view>
        <view class="info-row"><text class="label">所属年份</text><text class="value">{{ form.statisticalYear || '-' }}</text></view>
        <view class="info-row">
          <text class="label">是否国家规范化教材</text>
          <text class="value">{{ form.isStandard === '10' ? '是' : '否' }}</text>
        </view>
        <view class="info-row">
          <text class="label">是否翻译著作</text>
          <text class="value">{{ form.isTranslation === '10' ? '是' : '否' }}</text>
        </view>
      </view>

      <!-- 标注经济来源 -->
      <view class="common-section-card mt20" v-if="form.projList?.length">
        <view class="section-title">标注经济来源</view>
        <view class="achievement-card" v-for="(row, index) in form.projList" :key="index">
          <view class="a-row">
            <text class="al">关联类型：</text>
            <text class="av">{{ row.sourceType === '10' ? '项目' : '学科' }}</text>
          </view>
          <view class="a-row">
            <text class="al">关联对象：</text>
            <text class="av">{{ row.projectSource || '-' }}</text>
          </view>
        </view>
      </view>

      <!-- 著作人 -->
      <view class="common-section-card mt20" v-if="form.memberList?.length">
        <view class="section-title">著作人信息</view>
        <view class="member-list">
          <view class="member-item" v-for="(row, index) in form.memberList" :key="index">
            <view class="member-header">
              <text class="m-name">{{ row.memberName }}</text>
              <view class="name-box">
                <text class="m-tag" v-if="row.roleContent">{{ getDictLabel('sci_work_roleContent', row.roleContent) }}</text>
                <text class="m-tag" v-if="row.position">第{{ row.position }}位</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>
      </view>

      <!-- 附件 -->
      <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 to from 'await-to-js';
import AttachmentList from './AttachmentList.vue';

const props = defineProps<{
  code: string;
}>();

const { getDictLabel } = useDict('sci_work_class', 'sci_work_publisherType', 'sci_work_roleContent');
const documentApi = useDocumentApi();

const form = ref<any>(null);
const loading = ref(false);

const mergedFileList = computed(() => {
  if (!form.value?.workFile) return [];
  try {
    const file = JSON.parse(form.value.workFile);
    return [{ ...file, fileType: '著作附件' }];
  } catch (e) {
    return [];
  }
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const workCode = props.code.includes('-') ? props.code.split('-')[1] : props.code;
  const [err, res] = await to(documentApi.getWorkByCode(workCode));
  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>
