function formatDate(now) { 
	var date = new Date(now);// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
			.getMonth() + 1)
			+ '-';
	var D = change(date.getDate());
	return Y + M + D;
} 

function change(t) {
	if (t < 10) {
		return "0" + t;
	} else {
		return t;
	}
}

layui.use(['element'], function () {
    $('.site-demo-active').on('click', function () {
        var othis = $(this), type = othis.data('type');
        active[type] ? active[type].call(this, othis) : '';
    });

    document.getElementById("myTab").style.height = document.body.clientHeight - document.getElementById("myLi").offsetHeight - 31 + "px";

});

function setData(dom, minDate, maxDate) {
    dom.daterangepicker({
        timePicker: true, //是否显示小时和分钟
        //showDropdowns: true,
        autoUpdateInput: false,
        minDate: minDate,	//最小时间
        maxDate: maxDate, //最大时间
        timePickerIncrement: 30,
        timePicker24Hour: true, //是否使用小时制来显示时间
        opens: 'right', //日期选择框的弹出位置
        locale: {
            format: "YYYY-MM-DD HH:mm:ss",
            applyLabel: '确定',
            cancelLabel: '取消',
            separator: ' 至 ',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'],
            firstDay: 1
        }
    });
}

function clearData() {
    clear("first");
    clear("second");
    clear("thrid");
    disable("second", true);
    disable("thrid", true);
}

function clear(value) {
    $('#' + value).val("");
}

function disable(value, b) {
    b ? $('#' + value).prop("disabled", b) : $('#' + value).removeProp("disabled");
}

$(function () {
	var Time = $("#time").val();
	$("#startTime").val(formatDate(Time));

    getTableData();

    //绑定事件
    $("#startTime,#endTime,#interval").bind("input propertychange change", function (event) {
        document.getElementById("check").checked = false;
        $("#timeSet").prop("disabled", "disable");
        clearData();//清空内容  具体需求根据测试结果调整
    });

    //是否配置检修时间
    $("#check").click(function () {
        var state = document.getElementById("check").checked;

        if (state) { //控制检修时间按钮可操作
            $("#timeSet").removeProp("disabled");
        } else {
            $("#timeSet").prop("disabled", "disable");
            clearData();//清空内容  具体需求根据测试结果调整
        }
    });

    $("#query").click(function () {
        getTableData();
    });

    $("#export").click(function () {
        $.post("/pqs9300/createExcel", {"pointInfo": JSON.stringify(pointInfo)}, function () {
            window.location.href = "/pqs9300/exportExcel";
        }, "json");
    });

    disable("second", true);
    disable("thrid", true);

    $('#timeSet').click(function () {
        $('#timeModel').modal();

        var endDate = getValue("endTime") + " 00:00";
        setData($('#first'), getValue("startTime") + " 00:00", endDate);

        $("#first").on('apply.daterangepicker', function (ev, picker) {
            clear("second");
            clear("thrid");
            disable("second", false);
            disable("thrid", true);
            setData($('#second'), picker.endDate.format('YYYY-MM-DD HH:mm'), endDate);

            $("#second").on('apply.daterangepicker', function (ev, picker) {
                clear("thrid");
                disable("thrid", false);
                setData($('#thrid'), picker.endDate.format('YYYY-MM-DD HH:mm'), endDate);
            });
        });
    });
});

//缓存数据，方便切换指标，界面更新显示
var voltageData;
var powerData;
var flickerData;
var distortionData;
var vdeviationData;
var frequencyData;
var threephaseData;
var currentData;
var voltageRateData;
var ploysetting;
var fpyData;
var ployValue = "##";

//监听模板下拉框选中事件传值到后台
function changeEvent(value) {
    ployValue = value;

    $.post('/pqs9300/setPloy', {
        'guid': value
    }, function (data) {
        ploysetting = data;
        drawVoltageTable(voltageData);
        drawDistortionTable(distortionData);
        drawFlickerTable(flickerData);
        drawFrequencyTable(frequencyData);
        drawPowerTable(powerData);
        drawThreephaseTable(threephaseData);
        drawVoltageRateTable(voltageRateData);
        drawICurrentTable(currentData);
        drawVdeviationTable(vdeviationData);
        initFpyTable(fpyData);
    });
}

function judge(value, b, v) {
    if (ployValue === "##") {
        var len = b ? (pointOverLimit.list.length == 2 ? 12 : 16) : 8;
        len = (value === "功率" && pointOverLimit.mode != 0 ? 8 : len);

        return {"header": value, "phase": b, 'max': 1, 'min': 1, 'avg': 1, 'cp95': 1, 'length': len, 'showPass': true};
    }

    for (var i = 0; i < ploysetting.body.length; i++) {
        if (value === ploysetting.body[i].name) {
            return {
                "header": value, "phase": b,
                'max': getPloySettingValue(ploysetting.body[i].listTarget, 1),
                'min': getPloySettingValue(ploysetting.body[i].listTarget, 2),
                'avg': getPloySettingValue(ploysetting.body[i].listTarget, 3),
                'cp95': getPloySettingValue(ploysetting.body[i].listTarget, 4),
                'length': 4 + getLength(ploysetting.body[i].listTarget, value === "功率" && pointOverLimit.mode != 0 ? false : b),
                'showPass': showPass(v, ploysetting.body[i].listTarget)
            }
        }
    }

    return {"header": null};
}

function getPloySettingValue(arr, v) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == v) {
            return 1;
        }
    }

    return 0;
}

//v : 1 max
//v : 2 min ....
function showPass(v, arr) {
    if (v != -1) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] === v) {
                return true;
            }
        }
    }

    return false;
}

function getLength(le, b) {
    var length = 0;
    length += le.length;

    return b ? length * (pointOverLimit.list.length == 2 ? 2 : 3) : length;
}

