// 创建弹窗
import React, { useRef } from 'react';
import { createForm } from '@formily/core';
import { createSchemaField } from '@formily/react';
import Modal, { ModalProps } from '@/components/Modal';
import { Form, FormItem, Input } from '@formily/antd';

interface AddPermissionModalPropsType extends ModalProps {
  onCancel: () => void;
  onOk: (val: any) => void;
}

const SchemaField = createSchemaField({
  components: {
    FormItem,
    Input,
  },
});

const form = createForm({});

const AddPermissionModal = ({ onOk, onCancel, ...rest }: AddPermissionModalPropsType) => {
  const handleOk = async () => {
    form.submit(async () => {
      const formState = form.getFormState();
      onOk(formState.values);
    });
  };

  const handleCancel = () => {
    onCancel();
  };

  return (
    <Modal title="添加权限" onOk={handleOk} onCancel={handleCancel} width={800} {...rest}>
      <Form form={form} labelCol={4} wrapperCol={18}>
        <SchemaField>
          <SchemaField.String
            name="name"
            title="权限名"
            required
            x-decorator="FormItem"
            x-component="Input"
          />
        </SchemaField>
      </Form>
    </Modal>
  );
};

export default AddPermissionModal;
