function OaUserSelect(layui, opt) {
    var $ = layui.$;
    var layer = layui.layer;
    var restAjax = layui.restajax;

    var isApp = opt && opt.isApp ? opt.isApp : false;
    var headers = opt && opt.headers ? opt.headers : {};
    var departmentRootId = opt && opt.departmentRootId ? opt.departmentRootId : 0;
    var selectedUserIdArray = opt && opt.selectedUserIdArray && (opt.selectedUserIdArray instanceof Array) ? opt.selectedUserIdArray : [];
    var selectType = opt && opt.selectType && (opt.selectType === 'checkbox' || opt.selectType === 'radio') ? 'radio' : 'checkbox';
    var onConfirm = opt && opt.onConfirm && typeof(opt.onConfirm) === 'function' ? opt.onConfirm : null;

    var Consts = {
        avatarColorArray: [
            {
                bgColor: '#b4b4c4',
                color: '#fff'
            },
            {
                bgColor: '#7182e7',
                color: '#fff'
            },
            {
                bgColor: '#304eff',
                color: '#fff'
            },
            {
                bgColor: '#ff75ef',
                color: '#fff'
            },
            {
                bgColor: '#ff7878',
                color: '#fff'
            },
            {
                bgColor: '#d2d2d2',
                color: '#000'
            },
            {
                bgColor: '#b8c926',
                color: '#fff'
            },
            {
                bgColor: '#8d3434',
                color: '#fff'
            }
        ]
    }
    // 变量
    var Properties = {
        containerWidth : 0,
        containerTopSelectedUserListWidth : 0,
        containerBottomLeftWidth : 0,
        containerBottomLeftListWidth : 0,
        containerBottomLeftListHeight : 0,
        containerBottomRightWidth : 0,
        containerBottomRightListWidth : 0,
        containerBottomRightListHeight : 0,
        containerBottomRightListUserItemInfoDivWidth : 0,
        selectedDepartmentArray: [],
        subDepartmentArray: [],
        userArray: [],
        selectedUserArray: [],
        layerIndex: null
    }
    var Methods = {
        initStyle: function() {
            document.querySelectorAll('.select-user-container').forEach(function(item) {
                item.style.width = Properties.containerWidth;
            });
            document.querySelectorAll('.select-user-container .top .selected-user .list').forEach(function(item) {
                item.style.width = Properties.containerTopSelectedUserListWidth;
            });
            document.querySelectorAll('.select-user-container .bottom .left').forEach(function(item) {
                item.style.width = Properties.containerBottomLeftWidth;
            });
            document.querySelectorAll('.select-user-container .bottom .left .list').forEach(function(item) {
                item.style.width = Properties.containerBottomLeftListWidth;
                item.style.height = Properties.containerBottomLeftListHeight;
            });
            document.querySelectorAll('.select-user-container .bottom .right').forEach(function(item) {
                item.style.width = Properties.containerBottomRightWidth;
            });
            document.querySelectorAll('.select-user-container .bottom .right .list').forEach(function(item) {
                item.style.width = Properties.containerBottomRightListWidth;
                item.style.height = Properties.containerBottomRightListHeight;
            });
            document.querySelectorAll('.select-user-container .footer').forEach(function(item) {
                item.style.width = Properties.containerWidth;
            })
        },
        // 初始化用户列表样式
        initUserListStyle: function() {
            document.querySelectorAll('.select-user-container .bottom .right .list .user-item .info div').forEach(function(item) {
                item.style.width = Properties.containerBottomRightListUserItemInfoDivWidth;
            });
            document.querySelectorAll('.select-user-container .bottom .right .list .user-item .checked').forEach(function(item) {
                item.style.right = 28 +'px';
            });
        },
        // 用户是否被选择
        getSelectedUserIndex: function (userId) {
            for(var i = 0; i < Properties.selectedUserArray.length; i++) {
                var item = Properties.selectedUserArray[i];
                if(userId === item.userId) {
                    return i;
                }
            }
            return -1;
        },
        // 刷新已选择的用户
        refreshSelectedUser: function() {
            var doms = '';
            for(var i = 0, item; item = Properties.selectedUserArray[i++];) {
                doms += '<a href="javascript:void(0);" class="layui-btn layui-btn-xs remove-user" data-user-id="'+ item.userId +'" >'+ item.userName +' <i class="fa fa-close"></i></a>';
            }
            $('#selectedUsers').empty();
            $('#selectedUsers').append(doms);
        },
        // 刷新选择的部门
        refreshSelectedDepartments: function() {
            var doms = '';
            for(var i = 0, item; item = Properties.selectedDepartmentArray[i++];) {
                doms += '<span '+ (i === Properties.selectedDepartmentArray.length ? '' : 'class="selected-department"') +' data-department-id="'+ item.departmentId +'">'+ item.departmentName +'</span>';
            }
            $('#selectedDepartments').empty();
            $('#selectedDepartments').append(doms);
        },
        // 刷新下级部门
        refreshSubDepartments: function() {
            var doms = '';
            for(var i = 0, item; item = Properties.subDepartmentArray[i++];) {
                doms += '<div class="sub-department" data-department-id="'+ item.departmentId +'"><i class="fa fa-university"></i> '+ item.departmentName +'</div>'
            }
            $('#subDepartments').empty();
            $('#subDepartments').append(doms);
        },
        // 刷新用户
        refreshUsers: function() {
            var doms = '';

            for(var i = 0, item; item = Properties.userArray[i++];) {
                var color = Consts.avatarColorArray[(i - 1) % Consts.avatarColorArray.length];

                doms += [
                    '<div class="user-item '+ (Methods.getSelectedUserIndex(item.userId) != -1 ? 'active' : '') +'" data-user-id="'+ item.userId +'" data-user-name="'+ item.userName +'">',
                    '   <div class="avatar" style="background-color: '+ color.bgColor +'; color: '+ color.color +'">'+ item.userName.substring(0, 1) +'</div>',
                    '   <div class="info">',
                    '       <div class="username">'+ item.userName +'（'+ item.userUsername +'）</div>',
                    '       <div class="phone">'+ item.userPhone +'</div>',
                    '   </div>',
                    '   <div class="checked"><i class="fa fa-check"></i></div>',
                    '</div>'
                ].join('');
            }
            $('#users').empty();
            $('#users').append(doms);
            Methods.initUserListStyle();
        },
        // 清除选择的部门
        clearSelectedDepartment: function(departmentId) {
            for(var i = 0; i < Properties.selectedDepartmentArray.length; i++) {
                var item = Properties.selectedDepartmentArray[i];
                if(departmentId === item.departmentId) {
                    Properties.selectedDepartmentArray.splice(i, Properties.selectedDepartmentArray.length - i);
                    return;
                }
            }
        },
        // 移除点击事件
        removeClickEvent: function() {
            $(document).off('click', '.select-user-container .selected-department')
            // 部门列表点击
            $(document).off('click', '.select-user-container .sub-department')
            // 用户列表点击
            $(document).off('click', '.select-user-container .user-item')
            // 移除
            $(document).off('click', '.select-user-container .remove-user');

            $(document).off('click', '#userSelectConfirmBtn');
            // 关闭
            $(document).off('click', '#userSelectCloseBtn');
        },
        // 关闭窗口
        close: function() {
            layer.close(Properties.layerIndex);
        },
    }
    var Init = {
        initHtml: function() {
            var html = [
                '<div class="select-user-container">',
                '    <div class="top">',
                '        <div class="selected-user">',
                '            <div id="selectedUsers" class="list"></div>',
                '        </div>',
                '    </div>',
                '    <div class="center">',
                '        <div id="selectedDepartments" class="list"></div>',
                '    </div>',
                '    <div class="bottom">',
                '        <div class="left">',
                '            <div id="subDepartments" class="list"></div>',
                '        </div>\n' +
                '        <div class="right">',
                '            <div id="users" class="list"></div>',
                '        </div>',
                '    </div>',
                '    <div class="footer">',
                '        <div class="layui-btn-group">',
                '            <button id="userSelectConfirmBtn" type="button" class="layui-btn layui-btn-sm">确认</button>',
                '            <button id="userSelectCloseBtn" type="button" class="layui-btn layui-btn-primary layui-btn-sm">关闭</button>',
                '        </div>',
                '    </div>',
                '</div>'
            ].join('');
            return html;
        },
        // 初始化选择的用户
        initSelectedUser: function () {
            if(selectedUserIdArray.length == 0) {
                Methods.refreshSelectedUser();
                return;
            }
            var url;
            if(isApp) {
                url = 'app/user/list/ids';
            } else {
                url = 'api/user/listbyids';
            }
            console.log(headers);
            restAjax.post(restAjax.path(url, []), {
                ids: selectedUserIdArray
            }, {
                headers: headers
            }, function(code, data) {
                for(var i = 0, item; item = data[i++];) {
                    var user = {
                        userId: item.userId,
                        userName: item.userName
                    };
                    Properties.selectedUserArray.push(user);
                    Methods.refreshSelectedUser();
                }
            }, function(code, data) {
                layer.msg(data.msg);
            });
        },
        // 刷新部门和用户
        refreshDepartmentUser: function () {
            Methods.refreshSelectedDepartments();
            Methods.refreshSubDepartments();
            Methods.refreshUsers();
        },
        // 初始化部门与部门用户
        initDepartmentAndUser: function(parentDepartmentId) {
            var loadLayerIndex;
            var url;
            if(isApp) {
                url = 'app/department/user/get-department-contain-user-and-sub/department-id/{departmentId}';
            } else {
                url = 'api/department/user/get-department-contain-user-and-sub/department-id/{departmentId}';
            }
            console.log(headers);
            restAjax.get(restAjax.path(url, [parentDepartmentId]), {}, {
                headers: headers
            }, function(code, data) {
                Properties.selectedDepartmentArray.push({
                    departmentId: data.departmentId,
                    departmentName: data.departmentName,
                    departmentCode: data.departmentCode
                });
                Properties.subDepartmentArray = data.subDepartments;
                Properties.userArray = data.users;
                Init.refreshDepartmentUser();
            }, function(code, data) {
                layer.msg(data.msg);
            }, function() {
                loadLayerIndex = layer.msg('正在加载...', {icon: 16, time: 0, shade: 0.3});
            }, function() {
                layer.close(loadLayerIndex);
            });
        },
        initClickEvent: function() {
            $(document).on('click', '.select-user-container .selected-department', function() {
                var departmentId = this.dataset.departmentId;
                Methods.clearSelectedDepartment(departmentId);
                Init.initDepartmentAndUser(departmentId);
            })
            // 部门列表点击
            $(document).on('click', '.select-user-container .sub-department', function() {
                var departmentId = this.dataset.departmentId;
                Init.initDepartmentAndUser(departmentId);
            })
            // 用户列表点击
            $(document).on('click', '.select-user-container .user-item', function() {
                var userId = this.dataset.userId;
                var userName = this.dataset.userName;
                if(selectType === 'radio') {
                    // 单选
                    Properties.selectedUserArray = [];
                    Properties.selectedUserArray.push({
                        userId: userId,
                        userName: userName
                    });
                } else {
                    // 多选
                    var selectedIndex = Methods.getSelectedUserIndex(userId);
                    if(selectedIndex == -1) {
                        // 不存在，添加
                        Properties.selectedUserArray.push({
                            userId: userId,
                            userName: userName
                        });
                    } else {
                        // 存在，移除
                        Properties.selectedUserArray.splice(selectedIndex, 1);
                    }
                }
                Methods.refreshSelectedUser();
                Methods.refreshUsers();
            })
            // 移除
            $(document).on('click', '.select-user-container .remove-user', function() {
                var userId = this.dataset.userId;
                var selectedIndex = Methods.getSelectedUserIndex(userId);
                Properties.selectedUserArray.splice(selectedIndex, 1);
                Methods.refreshSelectedUser();
                Methods.refreshUsers();
            });

            $(document).on('click', '#userSelectConfirmBtn', function(e) {
                if(onConfirm) {
                    onConfirm(Properties.selectedUserArray);
                }
                Methods.close();
            });
            // 关闭
            $(document).on('click', '#userSelectCloseBtn', function(e) {
                Methods.close();
            });
        }
    }

    this.open = function() {
        var winWidth = window.innerWidth;
        var winHeight = window.innerHeight;
        var layerWidth;
        var layerHeight;
        if(isApp) {
            layerWidth = winWidth;
            layerHeight = winHeight * 0.6;
        } else {
            layerWidth = winWidth > 400 ? 400 : winWidth;
            layerHeight = winHeight > 400 ? 400 : winHeight * 0.8
        }

        Properties.containerWidth = layerWidth +'px';
        Properties.containerTopSelectedUserListWidth =  (layerWidth - 2) + 'px';
        Properties.containerBottomLeftWidth = (layerWidth * (2 / 5) - 2) +'px';
        Properties.containerBottomLeftListWidth = (layerWidth * (2 / 5) + 16) +'px';
        Properties.containerBottomLeftListHeight = (layerHeight - 166) +'px';
        Properties.containerBottomRightWidth = (layerWidth * (3 / 5) - 1) +'px';
        Properties.containerBottomRightListWidth = (layerWidth * (3 / 5) + 16) +'px';
        Properties.containerBottomRightListHeight = (layerHeight - 166) +'px';
        Properties.containerBottomRightListUserItemInfoDivWidth = (layerWidth * (3 / 5) - 56) +'px';

        Properties.layerIndex = layer.open({
            type: 1,
            area: [layerWidth +'px', layerHeight +'px'],
            offset: isApp ? 'b' : 'auto',
            closeBtn: 0,
            anim: isApp ? 2 : 0,
            isOutAnim: false,
            title: false,
            shadeClose: true,
            scrollbar: false,
            content: Init.initHtml(),
            success: function() {
                Methods.initStyle();
                Methods.initUserListStyle();
                Init.initSelectedUser();
                Init.initDepartmentAndUser(departmentRootId);
                Init.initClickEvent();
            },
            end: function () {
                // 所有属性置空
                Properties.selectedDepartmentArray = [];
                Properties.subDepartmentArray = [];
                Properties.userArray = [];
                Properties.selectedUserArray = [];
                Methods.removeClickEvent();
            }
        });
    }

}