var pointInfo = {};
/***************************************************************
 * 控制load显示
 ***************************************************************/
var TOTAL = 11; ///一共查询11个指标
var count = 0;
var layerId = null;

function counter() {
    count++;

    if (TOTAL === count) {
        //关闭layer的loading加载
        count = 0;
        layer.close(layerId);
    }
}

var pointOverLimit;

function getTableData() {
    var pointNode = $("#lineIndex").val();

    //loading加载
    layerId = layer.load();

    var obj = new Object();

    var list = new Object();
    list.point = pointNode;
    var aoData = new Array();
    aoData.push(list);
    $.ajax({
        type: 'POST',
        async: false,
        url: '/pqs9300/technical/getPttype',
        data: {
            obj: JSON.stringify(aoData)
        },
        success: function (data) {
            obj.mode = data[0];
        }
    })
    
    //这样配置后台不需要判断是否有检修时间配置属性
    obj.startTime = getValue("startTime");
    obj.endTime = getValue("startTime");
    obj.first = "";
    obj.second = "";
    obj.thrid = "";
    obj.state = false;
    obj.pointIndex = pointNode;

    $.post("/pqs9300/getOverLimitData", {
        timeObj: JSON.stringify(obj)
    }, function (data) {
        if (200 === data.code) {
            if (true === $("#export").prop("disabled")) {
                $("#export").removeProp("disabled");
            }

            getPointInfo(obj, pointNode);
            pointOverLimit = data.body;
        } else {
            layer.close(layerId);
            layer.msg("获取监测点限值失败.");
            $("#export").prop("disabled", "disable");
        }
    }, "json");
}

function initTableTD(data, json, jsonObj) {
    var code = '';
    var k = (pointOverLimit.mode != 0 && json.header === '三相电压不平衡度' ? 4 : 0);
    k = (pointOverLimit.mode != 0 && json.header === '有效值' ? 1 : k);

    for (var i = k; i < jsonObj.data.length; i++) {
        if (jsonObj.data[i].name == '线电压') {
            code += initheader(json, 1);
        } else if (jsonObj.data[i].name == '电流总谐波畸变率' && pointOverLimit.mode != 0) {
            code += initheader(json, 0);
        }

        code += '<tr><td class="text-center">' + jsonObj.data[i].name + '</td><td class="text-center">' + jsonObj.data[i].unit + '</td>';

        code += intiTableData(data, i, json, jsonObj.data[i].name);
    }

    return code;
}

function initheader(jsonObj, num) {
    if (jsonObj.header == null) {
        return null;
    }

    if (jsonObj.phase) {
        var code = '<tr><td></td><td></td>';

        if (jsonObj.max === 1) {
            code += '<td class="text-center">' + (num === 1 ? 'AB' : 'A') + '</td><td class="text-center">' + (num === 1 ? 'BC' : 'B') + '</td>';

            if (pointOverLimit.mode != 2 && num === 1) {
                code += '<td class="text-center">CA</td>';
            } else if (num === 0) {
                code += '<td class="text-center">C</td>';
            } else {
                code += '<td class="text-center">-</td>';
            }
        }

        if (jsonObj.min === 1) {
            code += '<td class="text-center">' + (num === 1 ? 'AB' : 'A') + '</td><td class="text-center">' + (num === 1 ? 'BC' : 'B') + '</td>';

            if (pointOverLimit.mode != 2 && num === 1) {
                code += '<td class="text-center">CA</td>';
            } else if (num === 0) {
                code += '<td class="text-center">C</td>';
            } else {
                code += '<td class="text-center">-</td>';
            }
        }

        if (jsonObj.avg === 1) {
            code += '<td class="text-center">' + (num === 1 ? 'AB' : 'A') + '</td><td class="text-center">' + (num === 1 ? 'BC' : 'B') + '</td>';

            if (pointOverLimit.mode != 2 && num === 1) {
                code += '<td class="text-center">CA</td>';
            } else if (num === 0) {
                code += '<td class="text-center">C</td>';
            } else {
                code += '<td class="text-center">-</td>';
            }
        }

        if (jsonObj.cp95 === 1) {
            code += '<td class="text-center">' + (num === 1 ? 'AB' : 'A') + '</td><td class="text-center">' + (num === 1 ? 'BC' : 'B') + '</td>';

            if (pointOverLimit.mode != 2 && num === 1) {
                code += '<td class="text-center">CA</td>';
            } else if (num === 0) {
                code += '<td class="text-center">C</td>';
            } else {
                code += '<td class="text-center">-</td>';
            }
        }

        code += '<td></td><td></td></tr>';
    }

    return code;
}

