//定义默认加载url
var defaultUrl = '';
//定义默认模块名
var defaultModle = '基本管理';
//系统标识
var sysFlag = 'power';
//用户id
var userId = $("#userId").val();
//选中菜单设置进浏览器存储
function setBrowItem(_this){
    var flag = sysFlag + '_' + userId;
    //设置a标签id
    localStorage.setItem(flag,$(_this).prop("id"));
    localStorage.setItem(flag+"_url",$(_this).prop("href"));
}
//取出历史选中并选中
function getBrowItem(){
    //取出a标签id
    var flag = sysFlag + '_' + userId;
    var hisMenuId = localStorage.getItem(flag);
    if(hisMenuId == 'undefined'){
        //报错时
        $("#iFrame1").prop("src", path+defaultUrl);
    }
    //选中历史菜单节点
    if(hisMenuId != null && hisMenuId != ''){
        var dom = $("#"+hisMenuId);
        if(dom != undefined && dom != null && dom != '') {
            indexSelected(dom);
            //取出历史菜单地址跳转历史页面
            var hisMenuUrl = localStorage.getItem(flag + "_url");
            if(hisMenuUrl == 'undefined'){
                //报错时
                hisMenuUrl = $("#"+hisMenuId).prop("href");
            }
            $("#iFrame1").prop("src", hisMenuUrl);
        }
    }
}
//菜单选中方法
function indexSelected(_this) {
    $("#indexTreeMenu li").removeClass("active");
    $("ul.treeview-menu").find("a").removeClass("indexSelected");
    $("#indexTreeMenu .treeview").find("a").removeClass("modleSelected");
    $(_this).addClass("indexSelected");
    $(_this).parent().parent().siblings().addClass('modleSelected');
    $(_this).parent().parent().parent().addClass("active");
    //设置到缓存
    setBrowItem(_this);
}
//获取菜单接口
$(function () {
    ajaxJsonFun();
});
function ajaxJsonFun() {
    $.ajax({
        url: path+"/menuPower/indexMenu",
        type: "POST",
        success: function (result) {
            if (result.code == 100) {
                var listUser = result.extend.listUser;
                var listRole = result.extend.listRole;
                var listPower=result.extend.listPower;
                if (listUser != null) {
                    var menuArr = menuRoleFun(listPower);
                    for (var i = 0; i < menuArr.length; i++) {
                        $('#indexTreeMenu').append(menuArr[i]);
                    }
                } else if (listPower!=null) {
                    var menuArr = menuRoleFun(listPower);
                    for (var i = 0; i < menuArr.length; i++) {
                        $('#indexTreeMenu').append(menuArr[i]);
                    }
                }else if (listRole != null) {
                    var menuArr = menuRoleFun(listRole);
                    for (var i = 0; i < menuArr.length; i++) {
                        $('#indexTreeMenu').append(menuArr[i]);
                    }
                }
                //取出历史选中并选中
                getBrowItem();
            }
        }
    });
}
//系统管理员
function menuRoleFun(menuArr) {
    var muluArr=[];
    var mulu=null;
    var menuLiArr=[];
    /*菜单*/
    if(menuArr!=null){
        //定义首个链接
        var firstLink = '';
        //定义是否有/user/pageUI权限
        var userPageUI = '';
        //定义是否有/user/pageUI1权限
        var userPageUI1 = '';
        /*系统目录menuArr.length*/
        for (var i = 0; i < menuArr.length; i++) {
            if ( menuArr[i].menuUrl == null || menuArr[i].menuUrl == '' || menuArr[i].menuUrl == '#') {
                mulu = liFun(menuArr[i].menuName, menuArr[i].menuIcon);
                /*链接跳转*/
                for (var j = 0; j < menuArr.length; j++) {
                    if ((menuArr[j].menuUrl != null || menuArr[j].menuUrl != '')&&menuArr[i].menuId==menuArr[j].parentId) {
                        menuLiArr.push(menuArr[j]);
                        //接收第一个链接
                        if(firstLink == ''){
                            firstLink = menuArr[j].menuUrl;
                        }
                        //接收通用的用户列表链接
                        if(userPageUI == '' && menuArr[j].menuUrl == '/user/pageUI'){
                            userPageUI = menuArr[j].menuUrl;
                        }
                        //接收脐血库的用户列表链接
                        if(userPageUI1 == '' && menuArr[j].menuUrl == '/user/pageUI1'){
                            userPageUI1 = menuArr[j].menuUrl;
                        }
                    }
                }
                if (menuLiArr != null) {
                    if (menuLiArr.length > 0) {
                        var liMenu = ulSysMenuFun(menuLiArr);
                        menuLiArr=[];
                        mulu.append(liMenu);
                    }
                }
                muluArr.push(mulu);
            }
        }
        //动态修改iframe首页链接
        var url = '';
        if(userPageUI != ''){
            url = userPageUI;
        }else if(userPageUI1 != ''){
            url = userPageUI1;
        }else{
            url = firstLink;
        }
        if(url != ''){
            defaultUrl = url;
            $("#iFrame1").prop("src",path+url);
        }
    }
    return muluArr;
}

function liFun(liName,iconCss) {
    var subLi = $("<li class=\"treeview menu-open\"></li>");
    if (liName == defaultModle) {
        subLi = $("<li href=\"#\" class='treeview menu-open active'></li>");
    }
    subLi.append(aFun(liName, iconCss));
   return subLi;
}

function aFun(aName, iconCss) {
    var subA = '';
    if(aName == defaultModle){
        subA = $("<a href=\"#\" class='modleSelected'></a>");
    }else{
        subA = $("<a href=\"#\"></a>");
    }
    var subIcon = $("<i></i>");
    subIcon.addClass(iconCss);
    var subSpan = $("<span class=\"pull-right-container\"><i class='fa fa-angle-left pull-right'></i></span>");
    subA.append(subIcon);
    subA.append("<span>" + aName + "</span>");
    subA.append(subSpan);
    return subA;
}

function ulSysMenuFun(menuArr) {
    var subUl = $(" <ul class=\"treeview-menu\" style=\"display: block;\"></ul>");
    for (var i = 0; i < menuArr.length; i++) {
        var menuId = sysFlag + "Menu_" + "_" + menuArr[i].menuId;
        var subLi = $("<li></li>");
        var subA = '';
        var menuUrl = menuArr[i].menuUrl;
        if(menuUrl == defaultUrl) {
            subA = $("<a href=" + path + menuUrl + " target=\"iFrame1\" onclick = 'indexSelected(this)' id='"+menuId+"' class='indexSelected'></a>");
        }else{
            subA = $("<a href=" + path + menuUrl + " target=\"iFrame1\" onclick = 'indexSelected(this)' id='"+menuId+"'></a>");
        }
        var subIcon = $("<i></i>");
        if (menuArr[i].menuIcon != undefined || menuArr[i].menuIcon != null || menuArr[i].menuIcon != '') {
            subIcon.addClass(menuArr[i].menuIcon);
            subA.append(subIcon);
        }
        subA.append(menuArr[i].menuName);
        subLi.append(subA);
        subUl.append(subLi);
    }
    return subUl;
}


