<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <base th:href="${#request.getContextPath() + '/'} ">
        <meta charset="UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=11,chrome=1"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <link rel="stylesheet" type="text/css" href="assets/fonts/font-awesome/css/font-awesome.css"/>
        <link rel="stylesheet" type="text/css" href="assets/js/vendor/bootstrap/css/bootstrap.min.css"/>
        <link rel="stylesheet" type="text/css" href="assets/js/easyui/themes/default/easyui.css"/>
        <link rel="stylesheet" type="text/css" href="assets/js/easyui/themes/icon.css"/>
        <link rel="stylesheet" type="text/css" href="assets/js/vendor/zTree3/css/metroStyle/metroStyle.css"/>
        <link rel="stylesheet" type="text/css" href="assets/css/minimal.css"/>
        <link rel="stylesheet" type="text/css" href="assets/css/system.css">
    </head>
    <body>
        <div id="app" class="easyui-layout easyui-layout-dialog dept-selector">
            <div class="selector-title" data-options="region:'north'">
                <div id="selectUsers" class="selector-title-wrapper"></div>
            </div>
            <div class="selector-tree" data-options="region:'west',split:false,collapsible:false,border:true">
                <div class="selector-tree-wrapper">
                    <ul id="leftTree" class="ztree"></ul>
                </div>
            </div>
            <div class="selector-body" data-options="region:'center',border:true">
                <div class="selector-body-wrapper">
                    <div class="selector-body-search">
                        <input type="text" class="form-control" id="name" placeholder="检索用户名" oninput="searchUser()"/>
                    </div>
                    <div id="users" class="selector-body-content list-group"></div>
                </div>
            </div>
            <div class="edit-button-footer2">
                <div class="col-sm-offset-4 col-sm-8">
                    <button type="button" class="btn btn-primary" onclick="confirmBox()">确认</button>
                    <!--
                    <button type="button" class="btn btn-default" onclick="closeBox()">关闭</button>
                    -->
                </div>
            </div>
        </div>
        <input type="hidden" id="selectedUserIds"/>
        <script type="text/javascript" src="assets/js/jquery-3.5.1.min.js"></script>
        <script type="text/javascript" src="assets/js/easyui/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="assets/js/easyui/locale/easyui-lang-zh_CN.js"></script>
        <script type="text/javascript" src="assets/js/vendor/zTree3/js/jquery.ztree.all.js"></script>
        <script type="text/javascript" src="assets/js/common.js"></script>
        <script type="text/javascript">
            var hrefParams = top.restAjax.params(window.location.href);
            $('#selectedUserIds').val(hrefParams.selectedUserIds);
            // 是否单选
            var single = hrefParams.single;
            var selectDepartmentUserArray = [];
            function closeBox() {
                top.DialogBox.closeBox();
            }
            function confirmBox() {
                top.DialogBox.dialogData.selectedDepartmentUsers = selectDepartmentUserArray;
                closeBox();
            }
            // 人员是否已经选择
            function isUserSelected(userId) {
                var isSelected = false;
                for(var i = 0, item; item = selectDepartmentUserArray[i++];) {
                    if(item.userId == userId) {
                        isSelected = true;
                        break;
                    }
                }
                return isSelected;
            }
            // 删除已经选择的人员
            function removeSelectedUser(userId) {
                for(var i = 0, item; item = selectDepartmentUserArray[i]; i++) {
                    if(item.userId == userId) {
                        selectDepartmentUserArray.splice(i, 1);
                        $('#selected_user_'+ userId).remove();
                        break;
                    }
                }
            }
            // 清空已经选择的人员
            function clearSelectedUser() {
                selectDepartmentUserArray.splice(0, selectDepartmentUserArray.length);
                $('.selected_user').remove();
            }
            // 添加人员
            function addSelectedUser(userId, userName, userUsername) {
                selectDepartmentUserArray.push({
                    userId: userId,
                    userName: userName,
                    userUsername: userUsername
                });
                $('#selectUsers').append('<a id="selected_user_'+ userId +'" href="javascript:;" class="btn btn-success btn-xs selected_user">'+ userName +' <i class="fa fa-close" onclick="removeSelectedUser(\''+ userId +'\')"></i></a>');
            }
            // 选择人员
            function selectUser(userId, userName, userUsername) {
                // 如果是单选
                if(single == 'true') {
                    clearSelectedUser();
                    addSelectedUser(userId, userName, userUsername);
                    return;
                }
                if(!isUserSelected(userId)) {
                    addSelectedUser(userId, userName, userUsername);
                }
            }
            // 筛选人员
            function searchUser() {
                $('.users').hide().filter(":contains('" + $('#name').val() + "')").show();
            }
            // 初始化人员列表
            function initUsers(parentId) {
                var loadLayerIndex;
                top.restAjax.get(top.restAjax.path('api/user/listdepartmentusers/{parentId}', [parentId]), {}, null, function(code, data) {
                    $('#users').empty();
                    var userDom = '';
                    for(var i = 0, item; item = data[i++];) {
                        var avatarDom;
                        if(null == item.userAvatar || '' == item.userAvatar) {
                            avatarDom = '<img class="user-avatar" src="assets/images/profile-photo.jpg"/> '
                        } else {
                            avatarDom = '<img class="user-avatar" src="route/file/downloadfile/false/'+ item.userAvatar +'"/> ';
                        }
                        userDom += '<a id="user_'+ item.userId +'" href="javascript:;" class="users list-group-item" onclick="selectUser(\''+ item.userId +'\', \''+ item.userName +'\', \''+ item.userUsername +'\')">'+ avatarDom + item.userName +' ['+ item.userUsername +']</a>';
                    }
                    $('#users').append(userDom);
                }, function(code, data) {
                    top.DialogBox.msg(data.msg);
                }, function() {
                    loadLayerIndex = top.DialogBox.msg(TextMessage.loading, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.DialogBox.close(loadLayerIndex);
                });
            }
            // 初始化树
            function initThree() {
                var setting = {
                    async: {
                        enable: true,
                        autoLoad: false,
                        type: 'get',
                        url: top.restAjax.path('api/department/listdepartments', []),
                        autoParam:['id'],
                        otherParam:{},
                        dataFilter: function(treeId, parentNode, childNodes) {
                            if (!childNodes) return null;
                            for (var i=0, l=childNodes.length; i<l; i++) {
                                childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
                            }
                            return childNodes;
                        }
                    },
                    callback: {
                        onClick: function(event, treeId, treeNode) {
                            initUsers(treeNode.id);
                            return false;
                        }
                    },
                    view: {
                        fontCss: {'color': 'black'}
                    }
                };
                var zTree = $.fn.zTree.init($("#leftTree"), setting);
                zTree.addNodes(null, {id: '0', pId: '-1', name: '未划分人员', url: 'javascript:;', isParent: 'true'})
            }
            // 初始化选择的人员
            function initSelectedUsers() {
                if($('#selectedUserIds').val() != '') {
                    top.restAjax.get(top.restAjax.path('api/user/listuserbyids/{selectedUserIds}', [$('#selectedUserIds').val()]), {}, null, function(code, data) {
                        for(var i = 0, item; item = data[i++]; ) {
                            selectUser(item.userId, item.userName, item.userUsername);
                        }
                    }, function(code, data) {
                        top.DialogBox.msg(data.msg);
                    }, function() {
                        loadLayerIndex = top.DialogBox.msg(TextMessage.loading, {icon: 16, time: 0, shade: 0.3});
                    }, function() {
                        top.DialogBox.close(loadLayerIndex);
                    });
                }
            }
            $(function() {
                $('#app').fadeTo(1000, 1);
                initThree();
                initUsers(0);
                initSelectedUsers();
                top.DialogBox.dialogData.selectedDepartmentUsers = null;
            });
        </script>
    </body>
</html>