function initTableHeader(jsonObj) {
    if (jsonObj.header == null) {
        return null;
    }

    var code = '<table class="table table-bordered"  style="table-layout:fixed;">';

    if (jsonObj.phase) {
        var overlimit = JSON.parse(JSON.stringify(pointOverLimit));

        if (jsonObj.header === "功率" && pointOverLimit.mode != 0) {
            overlimit.list = [];
            overlimit.list.push("ABC总");
        } else if (jsonObj.header === "有效值" || jsonObj.header === "谐波电流幅值") {
            overlimit.list = [];
            overlimit.list.push("A");
            overlimit.list.push("B");
            overlimit.list.push("C");
        }

        var length = overlimit.list.length;
        code += '<tr><th colspan = ' + jsonObj.length + ' class="text-center">' + jsonObj.header + '</th></tr>';
        code += '<tr><td rowspan="2" style="vertical-align: middle" class="text-center">名称</td><td rowspan="2" style="vertical-align: middle" class="text-center">单位</td>';

        if (jsonObj.max === 1) {
            code += '<td colspan="' + length + '" class="text-center">最大值</td>';
        }

        if (jsonObj.min === 1) {
            code += '<td colspan="' + length + '" class="text-center">最小值</td>';
        }

        if (jsonObj.avg === 1) {
            code += '<td colspan="' + length + '" class="text-center">平均值</td>';
        }

        if (jsonObj.cp95 === 1) {
            code += '<td colspan="' + length + '" class="text-center">95概率大值</td>';
        }

        code += '<td rowspan="2" style="vertical-align: middle" class="text-center">结论</td><td rowspan="2" style="vertical-align: middle" class="text-center">国标限值</td></tr>';

        if (jsonObj.max === 1) {
            for (var i = 0; i < overlimit.list.length; i++) {
                code += '<td class="text-center">' + overlimit.list[i] + '</td>';
            }
        }

        if (jsonObj.min === 1) {
            for (var i = 0; i < overlimit.list.length; i++) {
                code += '<td class="text-center">' + overlimit.list[i] + '</td>';
            }
        }

        if (jsonObj.avg === 1) {
            for (var i = 0; i < overlimit.list.length; i++) {
                code += '<td class="text-center">' + overlimit.list[i] + '</td>';
            }
        }

        if (jsonObj.cp95 === 1) {
            for (var i = 0; i < overlimit.list.length; i++) {
                code += '<td class="text-center">' + overlimit.list[i] + '</td>';
            }
        }
    }

    else {
        code += '<tr><th colspan =' + jsonObj.length + ' class="text-center">' + jsonObj.header + '</th></tr>';
        code += '<tr><td class="text-center">名称</td><td class="text-center">单位</td>';

        if (jsonObj.max === 1) {
            code += '<td class="text-center">最大值</td>';
        }

        if (jsonObj.min === 1) {
            code += '<td class="text-center">最小值</td>';
        }

        if (jsonObj.avg === 1) {
            code += '<td class="text-center">平均值</td>';
        }

        if (jsonObj.cp95 === 1) {
            code += '<td class="text-center">95概率大值</td>';
        }

        code += '<td class="text-center">结论</td><td class="text-center">国标限值</td></tr>';
    }

    return code;
}

function judgeValue(value) {
    if (null === value) {
        value = "/";
    }

    return value;
}

function intiTableData(data, i, json, name) {
    var code = '';
    var b = (name === "电压偏差" || name === "频率偏差");
    var length = json.phase ? pointOverLimit.list.length : 1;

    if (json.max === 1) {
        for (var j = 0; j < length; j++) {
            j = (json.header === "功率" && pointOverLimit.mode != 0) ? 3 : j;

            if (pointOverLimit.mode == 2 && j == pointOverLimit.list.length - 1 && name.indexOf("电流") === -1 && json.header != '谐波电流幅值') {
                code += '<td class="text-center">-</td>';
            }
            else if (null != data.body[i].overLimit && null != data.body[i].list[j].fmaxValue && parseFloat(data.body[i].overLimit) < (b ? Math.abs(parseFloat(data.body[i].list[j].fmaxValue)) : parseFloat(data.body[i].list[j].fmaxValue))) {
                code += '<td class="text-center" style="color: red">' + data.body[i].list[j].fmaxValue + '</td>';
            } else {
                code += '<td class="text-center">' + judgeValue(data.body[i].list[j].fmaxValue) + '</td>';
            }
        }
    }

    if (json.min === 1) {
        for (var j = 0; j < length; j++) {
            j = (json.header === "功率" && pointOverLimit.mode != 0) ? 3 : j;

            if (pointOverLimit.mode == 2 && j == pointOverLimit.list.length - 1 && name.indexOf("电流") === -1 && json.header != '谐波电流幅值') {
                code += '<td class="text-center">-</td>';
            }
            else if (null != data.body[i].overLimit && null != data.body[i].list[j].minValue && parseFloat(data.body[i].overLimit) < (b ? Math.abs(parseFloat(data.body[i].list[j].minValue)) : parseFloat(data.body[i].list[j].minValue))) {
                code += '<td class="text-center" style="color: red">' + data.body[i].list[j].minValue + '</td>';
            } else {
                code += '<td class="text-center">' + judgeValue(data.body[i].list[j].minValue) + '</td>';
            }
        }
    }

    if (json.avg === 1) {
        for (var j = 0; j < length; j++) {
            j = (json.header === "功率" && pointOverLimit.mode != 0) ? 3 : j;

            if (pointOverLimit.mode == 2 && j == pointOverLimit.list.length - 1 && name.indexOf("电流") === -1 && json.header != '谐波电流幅值') {
                code += '<td class="text-center">-</td>';
            }
            else if (null != data.body[i].overLimit && null != data.body[i].list[j].meanValue && parseFloat(data.body[i].overLimit) < (b ? Math.abs(parseFloat(data.body[i].list[j].meanValue)) : parseFloat(data.body[i].list[j].meanValue))) {
                code += '<td class="text-center" style="color: red">' + data.body[i].list[j].meanValue + '</td>';
            } else {
                code += '<td class="text-center">' + judgeValue(data.body[i].list[j].meanValue) + '</td>';
            }
        }
    }

    if (json.cp95 === 1) {
        for (var j = 0; j < length; j++) {
            j = (json.header === "功率" && pointOverLimit.mode != 0) ? 3 : j;

            if (pointOverLimit.mode == 2 && j == pointOverLimit.list.length - 1 && name.indexOf("电流") === -1 && json.header != '谐波电流幅值') {
                code += '<td class="text-center">-</td>';
            }
            else if (null != data.body[i].overLimit && null != data.body[i].list[j].cp95Value && parseFloat(data.body[i].overLimit) < (b ? Math.abs(parseFloat(data.body[i].list[j].cp95Value)) : parseFloat(data.body[i].list[j].cp95Value))) {
                code += '<td class="text-center" style="color: red">' + data.body[i].list[j].cp95Value + '</td>';
            } else {
                code += '<td class="text-center">' + judgeValue(data.body[i].list[j].cp95Value) + '</td>';
            }
        }
    }

    if (null == data.body[i].pass || !json.showPass) {
        code += '<td class="text-center">/</td>';
    } else if (-1 == data.body[i].pass) {
        code += '<td class="text-center" style="color: red">不合格</td>';
    } else {
        code += '<td class="text-center" >合格</td>';
    }

    if (null == data.body[i].overLimit) {
        code += '<td class="text-center">/</td><tr>';
    } else {
        if (b) {
            code += '<td class="text-center">±' + data.body[i].overLimit + '</td><tr>';
        } else {
            code += '<td class="text-center">' + data.body[i].overLimit + '</td><tr>';
        }
    }

    return code;
}

