import React, { useEffect, useState } from 'react';
import { Form, Input, Modal, Radio, Select, TreeSelect } from 'antd';
import type { JobList, RoleList, UserInfo, UserListItem } from '../data.d';
import { tree } from "@/utils/utils";
import { queryAllRelations } from '../../role/service';

export interface CreateFormProps {
  onCancel: () => void;
  onSubmit: (values: UserInfo) => void;
  createModalVisible: boolean;
}

const FormItem = Form.Item;

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

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

  const [roleConf, setRoleConf] = useState<RoleList[]>([]);
  const [jobConf, setJobConf] = useState<JobList[]>([]);
  const [deptConf, setDeptConf] = useState<JobList[]>([]);

  const {
    onSubmit,
    onCancel,
    createModalVisible,
  } = props;

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

    } else {
      queryAllRelations({ pageSize: 100, current: 0 }).then((res) => {
        setRoleConf(res.data)
        // setJobConf(res.data.jobRelations)
        // setDeptConf(tree(res.data.deptRelations, 0, 'parentId'))
      });
    }

  }, [props.createModalVisible]);


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

  const handleFinish = (values: UserInfo) => {
    if (onSubmit) {
      // values.deptId=Number(selectedKey)
      onSubmit(values);
    }
  };

  const renderContent = () => {
    return (
      <>
        <FormItem
          name="user_name"
          label="账号"
          rules={[{ required: true, message: '请输入账号' }]}
        >
          <Input id="update-nick_name" placeholder={'请输入账号'} />
        </FormItem>
        <FormItem
          name="phone_number"
          label="手机号"
          rules={[{ required: true, message: '请输入手机号' }]}
        >
          <Input id="update-mobile" placeholder={'请输入手机号'} />
        </FormItem>
        <FormItem
          name="password"
          label="密码"
          rules={[{ required: true, message: '请输入密码' }]}
        >
          <Input id="update-password" placeholder={'请输入密码'} />
        </FormItem>
        <FormItem
          name="role_name"
          label="权限分类"
          rules={[{ required: true, message: '请选择角色' }]}
        >
          <Select id="role_name" placeholder={'请选择角色'}>
            {roleConf.map(r => <Select.Option key={r.id} value={r.role_name}>{r.role_name + r.remark}</Select.Option>)}
          </Select>
        </FormItem>
        {/* <FormItem
          name="status"
          label="状态"
          initialValue={1}
          rules={[{ required: true, message: '请选择状态' }]}
        >
          <Radio.Group id="status">
            <Radio value={0}>禁用</Radio>
            <Radio value={1}>启用</Radio>
          </Radio.Group>
        </FormItem> */}

      </>
    );
  };


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

  return (
    <Modal
      forceRender
      destroyOnClose
      title="新建用户"
      open={createModalVisible}
      {...modalFooter}
    >
      <Form
        {...formLayout}
        form={form}
        onFinish={handleFinish}
      >
        {renderContent()}
      </Form>
    </Modal>
  );
};

export default CreateUserForm;
