import React, { useEffect, useState } from 'react';
import { connect } from 'react-redux';
import { DeleteOutlined, EllipsisOutlined, FormOutlined } from '@ant-design/icons';
import { Spin, Space, Button, Popconfirm, Row, Col, Tree, Table, Card, Switch, Tooltip } from 'antd';
import ProTable from '@ant-design/pro-table';
import { getDepMessage, getDepUser, createUser, updateUser, delUser, resetPwd, createDep, delDep, updateDep } from '../actions/user';
import UserModal from '../components/userModal';
import ResetPwd from '../components/resetPwd';
import DepModal from '../components/depModal';
const TreeNode = Tree.TreeNode;
const UserManage = (props) => {
    const { dispatch, loading, depMessage, depUser, clientHeight } = props
    const [modalVisible, setModalVisible] = useState(false);
    const [modalType, setModalType] = useState();
    const [modalRecord, setModalRecord] = useState();
    const [pwdModalVisible, setPwdModalVisible] = useState(false);
    const [depSelectedKeys, setDepSelectedKeys] = useState([])
    const [rowSelected, setRowSelected] = useState([])
    const [depModalVisible, setDepModalVisible] = useState(false);
    const [depModalType, setDepModalType] = useState();
    const [depModalRecord, setDepModalRecord] = useState();
    const [selectedTree, setSelectedTree] = useState();
    useEffect(() => {
        dispatch(getDepMessage())
    }, [])
    useEffect(() => {
        if (depMessage.length) {
            setDepSelectedKeys([depMessage[0].id])
            dispatch(getDepUser(depMessage[0].id))
        }
    }, [depMessage])
    const columns =
        [
            {
                title: '姓名',
                dataIndex: 'name',
            },
            {
                title: '手机号(用户名)',
                dataIndex: 'phone',
            },
            {
                title: '邮箱',
                dataIndex: 'email',
            }, {
                title: '启用状态',
                dataIndex: 'enable',
                render: (_, r) => {
                    return 
                }
            }, {
                title: '操作',
                dataIndex: 'action',
                render: (dom, record) => {
                    // return record.username == 'SuperAdmin' ? '' : [
                    //     ,
                    //      {
                    //             delUsers([record.id])
                    //         }}
                    //     >
                    //         
                    //     ,
                    //     
                    // ]
                    return [
                        ,
                         {
                                delUsers([record.id])
                            }}
                        >
                            
                        ,
                        
                    ]
                },
            },
        ];
    //弹窗确认
    const onConfirm = (values) => {
        if (modalType == 'edit') {
            dispatch(updateUser(modalRecord.id, values.contract)).then(res => {
                if (res.success) {
                    setModalVisible(false);
                    dispatch(getDepUser(depSelectedKeys[0]));
                }
            });
        } else {
            dispatch(createUser(values.contract)).then(res => {
                if (res.success) {
                    setModalVisible(false);
                    dispatch(getDepMessage());
                }
            });
        }
    }
    //打开弹窗
    const openModal = (type, record) => {
        setModalVisible(true);
        setModalType(type);
        if (type == 'edit') {
            setModalRecord(record);
        } else {
            setModalRecord(null);
        }
    }
    //打开部门弹窗
    const openDepModal = (type, record) => {
        setDepModalVisible(true)
        setDepModalType(type)
        if (type == 'edit') {
            setDepModalRecord(record);
        } else {
            setModalRecord(null);
        }
    }
    //删除用户
    const delUsers = (ids, type) => {
        dispatch(delUser(ids)).then(res => {
            dispatch(getDepUser(depSelectedKeys[0]));
            if (type == 'batch') {
                setRowSelected([]);
            }
        });
    }
    //重置密码
    const onPwdConfirm = (values) => {
        dispatch(resetPwd(modalRecord.id, { password: values.password })).then(res => {
            if (res.success) {
                setPwdModalVisible(false);
                dispatch(getDepUser(depSelectedKeys[0]));
            }
        });
    }
    //部门新增及编辑
    const onDepConfirm = (data) => {
        if (depModalType == 'edit') {
            dispatch(updateDep(data)).then(res => {
                if (res.success) {
                    setDepModalVisible(false);
                    dispatch(getDepMessage());
                }
            });
        } else {
            dispatch(createDep(data)).then(res => {
                if (res.success) {
                    setDepModalVisible(false);
                    dispatch(getDepMessage());
                }
            });
        }
    }
    // 删除部门
    const delDepartment = (id) => {
        dispatch(delDep(id)).then(res => {
            dispatch(getDepMessage())
        });
    }
    const renderTree = (item, id) => {
        return 
            
                {item.name}
            
            
                {
                    selectedTree == id ? <>
                        
 {
                            setDepModalRecord(item)
                            setDepModalVisible(true)
                            setDepModalType('edit')
                        }} />
                         { delDepartment(id) }}>
                            
                        
                    > : null
                }
             
         
    }
    return (
        
            
                
                    
                        
                        {
                            depMessage.length ?
                                 {
                                        if (e.selected) {
                                            setSelectedTree(selectedKeys)
                                            setDepSelectedKeys(selectedKeys)
                                            dispatch(getDepUser(selectedKeys[0]))
                                        }
                                    }}
                                    style={{ paddingTop: 20 }}
                                >
                                    {
                                        depMessage?.map((s, index) => {
                                            return 
                                                {
                                                    s.subordinate.map(k => {
                                                        return  { setIShowIcon(k.id) }} onMouseOut={() => { setIShowIcon(null) }}>
                                                            {
                                                                k.subordinate.map(i => {
                                                                    return  { setIShowIcon(i.id) }} onMouseOut={() => { setIShowIcon(null) }}>
                                                                    
                                                                })
                                                            }
                                                        
                                                    })
                                                }
                                            
                                        })
                                    }
                                 : ''
                        }
                    
                
                
                    
                        
                         {
                                    setRowSelected(selectedRowKeys);
                                },
                                getCheckboxProps: (record) => {
                                    return {
                                        disabled: record.username === 'SuperAdmin',
                                    }
                                },
                            }}
                            options={false}
                            search={false}
                            rowKey="id"
                            toolBarRender={() => [
                                
                                    
                                    {/*  */}
                                     { delUsers(rowSelected, 'batch') }}>
                                        
                                    
                                
                            ]}
                        />
                    
                    {
                        depMessage.length && modalVisible ?
                            
                            : ''
                    }
                    {pwdModalVisible ? 
 : ''}
                    {depModalVisible ? 
 : ''}
                
            
        
    
 
    )
}
function mapStateToProps(state) {
    const { depMessage, depUser, global } = state;
    // console.log(state);
    return {
        clientHeight: global.clientHeight,
        loading: depMessage.isRequesting,
        depMessage: depMessage.data || [],
        depUser: depUser.data || []
    };
}
export default connect(mapStateToProps)(UserManage);