/*************************************************************************
 * 获取表头信息
 * 监测点的CT PT变比、电压等级等信息
 *************************************************************************/
function judgeTime(time) {
    if (time < 10) {
        return '0' + time;
    }

    return time;
}

function getPointInfo(obj, pointNode) {
    $.post("/pqs9300/getPointInfo", {pointIndex: pointNode},
        function (data) {
            counter();
            pointInfo.bdName = data.body.bdName;
            pointInfo.pName = data.body.pName;
            pointInfo.Name = data.body.name;
            //获取当前
            var date = new Date();
            var hours = " 23:59:59";

            if (date.getFullYear() + "-" + judgeTime(date.getMonth() + 1) + "-" + judgeTime(date.getDate()) === obj.endTime) {
                hours = " " + judgeTime(date.getHours()) + ":" + judgeTime(date.getMinutes()) + ":" + judgeTime(date.getSeconds());
            }

            pointInfo.time = "起始时间:" + obj.startTime + ' 00:00:00  结束时间:' + obj.endTime + hours;
            pointInfo.exceptTime = (obj.first == "" ? "无" : obj.first + (obj.second != "" ? "、" : "")) + (obj.second == "" ? "" : obj.second + (obj.thrid != "" ? "、" : "")) + (obj.thrid == "" ? "" : obj.thrid);
            pointInfo.body = data.body;
            initPointInfoTable(data);
        }, "json");
}

function initPointInfoTable(data) {
    var value = (data.code === 200 ? data : null);

    if (data.code != 200) {   //上海升级部分监测点信息不全，故后台导出json没有发现字段报错
        layer.close(layerId);
        layer.msg("获取监测点基本信息失败.");
        $("#export").prop("disabled", "disable");
        return;
    }

    getVoltageData();
    getPowerData();
    getflickerData();
    getVdeviationData();
    getFrequencyData();
    getThreephaseData();
    getdistortionData();
    getICurrent();
    getVoltageRate();
    //合格率统计
    getFPY();

    drawPointInfoTable('pointInfo', value, 0, '非谐波统计报表');
    drawPointInfoTable('vPointInfo', value, 0, '谐波电压统计报表');
    drawPointInfoTable('iPointInfo', value, 0, '谐波电流统计报表');
    drawPointInfoTable('fpyPointInfo', value, 4, '合格率统计报表');
}

function getValue(value) {
    return $('#' + value).val();
}

function drawPointInfoTable(dom, data, length, describe) {
    var divHeader = document.getElementById(dom);
    var len = length / 2;
    var code = '<table class="table table-bordered"  style="table-layout:fixed;">';
    code += '<tr><th colspan =' + (16 + length) + ' class="text-center">' + pointInfo.bdName + '  ' + pointInfo.pName + describe + '</th></tr>';
    code += '<tr><td colspan = "3">统计报表时间:</td>';
    code += '<td colspan = ' + (13 + length) + '>' + pointInfo.time + '</td></tr>';
    code += '<tr><td colspan = "3">检修时间:</td>';
    code += '<td colspan = ' + (13 + length) + '>' + pointInfo.exceptTime + '</td></tr>';
    code += '<tr><td colspan="3">监测线路:</td><td colspan=' + (8 + length) + '>' + pointInfo.Name + '</td>';

    if (null != data) {
        code += '<td colspan="3">电压等级(kV):</td><td colspan="2">' + data.body.voltageClasses + '</td><tr>' + '<tr><td colspan="3">PT变比:</td><td colspan=' + (len + 2) + '>' + data.body.pt1 + '/' + data.body.pt2 + '</td><td colspan="3">CT变比:</td><td colspan=' + (len + 3) + '>' + data.body.ct1 + '/' + data.body.ct2 + '</td>';
        code += '<td colspan="3">基准短路容量(MVA):</td><td colspan="2">' + data.body.jzcmp + ' MVA</td><tr>' + '<tr><td colspan="3">母线最小短路容量(MVA):</td><td colspan=' + (len + 2) + '>' + data.body.dlcmp + ' MVA</td>';
        code += '<td colspan="3">用户协议用电容量(MVA):</td><td colspan=' + (len + 3) + '>' + data.body.xycmp + ' MVA</td><td colspan="3">供电设备容量(MVA):</td><td colspan="2">' + data.body.devcmp + ' MVA</td></tr>';
        code += '<tr><td colspan="3">工况</td><td colspan=' + (13 + length) + '></td></tr>';
    } else {
        code += '<td colspan="3">电压等级(kV):</td><td colspan="2">/</td><tr>' + '<tr><td colspan="3">PT变比:</td><td colspan=' + (len + 2) + '>/</td><td colspan="3">CT变比:</td><td colspan=' + (len + 3) + '>/</td>';
        code += '<td colspan="3">基准短路容量(MVA)</td><td colspan="2">/</td><tr>' + '<tr><td colspan="3">母线最小短路容量(MVA):</td><td colspan=' + (len + 2) + '>/</td>';
        code += '<td colspan="3">用户协议用电容量(MVA)</td><td colspan=' + (len + 3) + '>/</td><td colspan="3">供电设备容量(MVA):</td><td colspan="2">/</td></tr>';
        code += '<tr><td colspan="3">工况:</td><td colspan=' + (13 + length) + '></td></tr>';
    }

    divHeader.innerHTML = code + '</table>';
}

