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

      <!-- 附件 -->
      <view class="common-section-card mt20" v-if="attachment">
        <view class="section-title">佐证材料</view>
        <view class="common-file-list">
          <view class="file-item" @click="previewFile(attachment.url, attachment.name)">
            <view class="file-info">
              <text class="f-name">{{ attachment.name }}</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, computed } 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('disType');
const documentApi = useDocumentApi();
const form = ref<any>(null);
const loading = ref(false);

const attachment = computed(() => {
  if (form.value?.disFile) {
    try {
      return JSON.parse(form.value.disFile);
    } 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>
