// 创建弹窗
import React, { useEffect, 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 EditPermissionModalPropsType extends ModalProps {
  editModalData: any;
  onCancel: () => void;
  onOk: (val: any) => void;
}

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

const form = createForm({});

const EditPermissionModal = ({
  onOk,
  onCancel,
  editModalData,
  ...rest
}: EditPermissionModalPropsType) => {
  useEffect(() => {
    form.setInitialValues(editModalData);
  });

  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.String
            name="code"
            title="权限码"
            required
            x-decorator="FormItem"
            x-component="Input"
          />
        </SchemaField>
      </Form>
    </Modal>
  );
};

export default EditPermissionModal;