/*************************************************************************
 * 获取电压有效值
 *************************************************************************/
function getVoltageData() {
    $.post("/pqs9300/getVirtualData", function (data) {
        voltageData = data;
        counter();
        initVoltageTable(data);
    }, "json");
}

function initVoltageTable(data) {
    var value = (200 === data.code ? data : null);
    drawVoltageTable(value);
}

function drawVoltageTable(data) {
    var voltageDiv = document.getElementById('voltage');
    var json = judge("有效值", true, -1);
    var code = initTableHeader(json);

    if (code == null) {
        voltageDiv.innerHTML = '';
        return;
    }

    code += initTableTD(data, json,
        {
            'data': [{
                'name':
                    '电压', "unit":
                    "kV"
            }, {
                'name':
                    '电流', "unit":
                    "A"
            }, {
                'name':
                    '线电压', "unit":
                    "kV"
            }]
        });

    voltageDiv.innerHTML = code + '</table>';
}

/***************************************************************
 * 获取功率
 **************************************************************/
function getPowerData() {
    $.post("/pqs9300/getPowerData", function (data) {
        counter();
        powerData = data;
        initPowerTable(data);
    }, "json");
}

function initPowerTable(data) {
    var value = (200 === data.code ? data : null);
    drawPowerTable(value);
}

function drawPowerTable(data) {
    var power = document.getElementById('power');
    var json = judge("功率", true, -1);
    var code = initTableHeader(json);

    if (code == null) {
        power.innerHTML = '';
        return;
    }

    code += initTableTD(data, json, {
        'data': [
            {
                'name':
                    '有功', "unit":
                    "kW"
            }, {
                'name':
                    '无功', "unit":
                    "kVar"
            }, {
                'name':
                    '视在', "unit":
                    "kVA"
            }]
    });

    power.innerHTML = code + '</table>';
}

/**********************************************
 * 电压闪变
 **********************************************/
function getflickerData() {
    $.post("/pqs9300/getflickerData", function (data) {
        counter();
        flickerData = data;
        initFlickerTable(data);
    }, "json");
}

function initFlickerTable(data) {
    var value = (200 === data.code ? data : null);
    drawFlickerTable(value);
}

function drawFlickerTable(data) {
    var flicker = document.getElementById('flicker');
    var json = judge("电压闪变", true, 1);
    var code = initTableHeader(json);

    if (code == null) {
        flicker.innerHTML = '';
        return;
    }

    code += initTableTD(data, json, {
        'data': [
            {
                'name':
                    'Pst', "unit":
                    "/"
            }, {
                'name':
                    'Plt', "unit":
                    "/"
            }]
    });

    flicker.innerHTML = code + '</table>';
}

/**********************************************
 * 畸变率
 **********************************************/
function getdistortionData() {
    $.post("/pqs9300/getdistortionData", function (data) {
        counter();
        distortionData = data;
        initDistortionTable(data);
    }, "json");
}

function initDistortionTable(data) {
    var value = (200 === data.code ? data : null);
    drawDistortionTable(value);
}

function drawDistortionTable(data) {
    var distortion = document.getElementById('distortion');
    var json = judge("畸变率", true, 1);
    var code = initTableHeader(json);

    if (code == null) {
        distortion.innerHTML = '';
        return;
    }

    code += initTableTD(data, json, {
        'data': [
            {
                'name':
                    '电压总谐波畸变率', "unit":
                    "%"
            }, {
                'name':
                    '电流总谐波畸变率', "unit":
                    "%"
            }]
    });

    distortion.innerHTML = code + '</table>';
}

/**********************************************
 * 电压偏差
 **********************************************/
function getVdeviationData() {
    $.post("/pqs9300/getVdeviationData", function (data) {
        counter();
        vdeviationData = data;
        initVdeviationTable(data);
    }, "json");
}

function initVdeviationTable(data) {
    var value = (200 === data.code ? data : null);
    drawVdeviationTable(value);
}

function drawVdeviationTable(data) {
    var vdeviation = document.getElementById('vdeviation');
    var json = judge("电压偏差", true, 1);
    var code = initTableHeader(json);

    if (code == null) {
        vdeviation.innerHTML = '';
        return;
    }

    code += initTableTD(data, json, {
        'data': [
            {
                'name':
                    '电压偏差', "unit":
                    "%"
            }]
    });

    vdeviation.innerHTML = code + '</table>';
}

/**********************************************
 * 频率
 **********************************************/
function getFrequencyData() {
    $.post("/pqs9300/getFrequencyData", function (data) {
        counter();
        frequencyData = data;
        initFrequencyTable(data);
    }, "json");
}

function initFrequencyTable(data) {
    var value = (200 === data.code ? data : null);
    drawFrequencyTable(value);
}

function drawFrequencyTable(data) {
    var frequency = document.getElementById('frequency');
    var json = judge("频率", false, 1);
    var code = initTableHeader(json);

    if (code == null) {
        frequency.innerHTML = '';
        return;
    }

    code += initTableTD(data, json, {
        'data': [
            {
                'name':
                    '频率', "unit":
                    "Hz"
            }, {
                'name':
                    '频率偏差', "unit":
                    "Hz"
            }]
    });

    frequency.innerHTML = code + '</table>';
}

/**********************************************
 * 三相电压不平衡度
 **********************************************/
function getThreephaseData() {
    $.post("/pqs9300/getThreephaseData", function (data) {
        counter();
        threephaseData = data;
        initThreephaseTable(data);
    }, "json");
}

