$(document).ready(function () {
    if (window.top != window.self) {
        window.top.location.href = "/sso";
    }
});
var tabs;
var webSocket;
$(function () {
    $('#rightContent').width(document.body.clientWidth - 290).height(document.body.offsetHeight - 115);
    tabs = $('#tabs').cleverTabs();
    $(window).bind('resize',
        function () {
            tabs.resizePanelContainer();
            $('#rightContent').width(document.body.clientWidth - $('#left-menu').width() - 35).height(document.body.offsetHeight - 115);
        });

   if (getRole("/pqs9000/business/overview") == 1) {
        addTab('overview', '概览');
    } else if (getRole("/pqs9000/business/monitor") == 1) {
        addTab('monitor', '在线监测点');
    } else if (getRole("/pqs9000/business/offlinemonitor") == 1) {
        addTab('offlinemonitor', '离线监测点');
    } else if (getRole("/pqs9000/area/quality") == 1) {
        addTab('/pqs9000/area/quality', '电能质量评估');
    } else if (getRole("/pqs9000/area/steady") == 1) {
        addTab('/pqs9000/area/steady', '稳态指标合格率');
    } else if (getRole("/pqs9000/area/status") == 1) {
        addTab('/pqs9000/area/status', '终端状态');
    } else if (getRole("/pqs9000/area/onlinerate") == 1) {
        addTab('/pqs9000/area/onlinerate', '终端在线率');
    } else if (getRole("/pqs9000/area/integrality") == 1) {
        addTab('/pqs9000/area/integrality', '数据完整性');
    } else if (getRole("/pqs9000/area/vdlist") == 1) {
        addTab('/pqs9000/area/vdlist', '暂降事件列表');
    } else if (getRole("/pqs9000/business/norm") == 1) {
        addTab('norm', '稳态趋势对比');
    } else if (getRole("/pqs9000/business/harmonic") == 1) {
        addTab('harmonic', '历史谐波频谱');
    } else if (getRole("/pqs9000/responsibility/responsibilitylist") == 1) {
        addTab('/pqs9000/responsibility/responsibilitylist', '谐波责任划分列表');
    } else if (getRole("/pqs9000/business/runtime") == 1) {
        addTab('runtime', '终端运行情况');
    } else if (getRole("/pqs9000/area/onlineratetable") == 1) {
        addTab('/pqs9000/area/onlineratetable', '在线率列表');
    } else if (getRole("/pqs9000/area/integralitytable") == 1) {
        addTab('/pqs9000/area/integralitytable', '完整性列表');
    } else if (getRole("/pqs9000/area/steadytable") == 1) {
        addTab('/pqs9000/area/steadytable', '稳态合格率列表');
    } else if (getRole("/pqs9000/area/harmonicrate") == 1) {
        addTab('/pqs9000/area/harmonicrate', '谐波总畸变率统计');
    } else if (getRole("/pqs9000/business/excel") == 1) {
        addTab('excel', 'Excel报表');
    } else if (getRole("/pqs9000/business/expmodel") == 1) {
        addTab('expmodel', 'Word报告');
    } else if (getRole("/pqs9000/business/sourcemanage") == 1) {
        addTab('sourcemanage', '资源管理');
    } else if (getRole("/pqs9000/responsibility/areaovernoraml") == 1) {
        addTab('/pqs9000/responsibility/areaovernoraml', '区域稳态指标超标');
    } else if (getRole("/pqs9000/responsibility/warningsubstation") == 1) {
        addTab('/pqs9000/responsibility/warningsubstation', '告警变电站明细');
    } else if (getRole("/pqs9000/responsibility/warningmonitor") == 1) {
        addTab('/pqs9000/responsibility/warningmonitor', '监测点稳态超标');
    }
    $('#left-menu').remove();
    $('#rightContent').width(
        document.body.clientWidth - $('#left-menu').width() - 35).height(
        document.body.offsetHeight - 100).css({
        'left': '15px',
        'right': '20px'
    });

    startWebSocket();
});

// 新增tab页
function addTab(url, label) {
    tabs.add({url: url, label: label});
    if (url == 'jcd.html') {
        $('#left-menu').remove();
        $('#rightContent').width(
            document.body.clientWidth - $('#left-menu').width() - 35)
            .height(document.body.offsetHeight - 115).css({
            'left': '15px',
            'right': '20px'
        });
    }
}

var actLineIndex;
var thisUrl;
//扩展
function addTabs(url, label) {
    tabs.add({url: url, label: label,callback: function () {
        if(thisUrl){
            console.log(thisUrl)
            if(thisUrl == 'actualData'){
                $.ajax({
                    type: 'get',
                    dataType: "json",
                    url: '/pqs9000/device/closeRealTimeData',
                    data: {
                        lineIndex: actLineIndex
                    },
                    success: function (data) {
                    }
                })
            }
        }

        } });

}



/*
 * 主页面中其他功能如： 用户登出等
 */
$('.menu-slidedown').find('li').click(function () {
    $(this).addClass('hover').siblings().removeClass('hover');
    $(this).parents('.menu-con').siblings().find('li').removeClass('hover');
})
$('.menu-con').find('.menu-hd').click(function () {
    var menucon = $(this).parents('.menu-con');
    if (menucon.hasClass('hover')) {
        menucon.removeClass('hover');
    } else {
        menucon.addClass('hover').siblings('.menu-con').removeClass('hover')
    }
})

$("#destory").click(function () {
    layer.confirm('确定退出吗？', {
        btn: ['确定', '取消']
    }, function (index) {
        location.href = '/pqs9000/user/loginout';
        layer.close(index)

    }, function () {

    });
});
///登录用户详情及修改密码
$("#selectUserUpdatePass").click(function () {
    var userIndex =document.getElementById("user_index").value.split("/")[0];
    layer.open({
        type: 2,
        title: '用户详情',
        maxmin: false,
        shadeClose: true, // 点击遮罩关闭层
        area: ['35%', '32%'],
        content: 'userDetail?userIndex='
            + userIndex,
        scrollbar: false
    });
});

var length = 0;
var num = 0;

function goLeft() {
    // 750是根据你给的尺寸，可变的
    if (num == length) {
        num = 0;
    }
    num -= 1;
    $(".scroll").css({
        left: num
    })
}

function startWebSocket() {
    $.ajax({
        type: 'POST',
        dataType: "text",
        url: '/pqs9000/device/websocketon',
        async: true,
        success: function (data) {
            if (data != "" || data != null) {
                var userName = $("#user_name").val();
                webSocket = new WebSocket(data + userName);
                webSocket.onerror = function (event) {
                    layer.msg("websSocket错误信息:" + event.data);
                }
                webSocket.onopen = function () {
                    layer.msg("webSocket连接成功!", {
                        time: 404
                    });
                }
                webSocket.onclose = function () {
                    webSocket.close();
                    layer.msg("webSocket关闭!");
                }
            }
        }
    })
}
