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 UpdateFormProps {
  onCancel: () => void;
  onSubmit: (values: UserInfo) => Promise<void>;
  updateModalVisible: boolean;
  values: Partial<UserListItem>;
}

const FormItem = Form.Item;

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

const UpdateUserForm: React.FC<UpdateFormProps> = (props) => {
  const [form] = Form.useForm();
  const [roleConf, setRoleConf] = useState<RoleList[]>([]);
  const [jobConf, setJobConf] = useState<JobList[]>([]);
  const [deptConf, setDeptConf] = useState<JobList[]>([]);

  const {
    onSubmit,
    onCancel,
    updateModalVisible,
    values,
  } = props;

  useEffect(() => {
    if (form && !updateModalVisible) {
      form.resetFields();
    } else {
      queryAllRelations({ pageSize: 100, current: 0 }).then((res) => {
        setRoleConf(res.data)
        // setRoleConf(res.roleAll)
        // setJobConf(res.jobAll)
        // setDeptConf(tree(res.deptAll, 0, 'parentId'))
      });
    }
  }, [props.updateModalVisible]);

  useEffect(() => {
    if (values) {
      form.setFieldsValue({
        ...values,
        deptId: values.deptId + '',
      });
    }
  }, [props.values]);

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

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

  const renderContent = () => {
    return (
      <>
        <FormItem
          name="user_name"
          label="账号"
          rules={[{ required: true, message: '请输入账号' }]}
        >
          <Input id="update-nick_name" placeholder={'请输入账号'} />
        </FormItem>

        <FormItem
          name="user_alias"
          label="昵称"
          rules={[{ message: '请输入' }]}
        >
          <Input id="update-user_alias" 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="remark"
          label="备注"
          rules={[{ message: '请输入备注' }]}
        >
          <Input id="update-remark" placeholder={'请输入备注'} />
        </FormItem>
      </>
    );
  };


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

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

export default UpdateUserForm;