function initThreephaseTable(data) {
    var value = (200 === data.code ? data : null);
    drawThreephaseTable(value);
}

function drawThreephaseTable(data) {
    var threephase = document.getElementById('threephase');
    var json = judge("三相不平衡度", false, 4);
    var code = initTableHeader(json);

    if (code == null) {
        threephase.innerHTML = '';
        return;
    }

    code += initTableTD(data, json, {
        'data': [
            {
                'name':
                    '三相电压不平衡度', "unit":
                    "%"
            }, {
                'name':
                    '正序电压', "unit":
                    "kV"
            }, {
                'name':
                    '负序电压', "unit":
                    "V"
            }, {
                'name':
                    '零序电压', "unit":
                    "V"
            }, {
                'name':
                    '电流不平衡', "unit":
                    "%"
            }, {
                'name':
                    '正序电流', "unit":
                    "A"
            }, {
                'name':
                    '负序电流', "unit":
                    "A"
            }, {
                'name':
                    '零序电流', "unit":
                    "A"
            }]
    });

    threephase.innerHTML = code + '</table>';
}

/**********************************************
 * 谐波电流幅值
 **********************************************/
function getICurrent() {
    $.post("/pqs9300/getICurrent", function (data) {
        counter();
        currentData = data;
        initICurrentTable(data);
    }, "json");
}

function initICurrentTable(data) {
    var value = (200 === data.code ? data : null);
    drawICurrentTable(value);
}

function drawICurrentTable(data) {
    var icurrent = document.getElementById('icurrent');
    var json = judge("谐波电流幅值", true, 4);
    var code = initTableHeader(json);

    if (code == null) {
        icurrent.innerHTML = '';
        return;
    }

    code += initTableTD(data, json, transformICurrentData());
    icurrent.innerHTML = code + '</table>';
}

function transformICurrentData() {
    var arr = [];
    arr.push({'name': '基波', 'unit': 'A'});

    for (var i = 2; i < 51; i++) {
        var obj = {};
        obj.name = i + '次';
        obj.unit = 'A';

        arr.push(obj);
    }

    var jsonObj = {'data': arr};

    return jsonObj;
}

/**********************************************
 * 谐波电压含有率
 **********************************************/
function getVoltageRate() {
    $.post("/pqs9300/getVoltageRate", function (data) {
        counter();
        voltageRateData = data;
        initVoltageRateTable(data);
    }, "json");
}

function initVoltageRateTable(data) {
    var value = (200 === data.code ? data : null);
    drawVoltageRateTable(value);
}

function drawVoltageRateTable(data) {
    var voltageRate = document.getElementById('voltageRate');
    var json = judge("谐波电压含有率", true, 4);
    var code = initTableHeader(json);

    if (code == null) {
        voltageRate.innerHTML = '';
        return;
    }
    code += initTableTD(data, json, transformVoltageRateData());
    voltageRate.innerHTML = code + '</table>';
}

function transformVoltageRateData() {
    var arr = [];
    arr.push({'name': '基波', 'unit': 'kV'});

    for (var i = 2; i < 51; i++) {
        var obj = {};
        obj.name = i + '次';
        obj.unit = '%';

        arr.push(obj);
    }

    arr.push({'name': '电压总谐波畸变率', 'unit': '%'});

    var jsonObj = {'data': arr};

    return jsonObj;
}

function getFPY() {
    $.post("/pqs9300/getFpy", function (data) {
        counter();
        fpyData = data;
        initFpyTable(data);
    }, "json");
}

function initFpyTable(data) {
    var value = (200 === data.code ? data : null);
    var json = judgeFpy("合格率");
    drawFpyTable(json, value);
}

function drawFpyHeader(json, type) {
    var code = '';
    var length = 0;

    if (type == 1) {
        length = json.dyhyl + json.dlhyl;
    } else {
        if (json.dypc + json.dyxbjbl + json.dycssb > 0) {
            length++;
        }
    }

    code += '<tr>';
    var index = 0;

    for (var i = 0; i < length; i++) {
        code += '<td colspan = "3" style="vertical-align: middle" rowspan = "2" class="text-center">参数</td>';
        code += '<td rowspan = "2" colspan = "2" style="vertical-align: middle" class="text-center">限值(%)</td>';
        code += '<td rowspan = "2" colspan = "2" style="vertical-align: middle" class="text-center">合格率(%)</td>';
        for (var k = 0; k < pointOverLimit.list.length; k++) {
            if (json.dyhyl === 1 && json.dlhyl === 1) {
                index = 1;
            }

            if (json.dlhyl === 1 && i === index) {
                code += '<td colspan = "4" class="text-center">A相</td>';
                code += '<td colspan = "4" class="text-center">B相</td>';
                code += '<td colspan = "4" class="text-center">C相</td>';
                break;
            } else {
                code += '<td colspan = "4" class="text-center">' + pointOverLimit.list[k] + (pointOverLimit.list[k] === '-' ? '</td>' : '相</td>');
            }
        }
    }

    if (type == 2 && (json.sxdybphd + json.plpc > 0)) {
        code += '<td colspan = "4" style="vertical-align: middle" rowspan = "2" class="text-center">参数</td>';
        code += '<td rowspan = "2" colspan = "4" style="vertical-align: middle" class="text-center">限值(%)</td>';
        code += '<td colspan = "' + (pointOverLimit.list.length === 2 ? 4 : 6) + '" rowspan = "2" style="vertical-align: middle" class="text-center">采样点数</td>';
        code += '<td colspan = "' + (pointOverLimit.list.length === 2 ? 3 : 5) + '" rowspan = "2" style="vertical-align: middle" class="text-center">合格率(%)</td>';
    }

    code += '</tr><tr>';

    for (var i = 0; i < pointOverLimit.list.length * length; i++) {
        if ((type === 2 || (type === 1 && json.dyhyl == 1)) && pointOverLimit.mode == 2 && i === 2) {
            code += '<td colspan = "2" class="text-center">-</td>';
            code += '<td colspan = "2" class="text-center">-</td>';
        } else {
            code += '<td colspan = "2" class="text-center">采样点数</td>';
            code += '<td colspan = "2" class="text-center">合格率(%)</td>';
        }
    }

    code += '</tr>';

    return code;
}

