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/lineMark/batchImport' //上传接口
                    , 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 status = $("#status").val()
    let lineGrade = $("#lineGrade").val()

    $.ajax({
        type: "get",
        url: "/pqs9900/lineMark/getLineMark",
        dataType: 'json',
        data: {status: status, lineGrade: lineGrade},
        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: "province"},
            {width: "7%", data: "gdName"},
            {width: "15%", data: "subName"},
            {width: "10%", data: "devName"},
            {width: "12%", data: "lineName"},
            {
                width: "5%", data: "status", render: function (data) {
                    if (data == 1) {
                        return "正常"
                    } else {
                        return "中断"
                    }
                }
            },
            {
                width: "5%", data: "devflag", render: function (data) {
                    if (data == 0) {
                        return "投运"
                    } else if (data == 1) {
                        return "检修"
                    } else {
                        return "退运"
                    }
                }
            },
            {
                width: "12%", data: "updateTime", render: function (data) {
                    return data.substring(0, data.indexOf("."))
                }
            },
            {width: "3%", data: "lineGrade"},
            {
                width: "10%", render: function (data, type, full, meta) {
                    if (full.lineGrade == null) {
                        return "<button type='button' class='btn themeBGColor'  onclick='gradeOperate(\"" + full.lineIndex + "\",null,1)' >新增评级</button>"
                    } else {
                        return "<button type='button' class='btn themeBGColor'  onclick='gradeOperate(\"" + full.lineIndex + "\",\"" + full.lineGrade + "\",2)' >修改评级</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.status == "1") {
                $('td', row).eq(6).css('color', '#00ff04')
            } else if (data.status == "0") {
                $('td', row).eq(6).css('color', '#ED1C24')
            }
            if (data.devflag == 0) {
                $('td', row).eq(7).css('color', '#00ff04')
            } else if (data.devflag == 1) {
                $('td', row).eq(7).css('color', '#edca1c')
            } else if (data.devflag == 2) {
                $('td', row).eq(7).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: [1, 2, 3, 4, 5, 9],
                    format: {
                        body: function (data, rID, cID) {
                            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 lineId;

function gradeOperate(lineIndex, lineGrade, type) {
    lineId = lineIndex;
    let tiltle;
    if (type == 1) {
        //新增
        tiltle = "新增监测点评级"
    } else {
        tiltle = "修改监测点评级"
    }
    op2 = layer.open({
        type: 1,
        title: tiltle,
        shadeClose: true,
        resize: false,
        area: ['20%', '28%'],
        content: '<div class="form-inline" style="padding: 15px;text-align: center">' +
            '<label class="form-group">监测点评级：</label><select id="lineGradeSelect" class="form-control"></select></div>' +
            '<div class="form-inline mt15" style="text-align: center"><button id="submit" class="btn btn-primary">提交</button></div>',
        success: function () {
            var 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].dicIndex + "'>"
                            + data[i].dicName + "</option>";
                    }

                    if (type == 1) {
                        //新增

                    } else {
                        option+="<option value=''>清空</option>";
                    }
                    $("#lineGradeSelect").empty()
                    $("#lineGradeSelect").append(option)
                    if (type == 1) {
                        //新增

                    } else {
                        $.ajax({
                            method: "get",
                            url: "/pqs9900/lineMark/getLineGrade",
                            data: {
                                lineIndex: lineIndex
                            },
                            dataType: 'json',
                            success: function (data) {
                                if (data.code == 200) {
                                    $("#lineGradeSelect").val(data.body.lineGrade)
                                } else {
                                    layer.msg(data.message, {icon: 2})
                                }

                            }
                        })

                    }
                }
            })

        }
    })
}

//提交操作
$("body").on('click', "#submit", function () {
    let lineGrade = $("#lineGradeSelect").val()
    $.ajax({
        type: "POST",
        url: "/pqs9900/lineMark/singleOperate",
        data: {lineIndex: lineId, lineGrade: lineGrade},
        dataType: 'json',
        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();
}

$(document).on("click", "#templateExport", function () {
    var str = "省份,供电公司,所属变电站,装置名称,监测点名称,监测点评级";
    var aaaa = "data:text/csv;charset=gbk,\ufeff" + str;
    var link = document.createElement("a");
    link.setAttribute("href", aaaa);
    link.setAttribute("download", "监测点台账信息模板" + ".csv");
    link.click();
});

$(document).on("click", "#stopLineMark", function () {
    $.ajax({
        method: "get",
        url: "/pqs9900/lineMark/batchUpdateLineMarkStop",
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                layer.msg(data.message, {icon: 1, time: 2000},function () {
                    initTableData()
                })

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

        }


    })
});

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)
        }
    })
}

