var i;
var charts = null; // 定义全局变量
var picHeight = $(window).height() * 0.65;
var datalist = []
var devIndex = getQueryString("devIndex")
var devname = getQueryString("devName")
var gdName = getQueryString("gdName")
var bdName = getQueryString("bdName")

var websocket = window.parent.parent.webSocket


$(function () {
    $("#run_info").css('height', picHeight);
    $("#info_div").css('height', picHeight * 0.5);
    var html = '所选设备: ' + gdName + '->' + bdName + '->' + devname
    $('#info').html(html)

    window.setInterval(function () {
        $('#timeid').text('当前系统时间：' + getNowFormatDate())
    }, 1000)

    $.ajax({
        type: 'post',
        dataType: "json",
        url: '/pqs9900/terminalmanger/getQuailty',
        data: {devIndex: devIndex},
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER()
        },
        success: function (data) {
            if (data.code === 200) {
                //事件委托给父级div来处理
                initCpuRun()
                $(".tab-item").hide();
                $("#tab-item1").show();

            } else {
                layer.msg(data.message, {icon: 2, time: 2000})
            }
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    })

})


var heart = null;
websocket.onmessage = function (evnt) {
    console.log(evnt)
    if (evnt.data != "") {
        var info = JSON.parse(evnt.data)
        if (info.type == 190) {
            if (info.resultcode == 10000) {
                if (info.resultmsg == "功能已关闭") {
                    layer.msg('此功能已关闭', {icon: 2}, function () {
                        var aa = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(aa);
                    })
                }
                if (charts != null) {
                    //cpu
                    var x = (new Date()).getTime() // current time
                    var series0 = charts.series[0];

                    let cc = info.data.cpu
                    let cpuArr = cc.split('_')
                    let ycpu = null;
                    if (cpuArr.length > 1) {
                        ycpu = cpuArr[0]
                    } else {
                        ycpu = cc
                    }
                    if (series0.points.length > 20) {
                        for (var i = 20; i < series0.length; i++) {
                            series0[series0.points.length - 20].remove();
                        }

                    }

                    let y = Number(ycpu);
                    series0.addPoint([x, y], true, true);

                    //memory
                    var series1 = charts.series[1];
                    if (series1.points.length > 20) {
                        for (var i = 20; i < series1.length; i++) {
                            series1[series1.points.length - 20].remove();
                        }

                    }
                    let y1 = parseInt(Number(info.data.Memory) / Number(info.data.Total_Memory) * 100)
                    series1.addPoint([x, y1], true, true);

                    //disk
                    var series2 = charts.series[2];
                    if (series2.points.length > 20) {
                        for (var i = 20; i < series2.length; i++) {
                            series2[series2.points.length - 20].remove();
                        }

                    }

                    let y2 = parseInt(Number(info.data.Disk) / Number(info.data.Total_Disk) * 100)
                    series2.addPoint([x, y2], true, true);

                    let usec = Number(info.data.cpu)
                    let duoc = 100 - usec
                    $('.span0').eq(0).text(usec + ' %')
                    $('.span0').eq(1).text(duoc + ' %')


                    let total = Number(info.data.Total_Memory)
                    let use = Number(info.data.Memory)
                    let duo = (Number(info.data.Total_Memory) - Number(info.data.Memory)).toFixed(2)
                    $('.span1').eq(0).text(total + ' M')
                    $('.span1').eq(1).text(use + ' M')
                    $('.span1').eq(2).text(duo + ' M')

                    let btotal = Number(info.data.Total_Disk)
                    let buse = Number(info.data.Disk)
                    let bduo = (Number(info.data.Total_Disk) - Number(info.data.Disk)).toFixed(2)
                    $('.span2').eq(0).text(btotal + ' G')
                    $('.span2').eq(1).text(buse + ' G')
                    $('.span2').eq(2).text(bduo + ' G')

                    if (heart == null) {
                        heart = window.setInterval(function () {
                            $.ajax({
                                type: 'post',
                                dataType: "json",
                                url: '/pqs9900/terminalmanger/heartSocket',
                                data: {devIndex: devIndex},
                                success: function (data) {

                                }
                            })
                        }, 18000)
                    }

                }
            } else if (info.resultcode == 10009) {
                //流量超过关闭功能
                layer.msg('此功能已关闭', {icon: 2}, function () {
                    var aa = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(aa);
                })
            } else if (info.resultcode == 20000) {
                layer.msg('未知异常', {icon: 2}, function () {
                    var aa = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(aa);
                })
            } else if (info.resultcode == 10003) {
                layer.msg("JSON字符串解析出错", {
                    icon: 5,
                    time: 3000
                });
            } else if (info.resultcode == 10004) {
                layer.msg("socket处理出现异常", {
                    icon: 5,
                    time: 3000
                });
            } else if (info.resultcode == 10005) {
                layer.msg("未找到ID为对应的设备", {
                    icon: 5,
                    time: 3000
                });
            }
        }
    }
}


//初始化cpu运行情况
function initCpuRun() {

    Highcharts.setOptions({
        global: {
            useUTC: false
        }
    });
    charts = Highcharts.chart('run_info', {
        chart: {
            backgroundColor: canvasBG,
            borderWidth: 1,
            plotBorderWidth: 1,
            borderColor: canvasBG
        },
        colors: [red],
        plotOptions: {

            series: {
                lineWidth: 1.5,
                marker: {
                    enabled: false
                }
            }
        },
        title: {
            text: '终端性能详情',
            style: {
                fontWeight: 'bold',
                fontSize: '13px',
                color: '#303030'

            },
            align: 'center'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 100,
            tickInterval: 10000
        },
        yAxis: {
            max: 100,
            min: 0,
            title: {
                text: '使用率(%)'
            },
            showLastLabel: true,
            opposite: false, //y轴显示对立面
        },
        scrollbar: {
            enabled: true
        },
        legend: {
            enabled: true
        },
        tooltip: {
            split: false
        },
        credits: {
            enabled: false
        },
        exporting: {
            enabled: true,
            buttons: {
                contextButton: {
                    enabled: true,
                    align: 'right'
                }

            }
        },
        series: [{
            name: 'cpu使用率',
            data: (function () {
                var data = [], time = (new Date()).getTime(), i;
                for (i = -59; i <= 0; i += 3) {
                    data.push([
                        time + i * 1000,
                        null
                    ]);
                }
                return data;
            }())
        }, {
            name: '内存使用率',
            color: 'green',
            data: (function () {
                // generate an array of random data
                var data = [], time = (new Date()).getTime(), i;
                for (i = -59; i <= 0; i += 3) {
                    data.push([
                        time + i * 1000,
                        null
                    ]);
                }
                return data;
            }())
        }, {
            name: '磁盘使用率',
            color: orange,
            data: (function () {
                // generate an array of random data
                var data = [], time = (new Date()).getTime(), i;
                for (i = -59; i <= 0; i += 3) {
                    data.push([
                        time + i * 1000,
                        null
                    ]);
                }
                return data;
            }())
        }]
    });
}


//获取当前时间
function getNowFormatDate() {
    var date = new Date();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + '年' + month + '月' + strDate
        + "日" + date.getHours() + '时' + date.getMinutes()
        + '分' + date.getSeconds() + '秒';
    return currentdate;
}