function drawPhase(fpy, list, index, phase, name, className) {
    var code = '';

    if (name != null) {
        code += '<td colspan = "3" class="text-center ' + className + '" style="vertical-align: middle" class="text-center">' + name + '</td>';
    }

    var length = (phase == 'T') ? 1 : 2;

    if (phase === 'T') {
        code += '<td colspan = "2" class="text-center ' + className + '">' + fpy + '</td>';
    }

    if (list === '-') {
        code += '<td colspan = "2" class="text-center ' + className + '">-</td>';
        code += '<td colspan = "2" class="text-center ' + className + '">-</td>';
    } else if (list != null) {
        if (phase != 'T') {
            code += '<td colspan = "2" class="text-center ' + className + '">' + list[index].count + '</td>';
        }

        if (parseFloat(fpy) > parseFloat(list[index].rate)) {
            code += '<td colspan = "2" class="text-center ' + className + '" style="color: red">' + list[index].rate + '</td>';
        } else {
            code += '<td colspan = "2" class="text-center ' + className + '">' + list[index].rate + '</td>';
        }
    } else {
        for (var i = 0; i < length; i++) {
            code += '<td colspan = "2" class="text-center ' + className + '">/</td>'; //T
        }
    }

    return code;
}

function drawFpyBody(json, data, type) {
    var code = '';
    var length = 0;

    if (type == 1) {
        length = json.dyhyl + json.dlhyl;
    } else {
        if (json.dypc + json.dyxbjbl + json.dycssb > 0) {
            length++;
        }
    }

    if (length > 0) {
        for (var i = 0; i < 24; i++) {
            for (var j = 0; j < length; j++) {
                j == 0 ? code += '<tr>' : code += '';
                j = (json.dyhyl == 0 ? 1 : j);

                if (json.dyhyl == 1 && i == 0 && j < 1) {
                    code += '<td colspan = "2" rowspan="24" style="vertical-align: middle" class="text-center">2<br>至<br>25<br>次<br>谐<br>波<br>电<br>压<br>含<br>有<br>率<br>(%)</td>';
                } else if (json.dlhyl == 1 && i == 0) {
                    code += '<td colspan = "2" rowspan="24" style="vertical-align: middle" class="text-center">2<br>至<br>25<br>次<br>谐<br>波<br>电<br>流<br>含<br>有<br>率<br>(%)</td>';
                }

                code += '<td class="text-center">' + (i + 2) + '</td>';

                //总的合格率
                code += drawPhase(data.body[j].fpy, data.body[j].listS, i, 'T', null, 'xb');

                //A相指标
                code += drawPhase(data.body[j].fpy, data.body[j].listA, i, 'A', null, 'xb');

                //B相指标
                code += drawPhase(data.body[j].fpy, data.body[j].listB, i, 'B', null, 'xb');

                //C相指标
                if (pointOverLimit.mode != 2 || json.dlhyl === 1 && j === 1) {
                    code += drawPhase(data.body[j].fpy, data.body[j].listC, i, 'C', null, 'xb');
                } else {
                    code += drawPhase('-', '-', i, 'C', null, 'xb');
                }
            }

            code += '</tr>';
        }
    }

    return code;
}

function drawnoPhase(fpy, list, name, className) {
    var code = '';
    code += '<td colspan = "4" style="vertical-align: middle" class="text-center ' + className + '">' + name + '</td>';
    code += '<td colspan = "4" style="vertical-align: middle" class="text-center ' + className + '">' + fpy + '</td>';

    if (list != null) {
        code += '<td colspan = "' + (pointOverLimit.list.length === 2 ? 4 : 6) + '" style="vertical-align: middle" class="text-center ' + className + '">' + list[0].count + '</td>';

        if (parseFloat(fpy) > parseFloat(list[0].rate)) {
            code += '<td colspan = "' + (pointOverLimit.list.length === 2 ? 3 : 5) + '" style="vertical-align: middle;color: red" class="text-center ' + className + '">' + list[0].rate + '</td>';
        } else {
            code += '<td colspan = "' + (pointOverLimit.list.length === 2 ? 3 : 5) + '" style="vertical-align: middle" class="text-center ' + className + '">' + list[0].rate + '</td>';
        }
    } else {
        code += '<td colspan = "' + (pointOverLimit.list.length === 2 ? 4 : 6) + '" style="vertical-align: middle" class="text-center ' + className + '">/</td>';
        code += '<td colspan = "' + (pointOverLimit.list.length === 2 ? 3 : 5) + '" style="vertical-align: middle" class="text-center ' + className + '">/</td>';
    }

    return code;
}

