<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="40%">
    <div class="pt-4" style="height: 400px"> <BasicForm @register="registerForm" /></div>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import { updateCzwc } from '../api';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  // Emits声明
  const emit = defineEmits(['register', 'success']);

  const AdviceFormSchema: FormSchema[] = [
    { label: '污染地块ID', field: 'wrdkid', component: 'Input', show: false },

    {
      label: '实际转运时间',
      field: 'sjzysj',
      required: true,
      component: 'Input',
      // component: 'DatePicker',
      componentProps: {
        // format: 'YYYY-MM-DD',
        // valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      label: '实际转运总量',
      field: 'sjzyzl',
      required: true,
      component: 'InputNumber',
    },
    {
      label: '与转运计划是否有偏移',
      field: 'jhsfypy',
      required: true,
      component: 'Input',
    },
    {
      label: '偏移情况说明',
      field: 'pyqksm',
      component: 'Input',
    },
    {
      label: '转运联单',
      field: 'zyldlj',
      component: 'JUpload',
      componentProps: {
        showUploadList: false,
        accept: '.pdf,.zip',
        beforeUpload: (file) => {
          let isshow = false;
          const isFile = file.name.split('.')[file.name.split('.').length - 1];
          if (isFile == 'pdf' || isFile == 'zip' || isFile == 'PDF') {
            isshow = true;
          } else {
            createMessage.error('只能上传.pdf/.zip格式的文件');
            isshow = false;
          }
          return isshow;
        },
      },
    },
  ];
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 160,
    schemas: AdviceFormSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 12 },
  });
  //设置标题
  const title = ref('接收完成填报');
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单

    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });

    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
  });

  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      await updateCzwc(values);
      //关闭弹窗
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
