<template>
  <a-modal v-model:visible="props.visible" :title="dialogTitle" width="600px" @cancel="dialogClose">
    <a-form ref="formRef" :model="formData" :label-col="{ style: { width: '90px' } }">
      <a-form-item
        label="模板名称"
        name="name"
        :rules="{ required: true, message: '请输入模板名称', trigger: 'blur' }"
      >
        <a-input
          :maxlength="50"
          placeholder="请输入模板名称"
          allow-clear
          v-model:value="formData.name"
        />
      </a-form-item>
      <a-form-item
        label="模板code"
        name="code"
        :rules="{ required: true, message: '请输入模板code', trigger: 'blur' }"
      >
        <a-input
          v-if="!formData.id"
          :maxlength="100"
          placeholder="请输入模板code"
          allow-clear
          v-model:value="formData.code"
        />
        <span v-else>{{ formData.code }}</span>
      </a-form-item>
      <a-form-item
        label="模板文件"
        name="filePath"
        :rules="{ required: true, message: '请上传模板文件', trigger: 'blur' }"
      >
        <UploadFile
          @upload="fileUpload"
          file-type=".xlsx.xls.doc.docx"
          name="file"
          :file-list="
            formData.filePath ? [{ name: formData.fileName, url: formData.filePath }] : []
          "
        />
      </a-form-item>
    </a-form>
    <template #footer>
      <span class="dialog-footer">
        <a-button @click="dialogClose">取消</a-button>
        <a-button :loading="subLoading" type="primary" @click="submit"> 确定 </a-button>
      </span>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import {
    fileTemplateAdd,
    fileTemplateUpdate,
    getFileTemplateDetail,
  } from '@/api/file/fileTemplate';
  import { computed, onMounted, shallowRef, reactive } from 'vue';
  import type { FormInstance } from 'ant-design-vue';
  import { message } from 'ant-design-vue';
  import { useLockFn } from '@/utils/useLockFn';
  import UploadFile from '@/components/Upload/file.vue';

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    id: {
      type: Number,
      required: true,
      default: 0,
    },
  });

  const emit = defineEmits(['success', 'update:visible']);

  /**
   * 定义弹窗标题
   */
  const dialogTitle = computed(() => {
    return props.id ? '编辑模板文件' : '新增模板文件';
  });

  const formRef = shallowRef<FormInstance>();

  /**
   * 定义表单参数
   */
  const formData = reactive({
    id: '',
    name: '',
    code: '',
    filePath: '',
    fileName: '',
  });

  /**
   * 文件上传
   * @param filePath 文件路径
   * @param fileName 文件名称
   */
  const fileUpload = async (filePath: any, fileName: any) => {
    formData.filePath = filePath;
    formData.fileName = fileName;
    await formRef.value?.validateFields('filePath');
  };

  /**
   * 关闭窗体
   */
  const dialogClose = () => {
    emit('update:visible', false);
  };

  /**
   * 设置表单数据
   * @param id 参数
   */
  const setFormData = async (id: any) => {
    const data = await getFileTemplateDetail(id);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        formData[key] = data[key];
      }
    }
  };

  /**
   * 执行提交表单
   */
  const handleSubmit = async () => {
    await formRef.value?.validate();
    props.id ? await fileTemplateUpdate(formData) : await fileTemplateAdd(formData);
    message.success('操作成功');
    emit('update:visible', false);
    emit('success');
  };

  const { isLock: subLoading, lockFn: submit } = useLockFn(handleSubmit);

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.id) {
      setFormData(props.id);
    }
  });
</script>
