<template>
    <BasicModal v-bind="$attrs" @register="registerModal" title="添加任务" @ok="handleSubmit">
      <BasicForm @register="registerForm" />
    </BasicModal>
  </template>
  <script lang="ts" setup>
    import { ref, computed, unref } from 'vue';
    import { BasicModal, useModalInner } from '@/components/Modal';
    import { BasicForm, useForm } from '@/components/Form';
    import { formSchema } from '../data';
    import { addPicTask } from '@/api/monitor/index';
    import { useMessage } from '@/hooks/web/useMessage';
    const { createMessage } = useMessage();
  
    defineOptions({ name: 'DeptModal' });
  
    const emit = defineEmits(['success', 'register']);
  
    const isUpdate = ref(true);
  
    const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
      labelWidth: 100,
      baseColProps: { span: 24 },
      schemas: formSchema,
      showActionButtonGroup: false,
    });
  
    const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
      resetFields();
      setModalProps({ confirmLoading: false });
    });
  
  
    async function handleSubmit() {
      try {
        const values = await validate();
        let query = values;
        const data = await addPicTask(query);
          if (data) {
            setModalProps({ confirmLoading: true });
            // TODO custom api
            closeModal();
            emit('success');
            return createMessage.success('新增成功');
          } else {
            return createMessage.error('新增失败');
          }
      } finally {
        setModalProps({ confirmLoading: false });
      }
    }
  </script>
  