var loading;
var resultData;
var height = $(window).height() - 140;
$(function () {
    initselect()
    initTableData()

})


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

$("#batchImport").click(function () {
    var op = layer.open({
        type: 1,
        title: '批量导入监测点评级',
        shadeClose: true,
        resize: false,
        area: ['20%', '32%'],
        content: '<div style="padding: 15px"><button class="btn btn-primary" id="import">选择文件</input></div>' +
            '<div style="position:absolute;bottom:10px;width: 100%;text-align: center" ><button style="margin: 0px auto" class="btn btn-primary" id="upfile">提交</input></div>',
        success: function () {
            layui.use('upload', function () {
                var upload = layui.upload;

                //执行实例
                var uploadInst = upload.render({
                    elem: '#import' //绑定元素
                    , url: '/pqs9900/lineWeightMark/batchImportLineWeight' //上传接口
                    , accept: 'file' //普通文件
                    , exts: 'xls|excel|xlsx|csv' //只允许上传压缩文件
                    , auto: false
                    , bindAction: '#upfile' //
                    , done: function (res) {
                        if (res.code == 200) {
                            layer.msg(res.message, {icon: 1}, function () {
                                layer.close(op);
                                initTableData()
                            })

                        } else {
                            layer.msg(res.message, {icon: 2})
                        }

                    }
                    , error: function () {
                        //请求异常回调
                    }
                });
            })
        }
    });

});


function initTableData() {

    let devFlag = $("#devFlag").val()

    $.ajax({
        type: "get",
        url: "/pqs9900/deviceUnit/unitTable",
        dataType: 'json',
        data: {devFlag: devFlag},
        beforeSend: function () {
            loading = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            if (data.code === 500) {
                layer.msg(data.message, {icon: 2, time: 2000})
                resultData = null;
            } else {
                resultData = data.body;
            }
            initTable(resultData);
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(loading);
        }

    })
}

//实例化表格
function initTable(data) {

    dataTable = $("#lineLedger").DataTable();
    dataTable.clear().draw();
    dataTable = $('#lineLedger').DataTable({
        order: [],
        columnDefs: [
            {
                orderable: false,
                targets: [0]
            }
        ],
        columns: [
            {width: "3%", data: null},
            {width: "7%", data: "gdName"},
            {width: "15%", data: "subName"},
            {width: "10%", data: "devName"},
            {
                width: "5%", data: "devFlag", render: function (data) {
                    if (data == 0) {
                        return "投运"
                    } else if (data == 1) {
                        return "检修"
                    } else {
                        return "退运"
                    }
                }
            },

            {width: "3%", data: "unitFrequency"},
            {width: "3%", data: "unitFrequencyDev"},
            {width: "3%", data: "phaseVoltage"},
            {width: "3%", data: "lineVoltage"},
            {width: "3%", data: "voltageDev"},
            {width: "3%", data: "uvoltageDev"},
            {width: "3%", data: "iEffective"},

            {width: "3%", data: "singleP"},
            {width: "3%", data: "singleViewP"},
            {width: "3%", data: "singleNoP"},
            {width: "3%", data: "totalActiveP"},
            {width: "3%", data: "totalViewP"},
            {width: "3%", data: "totalNoP"},
            {width: "3%", data: "vFundEffective"},
            {width: "3%", data: "iFund"},
            {width: "3%", data: "fundActiveP"},
            {width: "3%", data: "fundNoP"},
            {width: "3%", data: "vDistortion"},
            {width: "3%", data: "vHarmonicRate"},

            {width: "3%", data: "iHarmonic"},
            {width: "3%", data: "pHarmonic"},
            {width: "3%", data: "noPHarmonic"},
            {width: "3%", data: "viewPHarmonic"},
            {width: "3%", data: "iIharmonic"},
            {width: "3%", data: "positiveV"},
            {width: "3%", data: "noPositiveV"},

            {
                width: "10%", render: function (data, type, full, meta) {
                    return "<button type='button' class='btn themeBGColor'  onclick='gradeOperate(\"" + full.devIndex + "\")' >修改</button>"
                }
            }
        ],
        autoWidth: false,
        destroy: true,
        data: data,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollY: height,
        scrollX: true,
        lengthMenu: [50],
        language: {
            url: '/pqs9900/json/chine.json'
        },
        createdRow: function (row, data, index) {
            if (data.devFlag == 0) {
                $('td', row).eq(4).css('color', '#00ff04')
            } else if (data.devFlag == 1) {
                $('td', row).eq(4).css('color', '#edca1c')
            } else if (data.devFlag == 2) {
                $('td', row).eq(4).css('color', '#ED1C24')
            }

        },
        fnDrawCallback: function (oTable) {
            $("#lineLedger_info").addClass("fl");
            $("#lineLedger_paginate").addClass("fr");
            $("#lineLedger_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 = $("#lineLedger").dataTable();
            $('#dataTable-btn').click(function (e) {
                if ($("#changePage").val() && $("#changePage").val() > 0) {
                    var redirectpage = $("#changePage").val() - 1;
                } else {
                    var redirectpage = 0;
                }
                table.fnPageChange(redirectpage);
            });
        }
    });


    new $.fn.dataTable.Buttons(dataTable, {
        buttons: [
            {
                extend: 'csv',
                text: '导出',//导出csv名称
                title: '单位' + new Date().getTime(),
                exportOptions: {
                    columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14,15,16,17,18,19,20,21,22,23],
                    format: {
                        body: function (data, rID, cID) {
                            if(cID == 0){
                                return rID+1;
                            }
                            return data;
                        }
                    }
                }
            }
        ]
    });


    modifyCss(dataTable);
    dataTable.on('order.dt search.dt',
        function () {
            dataTable.column(0, {
                search: 'applied',
                order: 'applied'
            }).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        }).draw();

}

