<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'}">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" type="text/css" href="assets/js/vendor/zTree3/css/metroStyle/metroStyle.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein" style="padding: 0;">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="dataForm">
                <div class="layui-form-item" style="height: 368px; overflow: auto;">
                    <ul id="ztree" class="ztree" style="min-height: 350px;"></ul>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交</button>
                            <button type="button" class="layui-btn layui-btn-primary close">关闭</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'form', 'laydate', 'ztree'], function() {
        var $ = layui.$;
        var form = layui.form;
        var hrefParams = top.restAjax.params(window.location.href);
        var roleId = top.restAjax.params(window.location.href).roleId;
        var zTree;
        function closeBox() {
            top.dialog.closeBox();
        }
        function initData(callback) {
            var layerIndex;
            top.restAjax.get(top.restAjax.path('api/menu/listallbyparentid/0', []), {}, null, function(code, data) {
                var setting = {
                    data: {
                        key: {
                            checked: 'right',
                            children: 'subMenus',
                            isParent: 'parent',
                            name: 'menuName',
                            title: 'menuName'
                        }
                    },
                    check: {
                        enable: true
                    }
                };
                zTree = $.fn.zTree.init($("#ztree"), setting, data);
                var nodes = zTree.getNodesByParam('menuParentId', '0', null);
                if(nodes.length < 3) {
                    for(var i = 0, node; node = nodes[i++];) {
                        zTree.expandNode(node, true, false, false);
                    }
                }
                callback();
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                layerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(layerIndex);
            });
        }
        initData(function() {
            var layerIndex;
            top.restAjax.get(top.restAjax.path('api/role/menu/list/{roleId}', [roleId]), {}, null, function(code, data) {
                for(var i = 0, item; item = data[i++];) {
                    var node = zTree.getNodeByParam('menuId', item.menuId, null);
                    if(!node) {
                        continue;
                    }
                    zTree.checkNode(node, true, false);
                }
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                layerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(layerIndex);
            });
        });

        form.on('submit(submitForm)', function(formData) {
            var nodes = zTree.getCheckedNodes(true);
            var menuIds = [];
            for(var i = 0, item; item = nodes[i++];) {
                menuIds.push(item.menuId);
            }
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var layerIndex;
                top.restAjax.put(top.restAjax.path('api/role/menu/update/{roleId}', [roleId]), {
                    menuIds: menuIds
                }, null, function(code, data) {
                    top.dialog.msg(top.dataMessage.updateSuccess, {
                        time: 0,
                        btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                        shade: 0.3,
                        yes: function(index) {
                            top.dialog.close(index);
                            window.location.reload();
                        },
                        btn2: function() {
                            closeBox();
                        }
                    });
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                    layerIndex = top.dialog.msg(top.dataMessage.updating, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(layerIndex);
                });
            });
            return false;
        });
        $('.close').on('click', function() {
            closeBox();
        });
    });
</script>
</body>
</html>