<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.copyrightCode || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">著作权名称</text>
          <text class="value">{{ form.copyrightName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">登记号</text>
          <text class="value">{{ form.registerCode || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">著作权类型</text>
          <text class="value">{{ getDictLabel('sci_copyright_type', form.copyrightType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">学科门类</text>
          <text class="value">{{ getDictLabel('subj_category', form.subjCategory) }}</text>
        </view>
        <view class="info-row">
          <text class="label">一级学科</text>
          <text class="value">{{ form.disciplineName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">所属单位</text>
          <text class="value">{{ form.deptName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">出版日期</text>
          <text class="value">{{ formatDate(form.publicationDate, 'YYYY-MM-DD') }}</text>
        </view>
        <view class="info-row" v-if="form.fileName">
          <text class="label">审批附件</text>
          <text class="value primary-color" @click="previewFile(form.fileUrl, form.fileName)">{{ form.fileName }}</text>
        </view>
        <view class="info-row">
          <text class="label">备注</text>
          <text class="value">{{ form.remark || '-' }}</text>
        </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="(member, index) in form.memberList" :key="index">
            <view class="member-header">
              <text class="m-name">{{ member.memberName }}</text>
              <text class="m-tag">署名顺序: {{ member.signOrder }}</text>
            </view>
            <view class="m-body">
              <view class="m-line">
                <text class="l">作者类型：</text>
                <text class="v">{{ getDictLabel('sci_paper_author_type', member.memberType) }}</text>
              </view>
              <view class="m-line"><text class="l">职工号/学号：</text><text class="v">{{ member.memberCode || '-' }}</text></view>
              <view class="m-line">
                <text class="l">性别：</text>
                <text class="v">{{ member.gender === '10' ? '男' : member.gender === '20' ? '女' : '-' }}</text>
              </view>
              <view class="m-line">
                <text class="l">学历：</text>
                <text class="v">{{ getDictLabel('sci_academic_degree', member.educated) }}</text>
              </view>
              <view class="m-line">
                <text class="l">职称：</text>
                <text class="v">{{ getDictLabel('sci_discipline_job_title', member.technicalTitle) }}</text>
              </view>
              <view class="m-line"><text class="l">所属单位/学校：</text><text class="v">{{ member.deptName || '-' }}</text></view>
              <view class="m-line"><text class="l">贡献率：</text><text class="v">{{ member.contributionRate || 0 }}%</text></view>
            </view>
          </view>
        </view>
      </view>
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
import { previewFile } from '@/utils/file';
import { formatDate } from '@/utils/date';
import to from 'await-to-js';

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

const { getDictLabel } = useDict(
  'sci_copyright_type',
  'subj_category',
  'sci_paper_author_type',
  'sci_academic_degree',
  'sci_discipline_job_title'
);
const documentApi = useDocumentApi();

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

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getCopyrightByCode(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";
.m-body {
  padding: 10rpx 0;
  .m-line {
    font-size: 26rpx;
    line-height: 1.6;
    display: flex;
    .l { color: #888; width: 180rpx; flex-shrink: 0; }
    .v { color: #333; }
  }
}
</style>
