<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="getDictLabel('disType', form.disType)" />
        <CommonInfoRow label="姓名" :value="form.reserName" />
        <CommonInfoRow label="性别" :value="form.reserGender === '10' ? '男' : (form.reserGender === '20' ? '女' : '-')" />
        <CommonInfoRow :label="form.disType == '10' ? '派遣单位' : '接收单位'" :value="form.disAddress" />
        <CommonInfoRow label="手机号" :value="form.reserPhone" />
        <CommonInfoRow label="电子邮箱" :value="form.reserEmail" />
        <CommonInfoRow label="派遣时间" :value="formatDate(form.disStartTime) + ' 至 ' + formatDate(form.disEndTime)" />
        <CommonInfoRow label="项目名称" :value="form.projectName" />
        <CommonInfoRow label="工作内容" :value="form.disContent" isColumn />
      </CommonSection>

      <!-- 附件 -->
      <AttachmentList :file="attachment" title="佐证材料" />
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed } 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('disType');
const documentApi = useDocumentApi();
const form = ref<any>(null);
const loading = ref(false);

const attachment = computed(() => {
  if (form.value?.disFile) {
    try {
      const file = JSON.parse(form.value.disFile);
      return {
        ...file,
        type: '佐证材料'
      };
    } catch (e) {
      return null;
    }
  }
  return null;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getDispatchById(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>