function drawFpyTable(json, data) {
    var fpy = document.getElementById('fpy');

    if (json.hgl == null) {
        fpy.innerHTML = '';
        return;
    }

    var code = '<table class="table table-bordered"  style="table-layout:fixed;">';

    //谐波电压含有率,谐波电流含有率
    code += drawFpyHeader(json, 1);
    code += drawFpyBody(json, data, 1);

    code += '</table>';
    code += '<table class="table table-bordered"  style="table-layout:fixed;">';

    //非谐波
    code += drawFpyHeader(json, 2);

    //电压偏差
    var dypc = drawPhase(data.body[2].fpy, data.body[2].listS, 0, 'T', '电压偏差', 'dypc');
    dypc += drawPhase(data.body[2].fpy, data.body[2].listA, 0, 'A', null, 'dypc');
    dypc += drawPhase(data.body[2].fpy, data.body[2].listB, 0, 'B', null, 'dypc');

    if (pointOverLimit.mode != 2) {
        dypc += drawPhase(data.body[2].fpy, data.body[2].listC, 0, 'C', null, 'dypc');
    } else {
        dypc += drawPhase('-', '-', 0, 'C', null, 'dypc');
    }

    //三相电压不平衡度  //ddddddd
    var sxdybphd = drawnoPhase(data.body[3].fpy, data.body[3].listT, '三相电压不平衡度', 'sxdybphd');

    //电压谐波畸变率
    var dyxbjbl = drawPhase(data.body[4].fpy, data.body[4].listS, 0, 'T', '电压总谐波畸变率', 'dyxbjbl');
    dyxbjbl += drawPhase(data.body[4].fpy, data.body[4].listA, 0, 'A', null, 'dyxbjbl');
    dyxbjbl += drawPhase(data.body[4].fpy, data.body[4].listB, 0, 'B', null, 'dyxbjbl');
    if (pointOverLimit.mode != 2) {
        dyxbjbl += drawPhase(data.body[4].fpy, data.body[4].listC, 0, 'C', null, 'dyxbjbl');
    } else {
        dyxbjbl += drawPhase('-', '-', 0, 'C', null, 'dyxbjbl');
    }

    //频率偏差
    var plpc = drawnoPhase(data.body[5].fpy, data.body[5].listT, '频率偏差', 'plpc');

    //电压长时闪变
    var dycssb = drawPhase(data.body[6].fpy, data.body[6].listS, 0, 'T', '电压长时闪变', 'dycssb');
    dycssb += drawPhase(data.body[6].fpy, data.body[6].listA, 0, 'A', null, 'dycssb');
    dycssb += drawPhase(data.body[6].fpy, data.body[6].listB, 0, 'B', null, 'dycssb');
    if (pointOverLimit.mode != 2) {
        dycssb += drawPhase(data.body[6].fpy, data.body[6].listC, 0, 'C', null, 'dycssb');
    } else {
        dycssb += drawPhase('-', '-', 0, 'C', null, 'dycssb');
    }

    //占位隐藏行
    var zwyc = drawPhase(data.body[6].fpy, data.body[6].listS, 0, 'T', '电压长时闪变', 'zwyc');
    zwyc += drawPhase(data.body[6].fpy, data.body[6].listA, 0, 'A', null, 'zwyc');
    zwyc += drawPhase(data.body[6].fpy, data.body[6].listB, 0, 'B', null, 'zwyc');
    if (pointOverLimit.mode != 2) {
        zwyc += drawPhase(data.body[6].fpy, data.body[6].listC, 0, 'C', null, 'zwyc');
    } else {
        zwyc += drawPhase('-', '-', 0, 'C', null, 'zwyc');
    }

    /**
     * @type {string}
     * 组装数据
     */
    var count1 = json.dypc + json.dyxbjbl + json.dycssb;
    var count2 = json.plpc + json.sxdybphd;

    if (count1 > 0) {
        if (count2 == 0) {
            code += (json.dypc === 1 ? '<tr>' + dypc + '</tr>' : "");
            code += (json.dyxbjbl === 1 ? '<tr>' + dyxbjbl + '</tr>' : "");
            code += (json.dycssb === 1 ? '<tr>' + dycssb + '</tr>' : "");
        } else {
            var length = count1 < count2 ? count2 : count1;

            for (var i = 0; i < length; i++) {
                //前半部分
                if (json.dypc == 1 && code.indexOf("dypc") == -1) {
                    code += '<tr>' + dypc;
                } else if (json.dyxbjbl == 1 && code.indexOf("dyxbjbl") == -1) {
                    code += '<tr>' + dyxbjbl;
                } else if (json.dycssb == 1 && code.indexOf("dycssb") == -1) {
                    code += '<tr>' + dycssb;
                } else {
                    code += '<tr>' + zwyc;
                }

                //后半部分
                if (json.sxdybphd == 1 && code.indexOf("sxdybphd") == -1) {
                    code += sxdybphd + '</tr>';
                }
                else if (json.plpc == 1 && code.indexOf("plpc") == -1) {
                    code += plpc + '</tr>';
                } else {
                    code += '</tr>';
                }
            }
        }
    } else {
        code += (json.sxdybphd === 1 ? '<tr>' + sxdybphd + '</tr>' : "");
        code += (json.plpc === 1 ? '<tr>' + plpc + '</tr>' : "");
    }

    fpy.innerHTML = code + '</table>';

    var x = document.getElementsByClassName("zwyc");
    for (var i = 0; i < x.length; i++) {
        x[i].style.visibility = "hidden";
    }
}

function judgeFpy(value) {
    if (ployValue === "##") {
        return {'hgl': 1, 'dyhyl': 1, 'dlhyl': 1, 'dypc': 1, 'dyxbjbl': 1, 'dycssb': 1, 'sxdybphd': 1, 'plpc': 1};
    }

    for (var i = 0; i < ploysetting.body.length; i++) {
        if (value === ploysetting.body[i].name) {
            return {
                'hgl': 1,
                'dyhyl': getPloySettingValue(ploysetting.body[i].listTarget, 5),
                'dlhyl': getPloySettingValue(ploysetting.body[i].listTarget, 6),
                'dypc': getPloySettingValue(ploysetting.body[i].listTarget, 7),
                'dyxbjbl': getPloySettingValue(ploysetting.body[i].listTarget, 9),
                'dycssb': getPloySettingValue(ploysetting.body[i].listTarget, 11),
                'sxdybphd': getPloySettingValue(ploysetting.body[i].listTarget, 8),
                'plpc': getPloySettingValue(ploysetting.body[i].listTarget, 10)
            }
        }
    }

    return {"hgl": null};
}