import { Form, Modal } from "antd";
import { useEffect, useState } from "react";
import PersonnelSelect from "zy-react-library/components/Select/Personnel/Gwj";

function ManagerModal(props) {
  const [form] = Form.useForm();
  const [departmentId, setDepartmentId] = useState();
  const [submitting, setSubmitting] = useState(false);

  useEffect(() => {
    if (props.currentRecord && props.currentRecord.fireRegionId) {
      form.setFieldsValue({ managerId: props.currentRecord.managerId });
    }
  }, []);

  const handleCancel = () => {
    form.resetFields();
    setDepartmentId(undefined);
    props.onCancel();
  };

  const handleSubmit = async (values) => {
    if (submitting) {
      return;
    }
    console.log(props.currentRecord);
    setSubmitting(true);
    values.fireRegionId = props.currentRecord.fireRegionId;
    values.regionConfigId = props.currentRecord.regionConfigId;

    try {
      await props.deviceRegionManager({ ...values });
      handleCancel();
      props.getData();
    }
    finally {
      setSubmitting(false);
    }
  };

  return (
    <Modal
      destroyOnClose
      open={props.open}
      title="设置负责人"
      onCancel={handleCancel}
      onOk={form.submit}
      confirmLoading={props.loading || submitting}
      maskClosable={false}

      width={640}
    >
      <Form form={form} layout="horizontal" onFinish={handleSubmit} labelCol={{ span: 4 }}>

        <Form.Item label="负责人" name="managerId" rules={[{ required: true, message: "请选择负责人" }]}>
          <PersonnelSelect
            isNeedDepartmentId={false}
            isNeedCorpInfoId={true}
            params={{ corpinfoId: props.currentRecord.corpinfoId }}
            onGetLabel={label => form.setFieldValue("managerName", label)}
          />
        </Form.Item>
        <Form.Item hidden name="managerName">
          <input />
        </Form.Item>
      </Form>
    </Modal>
  );
}

export default ManagerModal;
