import React, { useState, useEffect } from 'react';
import { Form, Modal, TreeSelect, Table, Button, Input } from 'antd';
import { connect } from 'dva';
const { TreeNode } = TreeSelect;

function ModifyManagerModal(props) {
  const { visible, onClose, currentItem, loading, depUserTree, dataSource, dispatch, onOk } = props;
  const [form] = Form.useForm();

  useEffect(() => {
    form.resetFields();
  }, [visible]);

  const handleOk = () => {
    form.validateFields().then(({ managerID }) => {
      const [user_id, dep_id] = managerID.split('||');
      dispatch({
        type: 'approval/modifyManager',
        payload: {
          user_id: Number(user_id),
          //project_status为0时为售前项目,flag填5
          //project_status为1时为执行项目,flag填0
          flag: currentItem?.project_status == 0 ? 5 : 0,
          project_id: Number(currentItem?.id),
        },
        callback: () => onOk?.(),
      });
    });
  };

  return (
    <Modal
      title="修改项目经理"
      confirmLoading={loading}
      maskClosable={false}
      destroyOnClose
      visible={visible}
      onCancel={onClose}
      onOk={handleOk}
    >
      <Form labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} form={form}>
        <Form.Item
          label="项目经理"
          name="managerID"
          rules={[{ required: true, message: '请选择项目经理' }]}
        >
          <TreeSelect
            showSearch
            allowClear
            style={{ width: '100%' }}
            placeholder="请选择项目经理"
            multiple={false}
            filterTreeNode={(input, option) => {
              return option.props.title === input;
            }}
            treeData={depUserTree}
          />
        </Form.Item>
      </Form>
    </Modal>
  );
}
export default connect()(ModifyManagerModal);
