var $startTime=$("#startTime");
var $endTime=$("#endTime");
var $statistic=$("#statistic");
var $monitorNature = $("#monitorNature")
var tiggleValue;
var buttonname;
var companyname;
var dataTable=null;
$("#interval").val("月份");

$(function () {
    var startTime = $startTime.eq(0).val();
    var endTime = $endTime.eq(0).val();
    var statistic = $statistic.eq(0).val();
    var monitorNature = $monitorNature.eq(0).val();
    $.ajax({
        type: "POST",
        data:{
            start: startTime,
            end: endTime
        },
        async:false,
        url: "/pqs9000/device/getMsgInfo",
        dataType: 'json',
        success: function (data) {
            buttonname = data.body.name;
            tiggleValue = data.body.name;
        }
    })

    $.ajax({
        type: "POST",
        url: "/pqs9000/theme/getThemeInfo",
        async: false,
        dataType: 'json',
        success: function (data) {
            companyname = data.body.name;
        }
    })

    //初始化按钮开关
    $(".checkbox").bootstrapSwitch({
        onText: buttonname,      // 设置ON文本
        offText: companyname,    // 设置OFF文本
        onColor: "primary",// 设置ON文本颜色     (info/success/warning/danger/primary)
        offColor: "primary",  // 设置OFF文本颜色        (info/success/warning/danger/primary)
        size: "small",    // 设置控件大小,从小到大  (mini/small/normal/large)
        handleWidth: "60",//设置控件宽度
        // 当开关状态改变时触发
        onSwitchChange: function (event, state) {
            if (state === true) {
                tiggleValue = buttonname;
                refresh(tiggleValue);
            } else {
                tiggleValue = companyname;
                refresh(tiggleValue);
            }
        }
    });
    initTable(null);
    refresh(tiggleValue);
})

//点击确定，去后台请求参数
$("#selectData").click(function() {
    refresh(tiggleValue);
});


