// 创建弹窗
import React, { useRef, useEffect } from 'react';
import { createForm } from '@formily/core';
import { createSchemaField } from '@formily/react';
import Modal, { ModalProps } from '@/components/Modal';
import { Form, FormItem, Input, Select } from '@formily/antd';
import { addUser, updateUser } from '@/services/system/accountManage';

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

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

const form = createForm({});

const EditRoleModal = ({ onOk, onCancel, editModalData, ...rest }: EditRoleModalPropsType) => {
  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="role_name"
            title="角色名"
            required
            x-decorator="FormItem"
            x-component="Input"
            x-component-props={{
              placeholder: '请输入账号名',
            }}
          />
          <SchemaField.String
            name="remark"
            title="备注"
            required
            x-decorator="FormItem"
            x-component="Input"
            x-component-props={{
              placeholder: '请输入密码',
            }}
          />
        </SchemaField>
      </Form>
    </Modal>
  );
};

export default EditRoleModal;
