<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.standardName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">标准编号</text>
          <text class="value">{{ form.standardCode || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">标准类别</text>
          <text class="value">{{ getDictLabel('sci_standard_type', form.standardType) }}</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.submitTime, 'YYYY-MM-DD') }}</text>
        </view>
        <view class="info-row">
          <text class="label">是否已发布</text>
          <text class="value">{{ form.publishStatus === '10' ? '是' : '否' }}</text>
        </view>
        <template v-if="form.publishStatus === '10'">
          <view class="info-row">
            <text class="label">发布部门</text>
            <text class="value">{{ form.publishDeptName || '-' }}</text>
          </view>
          <view class="info-row">
            <text class="label">发布时间</text>
            <text class="value">{{ formatDate(form.publishTime, 'YYYY-MM-DD') }}</text>
          </view>
        </template>
        <view class="info-row">
          <text class="label">所属行业</text>
          <text class="value">{{ form.standardProfession || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">标准属性</text>
          <text class="value">{{ getDictLabel('sci_standard_property', form.standardProperty) }}</text>
        </view>
        <view class="info-row house">
          <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_standard_type',
  'sci_standard_property',
  '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.getStandardByCode(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>
