<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>

    // 设置jQuery Ajax全局的参数
    $.ajaxSetup({
        type: "GET",
        error: function (jqXHR, textStatus, errorThrown) {
            isLogin();
            switch (jqXHR.status) {
                case(500):
                    $.ligerDialog.error("服务器系统内部错误");
                    break;
                case(401):
                    $.ligerDialog.error("未登录");
                    break;
                case(403):
                    $.ligerDialog.error("无权限执行此操作");
                    break;
                case(408):
                    $.ligerDialog.error("请求超时");
                    break;
                default:
                    $.ligerDialog.error("未知错误");
            }
        }
    });


    //判断是否登录
    function isLogin() {
        $.ajax({ //获取是否
            type: "POST",
            url: "${contextRoot}/system/isLogin",
            dataType: "json",
            cache: false,
            success: function (data) {
                if (!data.successFlg) {
                    location.href = "${contextRoot}/loginPage";
                }
            },
            error: function (data) {
                location.href = "${contextRoot}/loginPage";
            }
        });
    }

    var indexPage = {
        localNav: [],
        homeUrl: "${contextRoot}/homePage",
        init: function () {
            var me = this;
            //showLogo == "false"不显示logo横条
            var topHeight =120;
            if (${showLogo == "false"}) {
                topHeight = 40
            }
            //主布局
            $("#divIndexMain").ligerLayout({
                space: 0,
                topHeight: topHeight,
                leftWidth: 220,
                allowLeftCollapse: false,
                allowLeftResize: false,
                allowTopResize: false
            });

            $(".l-layout-left").css({background: "#dce6f0"})
            var ehrUser = $("#ehrUser").val();
            var tenantManager = {} ;
            var userRole = localStorage.getItem("userRole");
            //菜单列表
            var menu = [
                //标准管理
                //标准规范中心
                {id: 2, text: '标准规范', icon: '${staticRoot}/images/index/menu01.png'},
                <%--{id: 21, pid: 2, text: '平台标准', url: '${contextRoot}/integration/initial/standard', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                {id: 22, pid: 2, text: '标准管理', url: '${contextRoot}/integration/initial/application', icon: '${staticRoot}/images/index/Ins_icon.png'},
                {id: 23, pid: 2, text: '适配方案', url: '${contextRoot}/adapterPlan/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                //数据服务总线
                {id: 1, text: '服务总线', icon: '${staticRoot}/images/index/menu03.png'},
                {id: 15, pid: 1, text: '服务管理', url: '${contextRoot}/app/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                {id: 16, pid: 1, text: '转换管理', url: '${contextRoot}/processor/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                {id: 11, pid: 1, text: '流程编排', url: '${contextRoot}/flow/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                <%--{id: 13, pid: 1, text: '采集编排', url: '${contextRoot}/crawler/jobLayout', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                <%--{id: 14, pid: 1, text: '流程编辑', url: '${contextRoot}/process/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                //数据采集
                {id: 6, text: '数据采集', icon: '${staticRoot}/images/index/menu02.png'},
                {id: 60, pid: 6, text: '任务配置', url: '${contextRoot}/datacollect/configJob', icon: '${staticRoot}/images/index/Ins_icon.png'},
                {id: 61, pid: 6, text: '任务跟踪', url: '${contextRoot}/datacollect/trackJob', targetType: '1', icon: '${staticRoot}/images/index/Ins_icon.png'},
                <%--{id: 62, pid: 6, text: '任务补采', url: '${contextRoot}/datacollect/repeatDatacollect', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                //运行监控中心
                {id: 5, text: '运行监控', icon: '${staticRoot}/images/index/menu04.png'},
                {id: 51, pid: 5, text: '系统监控', url: '${contextRoot}/monitor/server/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                {id: 52, pid: 5, text: '服务监控', url: '${contextRoot}/monitor/service/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                <%--{id: 55, pid: 5, text: '远程管理', url: '${contextRoot}/shell/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>

                //资源管理
                <%--{id: 3, text: '资源服务', icon: '${staticRoot}/images/index/menu05.png'},--%>
                <%--{id: 31, pid: 3, text: '资源注册', url: '${contextRoot}/resource/resource/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                <%--{id: 32, pid: 3, text: '资源浏览', url: '${contextRoot}/resource/resourcePage', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                <%--{id: 34, pid: 3, text: '资源分类', url: '${contextRoot}/resource/rsCategory/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                <%--{id: 35, pid: 3, text: '业务资源', url: '${contextRoot}/resourceRest/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                <%--//维度管理--%>
                <%--&lt;%&ndash;{id: 4, text: '维度管理', icon: '${staticRoot}/images/index/menu5_icon.png'},&ndash;%&gt;--%>
                <%--{id: 36, pid: 3, text: '维度配置', url: '${contextRoot}/dimension/dimension', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                <%--{id: 37, pid: 3, text: '维度类别配置', url: '${contextRoot}/dimension/dimensioncatetory', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>

                //用户安全中心
                {id: 7, text: '用户安全', icon: '${staticRoot}/images/index/menu06.png'},
                {id: 71, pid: 7, text: '机构管理', url: '${contextRoot}/org/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                {id: 72, pid: 7, text: '用户管理', url: '${contextRoot}/user/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                <%--{id: 73, pid: 7, text: '角色管理', url: '${contextRoot}/role/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                <%--{id: 74, pid: 7, text: '权限管理', url: '${contextRoot}/authority/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                //质控规则管理
                {id: 8, text: '质控管理', icon: '${staticRoot}/images/index/menu05.png'},
                {id: 81, pid: 8, text: '规则管理', url: '${contextRoot}/rule/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                <%--{id: 82, pid: 8, text: '质控报告', url: '${contextRoot}/oauth2/report', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                {id: 82, pid: 8, text: '质控报告', url: '${contextRoot}/report/signin?user='+ehrUser, icon: '${staticRoot}/images/index/Ins_icon.png'},
                <%--{id: 83, pid: 8, text: '档案入库情况', url: '${contextRoot}/report/initialLs', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>

                //系统配置
                {id: 9, text: '系统配置', icon: '${staticRoot}/images/index/menu07.png'},
                {id: 91, pid: 9, text: '系统参数', url: '${contextRoot}/param/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                {id: 92, pid: 9, text: '字典管理', url: '${contextRoot}/dict/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},
                <%--{id: 93, pid: 9, text: '菜单配置', url: '${contextRoot}/menu/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                <%--{id: 94, pid: 9, text: '菜单按钮配置', url: '${contextRoot}/menu/menuAction/initial', icon: '${staticRoot}/images/index/Ins_icon.png'},--%>
                {id: 95, pid: 9, text: '数据源配置', url: '${contextRoot}/datasource/configSources', icon: '${staticRoot}/images/index/Ins_icon.png'},

            ];

            if(userRole=="admin"){
                //是管理中心用户，则添加租户管理模块
//                menu = menu.concat(tenantManager);
                var menu_1 = [
                    {id: 10, text: '租户管理', icon: '${staticRoot}/images/index/menu08.png'},
                    {id: 101, pid: 10, text: '租户管理', url: '${contextRoot}/tenant/initial', icon: '${staticRoot}/images/index/dot.png'},
                    {id: 102, pid: 10, text: '数据库实例管理', url: '${contextRoot}/tenant/dataBase/initial', icon: '${staticRoot}/images/index/dot.png'}
                ];
                menu =  menu.concat(menu_1);
            }


//            var menu_1 = [
//            ]
//            menu =  menu.concat(menu_1);
            me.menuTree = $('#ulTree').ligerTree({
                data: menu,
                idFieldName: 'id',
                parentIDFieldName: 'pid',
                checkbox: false,
                treeLine: false,
                autoCheckboxEven: false,
                needCancel: false,
                btnClickToToggleOnly: false,
                slide: true,
                nodeDraggable: false,
                isExpand: true,//true展开所有，false关闭所有
                parentIcon: true,
                childIcon: true,
                nodeWidth: 176,
                onSelect: function (obj) {
                    if (obj.data.url != undefined) {
                        if(obj.data.url.indexOf("report/signin") >= 0){
                            me.loadIframePage(obj.data.url);//质控页面
                        }else{
                            me.openMenu(obj);
                        }
                    }
                    //选中子菜单时默认选中父菜单
                    if(obj.data.children == undefined){
                        var parent = $("#ulTree li").eq(obj.data.treedataindex).parents("li");
                        var parImg = parent.children(".l-body").children(".l-tree-icon").find("img");
                        parent.children(".l-body").addClass("l-selected");
                        if(parImg.attr("src").indexOf("hover") == -1){
                            var imgSrc = parImg.attr("src");
                            parImg.attr("src",imgSrc.substring(0,imgSrc.length-4)+"_hover.png");
                        }
                    }
                    //切换选中菜单的图标
                    var imgSrc = obj.data.icon;
                    if(imgSrc.indexOf("hover")>0){
                        obj.data.hover = imgSrc;
                    }else{
                        obj.data.hover = imgSrc.substring(0,obj.data.icon.length-4)+"_hover.png";
                        obj.data.icon = obj.data.hover;
                    }
                    $("#ulTree li").eq(obj.data.treedataindex).children(".l-body").find("img").attr("src",obj.data.hover);
                    $.each($(".l-body"), function(index, item) {
                        var self = $(this);
                        if(!self.hasClass("l-selected")){
                            var imgSrcs = self.find("img").attr("src");
                            if(imgSrcs.indexOf("hover") > 0){
                                self.find("img").attr("src",imgSrcs.substring(0,imgSrcs.length-10)+".png");
                            }
                        }
                    });
                },
                onClick:function(obj){
                },
                onBeforeSelect:function(obj){//解决bug 3487【ESB】标准规范：点击标准规范的图标，图标会显示成白色。
                    if(obj.data == null){
                        if(obj.target.parentElement.parentElement.className.indexOf("l-selected") > 0){
                            return false;
                        }
                    }
                },
                //一级节点不可选中
                onAfterAppend: function () {
//                  $.each($("#ulTree li[outlinelevel='1']"), function (index, item) {
//                      $(item).addClass("l-unselectable");
//                  });
                }
            });

            //首页按钮
            $('#btnIndexHome').bind('click', function () {
                me.openHome();
            });
            //退出按钮
            $('#btnIndexLogout').click(function () {
                $.ligerDialog.confirm('是否确认退出！', function (yes) {
                    if (yes) {
                        $.ajax({ //获取表的字段列表
                            type: "POST",
                            url: "${contextRoot}/system/logoutAction",
                            dataType: "json",
                            cache: false,
                            success: function (data) {
                                if (data.successFlg) {
                                    localStorage.removeItem("userRole");
                                    location.href = "${contextRoot}/"+data.data+"/loginPage";
                                }
                                else {
                                    $.ligerDialog.error(data.message);
                                }
                            },
                            error: function (data) {
                                $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                            }
                        });
                    }
                });


            });

            $('#btnIndexPassword').click(function () {
                me.dialog = $.ligerDialog.open({
                    height: 300,
                    width: 600,
                    title: "修改密码",
                    url: '${contextRoot}/user/renewPassword',

                });
            });
            //me.openHome();
            //打开默认菜单
//            me.openMenuByText("任务跟踪");
        },
        //加载中
        loading: function () {
            $('#divIndexContent').html("loading...");
        },
        //首页按钮选择/取消
        clickHomeBtn: function (isClick) {
            try {
                var btn = $("#btnIndexHome");
                if (!isClick) {
                    btn.removeClass('m-index-menu-top-over');
                }
                else {
                    btn.addClass('m-index-menu-top-over');
                }
            }
            catch (e) {
                return;
            }
        },
        //打开首页
        openHome: function () {
            try {
                var me = this;
                me.localNav = [];
                me.localNav.push({text: "主页", url: me.homeUrl});
                me.localRender();
                me.clickHomeBtn(true);
                //取消菜单选中
                $('#ulTree .l-body.l-selected').removeClass("l-selected");

                me.loading();
                $('#divIndexContent').load(me.homeUrl);
            }
            catch (e) {
                return;
            }
        },
        //刷新当前位置栏
        localRender: function () {
            try {
                var me = this;
                var html = "";
                if (me.localNav.length > 0) {
                    for (var i = 0; i < me.localNav.length; i++) {
                        var text = me.localNav[i].text;
                        var url = me.localNav[i].url;
                        if (i == me.localNav.length - 1) //最后一个节点
                        {
                            html += text;
                        }
                        else {// html += "<a class='localNavBtn' href='javascripr:void(0)' url='"+url+"' onclick='indexPage.openMenu()'>"+text + "</a>&nbsp;>&nbsp;";
                            html += "<a class='localNavBtn' href='javascripr:void(0)' url='" + url + "' onclick='indexPage.openMenu()'>" + text + "</a>";
                        }
                    }
                }

                $('#indexNav').html(html);
            }
            catch (e) {
                return;
            }
        },
        //打开菜单（暂不考虑多级菜单）
        openMenu: function (obj) {
            try {
                var me = this;
                var id = obj.data.id;
                var text = obj.data.text;
                var url = obj.data.url;
                me.localNav = [];
                me.localNav.push({id: id, text: text, url: url});
                //父亲节点

                me.localRender();
                me.clickHomeBtn(false);

                if (obj.data.targetType == "1") {
                    me.loadIframePage(url);
                }
                else if (obj.data.targetType == "2") {
                    window.open(url);
                }
                else {
                    me.loadPage(url);
                }
            }
            catch (e) {
                return;
            }
        },
        //弹窗返回
        dialogSuccess: function (message) {

            this.dialog.close();

            $.ligerDialog.success(message);
        },
        //打开子功能页面
        openChildPage: function (text, url, targetType, data, fun) {
            try {
                var me = this;
                me.localNav.push({text: text, url: url, data: data, fun: fun});
                me.localRender();

                if (targetType == "1") {
                    me.loadIframePage(url, data, fun);
                }
                else if (targetType == "2") {
                    window.open(url);
                }
                else {
                    me.loadPage(url, data, fun);
                }
            }
            catch (e) {
                return;
            }

        },
        //加载页面
        loadPage: function (url, data, fun) {
            try {
                var me = this;

                me.loading();
                var div = $("<div class='m-index-content'></div>").load(url, data, function () {
                    if (fun != undefined && typeof fun == 'function') {
                        fun();
                    }
                });
                $('#divIndexContent').html(div);
            }
            catch (e) {
                return;
            }
        },
        //加载iframe页面
        loadIframePage: function (url, data, fun) {
            try {
                var me = this;

                $('#divIndexContent').html("<iframe style='height:98%;width:100%' frameborder='0' src='" + url + "' allowTransparency='true'></iframe>");
            }
            catch (e) {
                return;
            }
        },
        //通过text打开菜单
        openMenuByText: function (text, params, select) {
            try {
                var me = this;

                if (select == false) {
                    var nodeText = $("span", me.menuTree.tree);
                    var url = "";
                    $.each($("div[class='l-body l-selected']", me.menuTree.tree), function () {
                        $(this).removeClass("l-selected");
                    });
                    for (var i = 0; i < nodeText.length; i++) {
                        if ($(nodeText[i]).text().indexOf(text) >= 0) {
                            var id = $(nodeText[i]).closest("li").attr("id");
                            url = $(nodeText[i]).closest("li").attr("url");
                            me.menuTree.expandNode(id);
                            $(nodeText[i]).closest("li").find("div[class='l-body']").addClass("l-selected");
                            break;
                        }
                    }

                    //当前位置栏
                    me.localNav = [];
                    me.localNav.push({id: id, text: text, url: url});
                    me.localRender();
                    me.clickHomeBtn(false);
                    //打开页面
                    debugger
                    me.loadPage(url, params);
                }
                else {
                    //选择节点
                    me.menuTree.queryByText(text, select);
                }

            }
            catch (e) {
                return;
            }
        },
        //刷新当前页面
        refresh: function () {
            try {
                var me = this;
                if (me.localNav.length > 0) {
                    var text = me.localNav[me.localNav.length - 1].text;
                    me.openMenuByText(text);
                }
            }
            catch (e) {
                return;
            }
        }

    }


    $(function () {
        <%--if (window.history && window.history.pushState) {--%>
        <%--$(window).on('popstate', function () {--%>
        <%--var hashLocation = location.hash;--%>
        <%--var hashSplit = hashLocation.split("#!/");--%>
        <%--var hashName = hashSplit[1];--%>
        <%--if (hashName !== '') {--%>
        <%--var hash = window.location.hash;--%>
        <%--if (hash === '') {--%>
        <%--$.ligerDialog.confirm('是否确认后退登出系统！', function (yes) {--%>
        <%--if (yes) {--%>
        <%--$.ajax({ //获取表的字段列表--%>
        <%--type: "POST",--%>
        <%--url: "${contextRoot}/system/logoutAction",--%>
        <%--dataType: "json",--%>
        <%--cache: false,--%>
        <%--success: function (data) {--%>
        <%--if (data.successFlg) {--%>
        <%--localStorage.removeItem("userRole");--%>
        <%--location.href = "${contextRoot}/"+data.data+"/loginPage";--%>
        <%--}--%>
        <%--else {--%>
        <%--$.ligerDialog.error(data.message);--%>
        <%--}--%>
        <%--},--%>
        <%--error: function (data) {--%>
        <%--$.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");--%>
        <%--}--%>
        <%--});--%>
        <%--}--%>
        <%--});--%>
        <%--}--%>
        <%--}--%>
        <%--});--%>
        <%--window.history.pushState('forward', null, './indexPage');--%>
        <%--}--%>

        isLogin();
        indexPage.init();

        //不显示logo横条
        if (${showLogo == "false"}) {
            $(".m-index-top").css("display","none");
            $(".m-index-top").height(0);
            $(".l-layout-left").css("top","40px");
            $(".l-layout-center").css("top","40px");
        }
    });
</script>

