<%@ 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: "POST",
        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;
            //主布局
            $("#divIndexMain").ligerLayout({
                space: 0,
                topHeight: 120,
                leftWidth: 220,
                allowLeftCollapse: false,
                allowLeftResize: false,
                allowTopResize: false
            });

            $(".l-layout-left").css({background: "#dce6f0"})

            var tenantManager = [
                {id: 200, text: '租户管理', icon: '${staticRoot}/images/index/menu5_icon.png'},
                {id: 201, pid: 200, text: '租户管理', url: '${contextRoot}/tenant/initial'},
            ]
            //菜单列表
            var menu = [
                //标准管理
                //标准规范中心
                {id: 2, text: '标准规范', icon: '${staticRoot}/images/index/menu3_icon.png'},
                {id: 21, pid: 2, text: '平台标准', url: '${contextRoot}/integration/initial/standard'},
                {id: 22, pid: 2, text: '应用标准', url: '${contextRoot}/integration/initial/application'},
                {id: 23, pid: 2, text: '适配方案', url: '${contextRoot}/adapterPlan/initial'},
                //服务管理中心
                {id: 6, text: '服务管理', icon: '${staticRoot}/images/index/menu5_icon.png'},
                {id: 61, pid: 6, text: '服务管理', url: '${contextRoot}/app/initial'},
                //数据服务总线
                {id: 1, text: '服务总线', icon: '${staticRoot}/images/index/menu2_icon.png'},
                {id: 11, pid: 1, text: '流程编排', url: '${contextRoot}/flow/initial'},
                {id: 12, pid: 1, text: '任务配置', url: '${contextRoot}/datacollect/configJob'},
                {id: 13, pid: 1, text: '采集编排', url: '${contextRoot}/crawler/jobLayout'},
                {id: 14, pid: 1, text: '流程编辑', url: '${contextRoot}/process/initial'},
                //运行监控中心
                {id: 5, text: '运行监控', icon: '${staticRoot}/images/index/menu5_icon.png'},
                {id: 51, pid: 5, text: '系统监控', url: '${contextRoot}/monitor/server/initial'},
                {id: 52, pid: 5, text: '服务监控', url: '${contextRoot}/monitor/service/initial'},
                {id: 53, pid: 5, text: '任务跟踪', url: '${contextRoot}/datacollect/trackJob', targetType: '1'},
                {id: 54, pid: 5, text: '任务补采', url: '${contextRoot}/datacollect/repeatDatacollect'},
                //资源管理
                {id: 3, text: '资源服务', icon: '${staticRoot}/images/index/menu4_icon.png'},
                {id: 31, pid: 3, text: '资源注册', url: '${contextRoot}/resource/resource/initial'},
                {id: 32, pid: 3, text: '资源浏览', url: '${contextRoot}/resource/resourcePage'},
                {id: 34, pid: 3, text: '资源分类', url: '${contextRoot}/resource/rsCategory/initial'},
                {id: 35, pid: 3, text: '业务资源', url: '${contextRoot}/resourceRest/initial'},
                //维度管理
                <%--{id: 4, text: '维度管理', icon: '${staticRoot}/images/index/menu5_icon.png'},--%>
                {id: 36, pid: 3, text: '维度配置', url: '${contextRoot}/dimension/dimension'},
                {id: 37, pid: 3, text: '维度类别配置', url: '${contextRoot}/dimension/dimensioncatetory'},
                //用户安全中心
                {id: 7, text: '用户安全', icon: '${staticRoot}/images/index/menu5_icon.png'},
                {id: 71, pid: 7, text: '机构管理', url: '${contextRoot}/org/initial'},
                {id: 72, pid: 7, text: '用户管理', url: '${contextRoot}/user/initial'},
                {id: 73, pid: 7, text: '角色管理', url: '${contextRoot}/role/initial'},
                {id: 74, pid: 7, text: '权限管理', url: '${contextRoot}/authority/initial'},
                //系统配置
                {id: 9, text: '系统配置', icon: '${staticRoot}/images/index/menu6_icon.png'},
                {id: 91, pid: 9, text: '系统参数', url: '${contextRoot}/param/initial'},
                {id: 92, pid: 9, text: '字典管理', url: '${contextRoot}/dict/initial'},
                {id: 93, pid: 9, text: '菜单配置', url: '${contextRoot}/menu/initial'},
                {id: 94, pid: 9, text: '菜单按钮配置', url: '${contextRoot}/menu/menuAction/initial'},
                {id: 95, pid: 9, text: '数据源配置', url: '${contextRoot}/datasource/configSources'},
            ];
            var userRole = localStorage.getItem("userRole");
            if(userRole=="admin"){
                //是管理中心用户，则添加租户管理模块
                menu = menu.concat(tenantManager);
            }
            me.menuTree = $('#ulTree').ligerTree({
                data: menu,
                idFieldName: 'id',
                parentIDFieldName: 'pid',
                checkbox: false,
                treeLine: false,
                autoCheckboxEven: false,
                needCancel: false,
                btnClickToToggleOnly: false,
                slide: false,
                nodeDraggable: false,
                isExpand: false,
                parentIcon: true,
                nodeWidth: 176,
                onSelect: function (obj) {
                    if (obj.data.url != undefined) {
                        me.openMenu(obj);
                    }
                },
                //一级节点不可选中
                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);
                    //打开页面

                    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();
    });
</script>

