import React, { useEffect } from 'react';
import { Button, Form, Input, Modal, Radio } from 'antd';
import type { RoleInfo, RoleListItem } from '../data.d';

export interface UpdateFormProps {
  onCancel: () => void;
  onSubmit: (values: RoleInfo) => void;
  onDelete: (values: string) => void;
  updateModalVisible: boolean;
  currentData: Partial<RoleInfo>;
}

const FormItem = Form.Item;

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const UpdateRoleForm: React.FC<UpdateFormProps> = (props) => {
  const [form] = Form.useForm();

  const {
    onSubmit,
    onCancel,
    onDelete,
    updateModalVisible,
    currentData,
  } = props;

  useEffect(() => {
    if (form && !updateModalVisible) {
      form.resetFields();

    }
  }, [props.updateModalVisible]);

  useEffect(() => {
    if (currentData) {
      form.setFieldsValue({
        ...currentData,
      });
    }
  }, [props.currentData]);

  const handleSubmit = () => {
    if (!form) return;
    form.submit();
  };

  const handleFinish = (values: { [key: string]: any }) => {
    if (onSubmit) {
      onSubmit(values as RoleInfo);
    }
  };

  const renderContent = () => {
    return (
      <>
        <FormItem
          name="id"
          label="主键"
          hidden
        >
          <Input id="update-id" placeholder="请输入主键" />
        </FormItem>
        <FormItem
          name="role_name"
          label="名称"
          rules={[{ required: true, message: '请输入角色名称' }]}
        >
          <Input id="update-name" placeholder={'请输入角色名称'} />
        </FormItem>

        <FormItem
          name="remark"
          label="备注"
        >
          <Input.TextArea id="update-remark" placeholder={'请输入备注'} rows={2} />
        </FormItem>
      </>
    );
  };


  const modalFooter = { okText: '保存', onOk: handleSubmit, onCancel, };

  return (
    <Modal
      forceRender
      destroyOnClose
      title="修改角色信息"
      open={updateModalVisible}
      {...modalFooter}
      footer={[
        <Button key="back" onClick={onCancel}>
          取消
        </Button>,
        <Button key="del" type="primary" onClick={() => {
          if (currentData.role_name != undefined) {
            onDelete(currentData.role_name)
          }

        }}>
          删除
        </Button>,
        <Button
          key="submit"
          type="primary"
          onClick={handleSubmit}
        >
          确定
        </Button>,
      ]}
    >
      <Form
        {...formLayout}
        form={form}
        onFinish={handleFinish}
      >
        {renderContent()}
      </Form>
    </Modal>
  );
};

export default UpdateRoleForm;
