<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.dutPersonnelName || form.memberName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">所属科室</text>
          <text class="value">{{ form.dutDeptName || form.deptName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">社会任职/兼职</text>
          <text class="value">{{ form.dutType === '10' ? '任职' : (form.dutType === '20' ? '兼职' : '-') }}</text>
        </view>
        <view class="info-row">
          <text class="label">社会任职机构名称</text>
          <text class="value">{{ form.dutCommitteeName || form.orgName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">任职级别</text>
          <text class="value">{{ getDictLabel('sci_duties_dutCommitteeLevel', form.dutCommitteeLevel) || getDictLabel('sci_duty_level', form.dutyLevel) || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">任职类型</text>
          <text class="value">{{ getDictLabel('sci_duties_dutCommitteeType', form.dutCommitteeType) || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">发证单位</text>
          <text class="value">{{ form.dutOrganizer || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">职务</text>
          <text class="value">{{ form.dutDuties || form.positionName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">聘期</text>
          <text class="value">{{ (form.dutStartDate || form.startDate) || '-' }} 至 {{ (form.dutEndDate || form.endDate) || '至今' }}</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.fileList?.length">
        <view class="section-title">证明材料</view>
        <view class="common-file-list">
          <view class="file-item" v-for="(file, index) in form.fileList" :key="index" @click="previewFile(file.fileUrl, file.fileName)">
            <view class="file-info">
              <text class="f-name">{{ file.fileName }}</text>
              <view class="f-meta">
                <text class="f-type">附件</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 to from 'await-to-js';

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>
