var $;
layui.config({
    base: ctx + '/resources/layuiadmin/' //静态资源所在路径
}).use(['index', 'jquery', 'element'], function() {
    $ = layui.jquery;
    let element = layui.element;
    let admin = layui.admin;

    notifyStatus()

    admin.events.message = null;

    $.ajax({
        url: ctx + "/auth/getPermissionMenusOnceAll",
        type: 'POST',
        dataType: 'json',
        success: function (menus) {
            g_menus = menus;
            //获取父节点
            var html = "";

            /** 左边菜单，顶部节点id */
            var rootId = null;
            var tempRoot = [];
            var root = [];
            var flag = false;

            // 列表排序遍历
            $.each (g_menus, function(i, menu) {
                // 第一遍循环，查找根节点的id
                if (menu.parentId == null) {
                    rootId = menu.id;
                    // 同 break
                    return false;
                }
            });

            if (rootId == null || rootId == undefined) {
                return;
            }

            var index = 0;
            $.each (g_menus, function (i, menu) {
                // 第二遍循环，查找根节点菜单
                if (menu.parentId == rootId) {
                    menu.subMenu = [];
                    root[index] = menu;
                    index++;
                }
            })
            /** 获取菜单结构 */
            menuRecursion(root, g_menus);

            var menuText = menuHtml("", "", root, true);

            $('#leftMenu').html(menuText)
            element.render('nav')
        }
    })
})

function menuRecursion(root, data) {
    root.sort(function (a, b) {
        return a.menuOrder - b.menuOrder;
    });

    for (var i=0; i < root.length; i++) {
        for (var j=0; j<data.length; j++) {
            if (data[j].parentId == root[i].id) {
                var subMenuLength = root[i].subMenu.length;
                data[j].subMenu = [];
                root[i].subMenu[subMenuLength] = data[j];
                //data.splice(j, 1);
            }
        }

        if (root[i].subMenu.length > 0) {
            menuRecursion(root[i].subMenu, data);
        }
    }
}

function menuHtml(start, padding, root, isRoot) {

    if (root.length > 0) {
        if (isRoot) {
            start += '<ul class="layui-nav layui-nav-tree" lay-shrink="all" id="LAY-system-side-menu" lay-filter="layadmin-system-side-menu">';
        } else {
            start += '<dl class="layui-nav-child">';
        }
    }

    for (var i=0; i<root.length; i++) {
        var subMenu = root[i].subMenu;
        if (isRoot) {
            start += '<li class="layui-nav-item">';
            if (root[i].link !== "" && root[i].link !== undefined && root[i].link !== "#") {
                start += '<a lay-href="'+ ctx + root[i].link + '">';
                start += padding + root[i].name + '</a>';
            } else {
                start += '<a href="javascript:;" lay-tips="'+ root[i].name + '" lay-direction="2">';
                start += '' + root[i].name + '</a>';
            }

            if (subMenu.length > 0) {
                start = menuHtml(start, padding + "&nbsp;&nbsp;&nbsp;&nbsp;", subMenu, false);
            }

            start += '</li>';
        } else {
            start += '<dd class="layui-nav-item">';
            if (root[i].link !== "" && root[i].link !== undefined && root[i].link !== "#") {
                start += '<a lay-href="'+ ctx + root[i].link + '">';
                start += padding + root[i].name + '</a>';
            } else {
                start += '<a href="javascript:;" lay-tips="'+ root[i].name + '" lay-direction="2">';
                start += padding + root[i].name + '</a>';
            }

            if (subMenu.length > 0) {
                start = menuHtml(start, padding + "&nbsp;&nbsp;&nbsp;&nbsp;", subMenu, false);
            }

            start += '</dd>';
        }
    }



    if (root.length > 0) {
        if (isRoot) {
            start += '</ul>';
        } else {
            start += '</dl>';
        }
        start += '';
    }

    return start;
}

function notifyStatus() {
    $.ajax({
        url: ctx + "/notification/countStatus",
        method: 'get',
        dataType: 'JSON',
        contentType: "application/json",
        success: function (count) {
            if (count > 0){
                $('.layui-badge-dot').show();
            } else {
                $('.layui-badge-dot').hide();
            }
        }
    })
}