// 创建弹窗
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, Select } from '@formily/antd';
import { modifyPassword } from '@/services/login';

interface ModifyPasswordModalPropsType extends ModalProps {
  onCancel: () => void;
  onOk: () => void;
}

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

const form = createForm({});

const ModifyPasswordModal = ({ onOk, onCancel, ...rest }: ModifyPasswordModalPropsType) => {
  const handleOk = async () => {
    onOk();
    const formState = form.getFormState();
    await modifyPassword(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="password"
            title="旧密码"
            required
            x-decorator="FormItem"
            x-component="Input"
            x-component-props={{
              placeholder: '请输入旧密码',
            }}
          />
          <SchemaField.String
            name="new_password"
            title="新密码"
            required
            x-decorator="FormItem"
            x-component="Input"
            x-component-props={{
              placeholder: '请输入新密码',
            }}
          />
        </SchemaField>
      </Form>
    </Modal>
  );
};

export default ModifyPasswordModal;
