<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.dutPersonnelName || form.memberName" />
        <CommonInfoRow label="所属科室" :value="form.dutDeptName || form.deptName" />
        <CommonInfoRow label="社会任职/兼职" :value="form.dutType === '10' ? '任职' : (form.dutType === '20' ? '兼职' : '-')" />
        <CommonInfoRow label="社会任职机构名称" :value="form.dutCommitteeName || form.orgName" />
        <CommonInfoRow label="任职级别" :value="getDictLabel('sci_duties_dutCommitteeLevel', form.dutCommitteeLevel) || getDictLabel('sci_duty_level', form.dutyLevel)" />
        <CommonInfoRow label="任职类型" :value="getDictLabel('sci_duties_dutCommitteeType', form.dutCommitteeType)" />
        <CommonInfoRow label="发证单位" :value="form.dutOrganizer" />
        <CommonInfoRow label="职务" :value="form.dutDuties || form.positionName" />
        <CommonInfoRow label="聘期" :value="formatDate(form.dutStartDate || form.startDate) + ' 至 ' + formatDate(form.dutEndDate || form.endDate)" />
        <CommonInfoRow label="备注" :value="form.remark" isColumn />
      </CommonSection>

      <!-- 附件 -->
      <AttachmentList :list="form.fileList" 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 CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.vue';
import AttachmentList from './AttachmentList.vue';

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

const { getDictLabel } = useDict('sci_duties_dutCommitteeLevel', 'sci_duties_dutCommitteeType', 'sci_duty_level');
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.getDutiesById(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>
