import React, { useEffect } from 'react';
import { Modal, Form, Input, notification, message } from 'antd';
import { editOrgInfo } from '@/services/userCenter/userManage/api';

const EditOrgModal = props => {
  const {
    title,
    visible,
    orgID,
    orgTitle,
    description,
    onCancel,
    updateTrees,
    onSelect,
  } = props;
  const [editOrgForm] = Form.useForm(); // 添加用户

  useEffect(() => {
    editOrgForm.setFieldsValue({
      OUName: orgTitle,
      description,
    });
  }, [orgTitle, description]);

  // 提交-编辑当前机构
  const submitEditOrg = () =>
    editOrgInfo(
      orgID.id,
      editOrgForm.getFieldValue('OUName'),
      editOrgForm.getFieldValue('description') || '',
      '',
    )
      .then(res => {
        if (res.msg==='') {
          onCancel();
          notification.success({
            message: '提交成功',
            duration: 2,
          });
          // 重新获取机构树与用户表
          updateTrees();
          onSelect([orgID]);
          // setExpandedKeys([`${orgID}`]);
        } else {
          notification.error({
            message: '提交失败',
            description: res.message,
          });
        }
      })
      .catch(err => {
        message.error(err);
      });
  return (
    <Modal
      title={title}
      visible={visible}
      onCancel={onCancel}
      onOk={submitEditOrg}
      okText="确认"
      cancelText="取消"
    >
      <Form form={editOrgForm} labelCol={{ span: 4 }}>
        <Form.Item
          name="OUName"
          label="机构名称"
          rules={[{ required: true, message: '不能为空' }]}
        >
          <Input placeholder="请输入机构名称" />
        </Form.Item>
        <Form.Item name="description" label="描述">
          <Input placeholder="请输入相关描述" />
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default EditOrgModal;
