/* eslint-disable import/no-unresolved */
/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-01-13 17:26:14
 * @LastEditTime: 2022-04-27 18:56:09
 * @LastEditors: leizhe
 */
import React, { useEffect, useState } from 'react';
import { Modal, Form, Input, notification, message } from 'antd';
import { editOrgInfo } from '@/services/userManage/api';

const EditOrgModal = props => {
  const { visible, orgID, description, onCancel, updateTrees, onSelect, orgTitle1 } = props;
  const [editOrgForm] = Form.useForm(); // 添加用户
  const { Item } = Form;
  useEffect(() => {
    console.log(orgTitle1);
    console.log(orgID);
    if (visible && orgID) {
      console.log(orgID.text);
      editOrgForm.setFieldsValue({ OUName: orgID.text });
      console.log(editOrgForm.getFieldValue('OUName'));
    }
  }, [visible]);

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

export default EditOrgModal;