function modifyCss(dataTable) {
    dataTable.buttons().container().css("display", "inline-block");
    dataTable.buttons().container().css("float", "right");
    dataTable.buttons().container().css("top", "4px");
    dataTable.buttons().container().find("a").prepend("<i class='glyphicon glyphicon-share'></i>");
    dataTable.buttons().container().appendTo('.header');
    dataTable.buttons().container().after($("#fliteData")[0]);
}

//评分操作
var op2;
var devId;

function gradeOperate(devIndex) {
    devId = devIndex;

    op2 = layer.open({
        type: 1,
        title: "单位修改",
        shadeClose: true,
        resize: false,
        area: ['30%', '90%'],
        content: '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">频率：</label></div><select style="width: 100px"  id="unitFrequency" class="form-control"><option value="Hz">Hz</option><option value="kHz">kHz</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">频率偏差：</label></div><select style="width: 100px" id="unitFrequencyDev" class="form-control"><option value="Hz">Hz</option><option value="kHz">kHz</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">相电压有效值：</label></div><select style="width: 100px" id="phaseVoltage" class="form-control"><option value="kV">kV</option><option value="V">V</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">线电压有效值：</label></div><select style="width: 100px" id="lineVoltage" class="form-control"><option value="kV">kV</option><option value="V">V</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">电压上偏差：</label></div><select style="width: 100px" id="voltageDev" class="form-control"><option value="%">%</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">电压下偏差：</label></div><select style="width: 100px" id="uvoltageDev" class="form-control"><option value="%">%</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">电流有效值：</label></div><select style="width: 100px" id="iEffective" class="form-control"><option value="A">A</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">单相有功功率：</label></div><select style="width: 100px" id="singleP" class="form-control"><option value="kW">kW</option><option value="W">W</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">单相视在功率：</label></div><select style="width: 100px" id="singleViewP" class="form-control"><option value="kVA">kVA</option><option value="VA">VA</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">单相无功功率：</label></div><select style="width: 100px" id="singleNoP" class="form-control"><option value="kVar">kVar</option><option value="Var">Var</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">总有功功率：</label></div><select style="width: 100px" id="totalActiveP" class="form-control"><option value="kW">kW</option><option value="W">W</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">总视在功率：</label></div><select style="width: 100px" id="totalViewP" class="form-control"><option value="kVA">kVA</option><option value="VA">VA</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">总无功功率：</label></div><select style="width: 100px" id="totalNoP" class="form-control"><option value="kVar">kVar</option><option value="Var">Var</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">相（线）电压基波有效值：</label></div><select style="width: 100px" id="vFundEffective" class="form-control"><option value="kV">kV</option><option value="V">V</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">基波电流：</label></div><select style="width: 100px" id="iFund" class="form-control"><option value="A">A</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">基波有功功率：</label></div><select style="width: 100px" id="fundActiveP" class="form-control"><option value="kW">kW</option><option value="W">W</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">基波无功功率：</label></div><select style="width: 100px" id="fundNoP" class="form-control"><option value="kVar">kVar</option><option value="Var">Var</option></select></div>' +

            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">2～50次谐波电流有效值：</label></div><select style="width: 100px" id="iHarmonic" class="form-control"><option value="A">A</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">2～50次谐波有功功率：</label></div><select style="width: 100px" id="pHarmonic" class="form-control"><option value="kW">kW</option><option value="W">W</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">2～50次谐波无功功率：</label></div><select style="width: 100px" id="noPHarmonic" class="form-control"><option value="Var">Var</option><option value="kVar">kVar</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">2～50次谐波视在功率：</label></div><select style="width: 100px" id="viewPHarmonic" class="form-control"><option value="VA">VA</option><option value="KVA">KVA</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">0.5～49.5次间谐波电流有效值：</label></div><select style="width: 100px" id="iIharmonic" class="form-control"><option value="A">A</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">正序电压：</label></div><select style="width: 100px" id="positiveV" class="form-control"><option value="kV">kV</option><option value="V">V</option></select></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-5 col-sm-5"><label style="margin-top: 4px" class="form-group">零序负序电压：</label></div><select style="width: 100px" id="noPositiveV" class="form-control"><option value="kV">kV</option><option value="V">V</option></select></div>' +


            '<div class="form-inline mt15" style="text-align: center;margin-bottom: 10px"><button id="submit" class="btn btn-primary">提交</button></div>',



        success: function () {

            $.ajax({
                type: "get",
                url: "/pqs9900/deviceUnit/devUnitDetail",
                data: {devId:devId},
                dataType: 'json',
                beforeSend: function () {
                    loading = ityzl_SHOW_LOAD_LAYER()
                },
                success: function (res) {
                    if (res.code == 200) {
                        let data = res.body

                        $("#unitFrequency").val(data.unitFrequency);

                        $("#unitFrequencyDev").val(data.unitFrequencyDev);

                        $("#phaseVoltage").val(data.phaseVoltage);

                        $("#lineVoltage").val(data.lineVoltage);

                        $("#voltageDev").val(data.voltageDev);

                        $("#uvoltageDev").val(data.uvoltageDev);

                        $("#iEffective").val(data.iEffective);

                        $("#singleP").val(data.singleP);

                        $("#singleViewP").val(data.singleViewP);

                        $("#singleNoP").val(data.singleNoP);

                        $("#totalActiveP").val(data.totalActiveP);

                        $("#totalViewP").val(data.totalViewP);

                        $("#totalNoP").val(data.totalNoP);

                        $("#vFundEffective").val(data.vFundEffective);

                        $("#iFund").val(data.iFund);

                        $("#fundActiveP").val(data.fundActiveP);

                        $("#fundNoP").val(data.fundNoP);

                        $("#vDistortion").val(data.vDistortion);

                        $("#vHarmonicRate").val(data.vHarmonicRate);



                        $("#iHarmonic").val(data.iHarmonic);

                        $("#pHarmonic").val(data.pHarmonic);
                        $("#noPHarmonic").val(data.noPHarmonic);
                        $("#viewPHarmonic").val(data.viewPHarmonic);

                        $("#iIharmonic").val(data.iIharmonic);

                        $("#positiveV").val(data.positiveV);

                        $("#noPositiveV").val(data.noPositiveV);

                    } else {
                        layer.msg(data.message, {icon: 2})
                    }

                    ityzl_CLOSE_LOAD_LAYER(loading)
                }
            })

        }
    })
}