//获取表格数据
function refresh(tiggleValue) {
    var startTime = $startTime.eq(0).val();
    var endTime = $endTime.eq(0).val();
    var statistic = $statistic.eq(0).val();
    let statFlag = $('#allLineCheck').is(":checked")
    var monitorNature = $monitorNature.eq(0).val();
    console.log(statistic)
    console.log(monitorNature)
    $.ajax({
        type: "POST",
        url: "/pqs9000/responsibility/overLimitData",
        dataType:'json',
        data : {
            startTime : startTime,
            endTime : endTime,
            type: tiggleValue,
            statistic: statistic,
            statFlag:statFlag,
            monitorNature: monitorNature
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success:function (data) {
            var sortData = bubbleSort(data.body);
            initTable(sortData);
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    })
}

function initTable(data){
    var size = 0
    if (data != null){
        size = data.length;
    }
    var height=$(window).height()-170;
    var statistic = $statistic.eq(0).val();
    dataTable=$("#overLimitTable").DataTable();
    dataTable.clear().draw();
    if (statistic === "0"){
        dataTable=$('#overLimitTable').DataTable({
            order:[],
            columnDefs : [
                {
                    orderable : false,
                    targets : [0,1]
                }
            ],
            columns: [
                // 序号
                {width:"2%",data: null, render : function(data, type, full, meta){
                        if (size === 1){
                            return meta.row + 1 + meta.settings._iDisplayStart;
                        }else {
                            if (meta.row + 1 + meta.settings._iDisplayStart === 1){
                                return "";
                            } else {
                                return meta.row + meta.settings._iDisplayStart;
                            }
                        }
                    }},
                {width:"2%",data:"name",render : function(data, type, full, meta){
                        var value=data;
                        if(data.length>10){
                            value=data.substring(0,9)+"...";
                            value="<span title="+data+">"+value+"</span>";
                        }
                        return value;
                    }},
                {width:"3%",data:"onlineMonitorNumber"},
                {width:"3%",data:"overLimitMonitorNumber"},
                {width:"3%",data:"overBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"frequencyMonitorNumber"},
                {width:"2.5%",data:"frequencyBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"frequencyAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"frequencyOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"voltageMonitorNumber"},
                {width:"2.5%",data:"voltageBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"voltageAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"voltageOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicVoltageMonitorNumber",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicVoltageBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicVoltageAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"harmonicVoltageOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicCurrentMonitorNumber"},
                {width:"2.5%",data:"harmonicCurrentBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicCurrentAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"harmonicCurrentOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"threePhaseVoltageMonitorNumber"},
                {width:"2.5%",data:"threePhaseVoltageBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"threePhaseVoltageAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"threePhaseVoltageOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"flickerMonitorNumber"},
                {width:"2.5%",data:"flickerBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"flickerAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"flickerOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"negativeMonitorNumber"},
                {width:"2.5%",data:"negativeBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"negativeAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"negativeOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"interharmonicMonitorNumber"},
                {width:"2.5%",data:"interharmonicBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"interharmonicAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"interharmonicOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
            ],
            bAutoWidth:false,
            serverSide : false,
            destroy : true,
            data : data,
            lengthChange: false,
            searching: true,
            dom: 'rtip',
            sScrollY: height,
            scrollX: true,
            lengthMenu : [18],
            language : {
                url:'/pqs9000/json/chine.json'
            },
            fnDrawCallback: function(oTable) {
                $("#overLimitTable_info").addClass("fl");
                $("#overLimitTable_paginate").addClass("fr");
                $("#overLimitTable_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");
                var table = $("#overLimitTable").dataTable();
                $('#dataTable-btn').click(function(e) {
                    if($("#changePage").val() && $("#changePage").val() > 0) {
                        var redirectpage = $("#changePage").val() - 1;
                    } else {
                        var redirectpage = 0;
                    }
                    table.fnPageChange(redirectpage);
                });
            }
        });
    }else {
        dataTable=$('#overLimitTable').DataTable({
            order:[],
            columnDefs : [
                {
                    orderable : false,
                    targets : [0,1]
                }
            ],
            columns: [
                // 序号
                {width:"1.5%",data: null, render : function(data, type, full, meta){
                        return meta.row + 1 + meta.settings._iDisplayStart;
                    }},
                {width:"2.5%",data:"name",render : function(data, type, full, meta){
                        var value=data;
                        if(data.length>10){
                            value=data.substring(0,9)+"...";
                            value="<span title="+data+">"+value+"</span>";
                        }
                        return value;
                    }},
                {width:"3%",data:"onlineMonitorNumber"},
                {width:"3%",data:"overLimitMonitorNumber"},
                {width:"3%",data:"overBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"frequencyMonitorNumber"},
                {width:"2.5%",data:"frequencyBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"frequencyAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"frequencyOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"voltageMonitorNumber"},
                {width:"2.5%",data:"voltageBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"voltageAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"voltageOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicVoltageMonitorNumber",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicVoltageBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicVoltageAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"harmonicVoltageOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicCurrentMonitorNumber"},
                {width:"2.5%",data:"harmonicCurrentBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"harmonicCurrentAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"harmonicCurrentOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"threePhaseVoltageMonitorNumber"},
                {width:"2.5%",data:"threePhaseVoltageBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"threePhaseVoltageAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"threePhaseVoltageOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"flickerMonitorNumber"},
                {width:"2.5%",data:"flickerBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"flickerAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"flickerOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"negativeMonitorNumber"},
                {width:"2.5%",data:"negativeBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"negativeAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"negativeOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"interharmonicMonitorNumber"},
                {width:"2.5%",data:"interharmonicBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"2.5%",data:"interharmonicAvg",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
                {width:"3%",data:"interharmonicOverDayBiLi",render : function(data, type, full, meta){
                        var value= toDecimal2(data);
                        return value;
                    }},
            ],
            bAutoWidth:false,
            serverSide : false,
            destroy : true,
            data : data,
            lengthChange: false,
            searching: true,
            dom: 'rtip',
            sScrollY: height,
            scrollX: true,
            lengthMenu : [18],
            language : {
                url:'/pqs9000/json/chine.json'
            },
            fnDrawCallback: function(oTable) {
                $("#overLimitTable_info").addClass("fl");
                $("#overLimitTable_paginate").addClass("fr");
                $("#overLimitTable_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");
                var table = $("#overLimitTable").dataTable();
                $('#dataTable-btn').click(function(e) {
                    if($("#changePage").val() && $("#changePage").val() > 0) {
                        var redirectpage = $("#changePage").val() - 1;
                    } else {
                        var redirectpage = 0;
                    }
                    table.fnPageChange(redirectpage);
                });
            }
        });
    }
}

//点击导出功能
$('#exportTable').click(function () {
    var startTime = $startTime.eq(0).val();
    var endTime = $endTime.eq(0).val();
    var statistic = $statistic.eq(0).val();
    $("<form method='post' action='/pqs9000/responsibility/exportAreaExcel?statistic="+statistic+"&startTime="+startTime+"&endTime="+endTime+"'>"+
        "<input type='hidden'/>"+
        encodeURIComponent("excelname")+"'/></form>").appendTo('body').submit().remove();
});

//制保留2位小数，如：2，会在2后面补上00.即2.00
function toDecimal2(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x*100)/100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

//排序
function bubbleSort(data) {
    for (var i=0;i<data.length-1;i++){
        for (var j=0;j<data.length-1-i;j++){
            if (data[j].onlineMonitorNumber<data[j+1].onlineMonitorNumber){
                var temp = data[j+1];
                data[j+1] = data[j];
                data[j] = temp;
            }
        }
    }
    return data
}
