var MENU;
//计算左面的
$(document).ready(function () {


    setletftime();

    $('#treeMenu').on('click', 'a', function () {
        if ($(this).hasClass("p_m")) { return; }
        $('#treeMenu li.active').removeClass('active');
        $(this).closest('li').addClass('active');
        getPageInfo(this.id)
        // $("#main_content").html(getPageInfo(this.id));
    });

    //获取用户信息
    var user = $.zui.store.get("user")
    if (user == null || typeof (user) == "undefined") {
        layer.msg('登陆已经过期，将重新登陆！', {
            time: 2000, //20s后自动关闭
        }, function () {
            self.location = base_ui_url + UI_USER_LOGIN
        })

        return;
    } else {
        var html = "<i class=\"icon icon-user\"></i>  " + user.truename + " " +user.organname+",欢迎您"
        $("#user").html(html);
    }

    var offset = "auto";
    if(ISCLIENT) {
        offset ="t";
    }
    $("#login_out_btn").on("click", function () {
        layer.confirm('您确定要退出登录？', {
            offset:offset,
            shade:0,
            btn: ['确定'] //按钮
        }, function () {
            UserLoginOUT(function () {
               
                self.location = base_ui_url + UI_USER_LOGIN
            }, function (error) {
                layer.msg(error, { icon: 3 });
            })
        });
    });



    MenuGetAll(function (data) {
        var secondMenus=[];
        //初始化菜单
        var treedata = [];
        for (var i = 0; i < data.length; i++) {
            var menu = data[i];
            var datatemp = {}
            datatemp["html"] = "<a  id=\"" + menu.id + "\" class=\"p_m\" href=\"#\"><i class=\"icon icon-" + menu.iconClass + "\"></i>" + menu.menuname + "</a>";

            datatemp["open"] = false;
            if (menu.childs.length > 0) {
                var datac = [];
                for (var j = 0; j < menu.childs.length; j++) {
                    var datactemp = {}
                    var mtemp = menu.childs[j];
                    datactemp["html"] = "<a id=\"" + mtemp.id + "\" href=\"#\"><i class=\"icon\">·</i>" + mtemp.menuname + "</a>";
                    secondMenus.push(mtemp.id);
                    datac.push(datactemp);

                    $.zui.store.set(mtemp.url,mtemp.menuname); 
                }
                datatemp["children"] = datac;
            }
            treedata.push(datatemp)

        }
        // 获取 tree 实例
        var myTree = $('#treeMenu').data('zui.tree');
        // 更新数据
        myTree.reload(treedata);
        myTree.collapse();

        MENU = data;
        //获取当前页
        var currentpage = 0;
        currentpage = $.zui.store.get("currentpage");
        if (typeof (currentpage) != undefined && currentpage != null) {
            //改变页面
            getPageInfo(currentpage);
        }else{
            getPageInfo(secondMenus[0]);
        }
    }, function (error) { });

});

function chageToPageUI(menu) {
    $('#treeMenu').tree('expand', $("#" + menu.parentId).parent());
    $('#treeMenu li.active').removeClass('active');
    $("#" + menu.id).closest('li').addClass('active');
}



function setletftime() {
    var height = window.innerHeight;

    $("#mum_left").css("min-height", height - 90);
    if ($("#main").height() > height) {
        $("#mum_left").css("min-height", $("#main").height() - 80);
    }

    setTimeout(setletftime, 100);
}


function getPageInfo(id) {
    //设置当前页面
    $.zui.store.set("currentpage", id);
    if (typeof (MENU) != "undefined" && MENU != null) {
        for (var i = 0; i < MENU.length; i++) {
            var menu = MENU[i];
            if (menu.id == id) {
                if (typeof (menu.url) != "undefined") {
                    changePage(menu.url);
                    chageToPageUI(menu)
                    return;
                }
                break;
            }
            if (menu.childs.length > 0) {
                for (var j = 0; j < menu.childs.length; j++) {
                    var mtemp = menu.childs[j];
                    if (mtemp.id == id) {
                        if (typeof (mtemp.url) != "undefined") {
                            changePage(mtemp.url);
                            chageToPageUI(mtemp)
                            return;
                        }
                        break;
                    }
                }
            }

        }
    }
    return $("#main_content").load(base_ui_url + "/view/user/aa.html");


}


function changePage(url) {
    if (typeof (url) != "undefined") {
        CURRENT_PAGE_URL = url;
        var page_params = GetQueryString(url);
        $.zui.store.set("page_params", page_params);
        $("#main_content").load(base_ui_url + url);

        var name = $.zui.store.get(url); 
        if(name){
            // $("#breadcrumb_menu").html('<li murl="'+url+'" mname="'+name+'"><a href="#" onclick="changePage('+url+')">'+name+'</a></li>');
            addBreadMenu(url, name, true);
        }
        event = document.createEvent('MessageEvent');
        var origin = window.location.protocol + '//' + window.location.host;
        event.initMessageEvent('changepage', true, true, CURRENT_PAGE_URL, origin, 1234, window, null);
        document.dispatchEvent(event);
    }
}


function GetQueryString(url)
{
     var theRequest = new Object();
     if (url.indexOf("?") != -1) {
        var str = url.split("?")[1];
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
     }
     return theRequest;
}

function addBreadMenu(url, name, flag){
    var menu = hasMenu(url, name, flag);
    if(menu >= 0){
        // $("#breadcrumb_menu").remove($(menu).nextAll());
        var children = $("#breadcrumb_menu").children("li");
        for(var i =0;i<children.length;i++) {
            if(i>menu){
                $(children[i]).remove();
            }
        }
    }else{
        if(flag)
            $("#breadcrumb_menu").html('<li murl="'+url+'" mname="'+name+'"><a href="#" onclick="changePage(\''+url+'\')">'+name+'</a></li>');
        else
            $("#breadcrumb_menu").append('<li murl="'+url+'" mname="'+name+'"><a href="#" onclick="changePage(\''+url+'\')">'+name+'</a></li>');
    }        
}

function hasMenu(url, name){
    var children = $("#breadcrumb_menu").children("li");
    for(var i =0;i<children.length;i++) {
        if($(children[i]).attr("murl")==url && $(children[i]).attr("mname")==name){
            return i;
        }
    }
    return -1;
}