<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="40%">
    <BasicForm @register="registerForm" class="pt-3"> </BasicForm>
  </BasicModal>
</template>
<script setup lang="ts">
  import { ref } from 'vue';
  import { FormSchema } from '/@/components/Table';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';

  import { fileAdd, fileEdit } from '../api';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();

  const title = ref('上传文件');

  const BackFormSchema: FormSchema[] = [
    {
      label: 'pid',
      field: 'pid',
      component: 'ApiSelect',
      show: false,
    },
    {
      label: 'id',
      field: 'id',
      component: 'ApiSelect',
      show: false,
    },
    {
      label: '文件名称',
      field: 'name',
      component: 'Input',
    },
    {
      label: '文件类型',
      field: 'type',
      component: 'Select',
      required: true,
      componentProps: {
        options: [
          { value: '1', label: '法律法规' },
          { value: '2', label: '部门规章' },
          { value: '3', label: '技术规范' },
        ],
      },
    },
    {
      label: '文件大小',
      field: 'wjdx',
      component: 'Input',
      show: false,
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '文件日期',
      field: 'timestamp',
      component: 'Input',
      show: false,
      componentProps: {
        disabled: true,
      },
    },
    // {
    //   label: '文件后缀',
    //   field: 'hz',
    //   component: 'Input',
    //   componentProps: {
    //     disabled: true,
    //   },
    // },
    {
      label: '文件描述',
      field: 'remark',
      component: 'Input',
    },
    {
      label: '上传文件',
      field: 'file',
      required: true,
      component: 'JUpload',
      componentProps: () => {
        return {
          showUploadList: false,
          fileName: `上传文件`,
          // tipArr: ['提示：需上传PDF格式的已公开的正式红头文件，未公开的不能上传'],
          tipHtml: {
            preContentHtml: '<span style="color:red;font-size:12px;">提示：需上传PDF格式的已公开的正式红头文件，未公开的不能上传</span>',
          },
          accept: '.pdf',
          beforeUpload: (file) => {
            let isshow = false;
            const isFile = file.name.split('.')[file.name.split('.').length - 1];
            const name = file.name.split('.')[0];
            handUp(file, isFile, name);
            const arryType = ['pdf', 'PDF'];
            if (arryType.includes(isFile)) {
              isshow = true;
            } else {
              createMessage.error('只支持 pdf , PDF 格式的文件上传');
              isshow = false;
            }

            return isshow;
          },
        };
      },
    },
  ];
  // Emits声明
  const emit = defineEmits(['register', 'success']);

  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate, getFieldsValue }] = useForm({
    labelWidth: 120,
    schemas: BackFormSchema,
    // autoAdvancedCol: 2,
    // baseColProps: { span: 24 },
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data: any) => {
    console.log(data)
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter, centered: true });
    //表单赋值
    if (data.isEdit) {
      title.value = '编辑文件';
    } else {
      title.value = '上传文件';
    }
    await setFieldsValue({
      ...data.record,
    });
  });
  // 点击上传文件的赋值处理
  async function handUp(file, isFile, name) {
    const values = await getFieldsValue();
    if (!values.name) {
      values.name = name;
    }
    values.wjdx = file.size;
    values.hz = isFile;
    values.timestamp = file.timestamp;
    await setFieldsValue({
      ...values,
    });
  }
  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      if (title.value == '编辑文件') {
        await fileEdit(values);
      } else {
        await fileAdd(values);
      }

      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