//提交操作
$("body").on('click', "#submit", function () {
    let unitFrequency = $("#unitFrequency").val();

    let unitFrequencyDev = $("#unitFrequencyDev").val();

    let phaseVoltage = $("#phaseVoltage").val();

    let lineVoltage = $("#lineVoltage").val();

    let voltageDev = $("#voltageDev").val();

    let uvoltageDev = $("#uvoltageDev").val();

    let iEffective = $("#iEffective").val();

    let singleP = $("#singleP").val();

    let singleViewP = $("#singleViewP").val();

    let singleNoP = $("#singleNoP").val();

    let totalActiveP = $("#totalActiveP").val();

    let totalViewP = $("#totalViewP").val();

    let totalNoP = $("#totalNoP").val();

    let vFundEffective = $("#vFundEffective").val();

    let iFund = $("#iFund").val();

    let fundActiveP = $("#fundActiveP").val();

    let fundNoP = $("#fundNoP").val();

    let vDistortion = $("#vDistortion").val();

    let vHarmonicRate = $("#vHarmonicRate").val();

    let iHarmonic = $("#iHarmonic").val();

    let pHarmonic = $("#pHarmonic").val();

    let noPHarmonic = $("#noPHarmonic").val();

    let viewPHarmonic = $("#viewPHarmonic").val();

    let iIharmonic = $("#iIharmonic").val();

    let positiveV = $("#positiveV").val();

    let noPositiveV = $("#noPositiveV").val();

    $.ajax({
        type: "POST",
        url: "/pqs9900/deviceUnit/addUnit",
        data: JSON.stringify({
                devIndex:devId,
                unitFrequency: unitFrequency,
                unitFrequencyDev: unitFrequencyDev,
                phaseVoltage: phaseVoltage,
                lineVoltage: lineVoltage,
                voltageDev: voltageDev,
                uvoltageDev: uvoltageDev,
                iEffective: iEffective,
                singleP: singleP,
                singleViewP: singleViewP,
                singleNoP: singleNoP,
                totalActiveP: totalActiveP,
                totalViewP: totalViewP,
                totalNoP: totalNoP,
                vFundEffective: vFundEffective,
                iFund: iFund,
                fundActiveP: fundActiveP,
                fundNoP: fundNoP,
                vDistortion: vDistortion,
                vHarmonicRate: vHarmonicRate,
                iHarmonic:iHarmonic,
                pHarmonic:pHarmonic,
                noPHarmonic:noPHarmonic,
                viewPHarmonic:viewPHarmonic,
                iIharmonic:iIharmonic,
                positiveV:positiveV,
                noPositiveV:noPositiveV

            }
        ),
        dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        beforeSend: function () {
            loading = ityzl_SHOW_LOAD_LAYER()
        },
        success: function (data) {
            if (data.code == 200) {
                layer.msg(data.message, {icon: 1, time: 2000}, function () {
                    layer.close(op2);
                    initTableData()
                })
            } else {
                layer.msg(data.message, {icon: 2})
            }

            ityzl_CLOSE_LOAD_LAYER(loading)
        }
    })
})


//搜索表中的数据
function searchResult(value) {
    dataTable.search(value).draw();
}




function initselect() {
    var option = "<option value=''>全部</option>";
    $.ajax({
        method: "post",
        url: "/pqs9900/device/getselect",
        data: {
            dictypeName: '终端等级'
        },
        dataType: 'json',
        async: false,
        success: function (data) {
            for (var i = 0; i < data.length; i++) {

                option = option + "<option value='"
                    + data[i].dicLeave + "'>"
                    + data[i].dicName + "</option>";

            }
            $("#lineGrade").empty()
            $("#lineGrade").append(option)
        }
    })
}

