<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.varietyName || '-' }}</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">{{ form.varietyCode || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">审订单位</text>
          <text class="value">{{ form.ratifyDept || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">审订时间</text>
          <text class="value">{{ formatDate(form.ratifyTime, 'YYYY-MM-DD') }}</text>
        </view>
        <view class="info-row">
          <text class="label">审订类别</text>
          <text class="value">{{ getDictLabel('sci_variety_type', form.varietyType) }}</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>

      <!-- 附件信息 -->
      <AttachmentList 
        v-if="form.fileName" 
        :file="{ fileName: form.fileName, fileUrl: form.fileUrl }" 
        title="审批附件" 
      />
    </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 { formatDate } from '@/utils/date';
import to from 'await-to-js';
import AttachmentList from './AttachmentList.vue';

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

const { getDictLabel } = useDict(
  'sci_variety_type',
  '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.getVarietyByCode(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>
