var dataTable = null;
var resultData = null;
var areaInfo;
$("#interval").val("月份");
$(function () {
    initselect()
    refresh();
})


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>";

            }
            $("#selectId").empty()
            $("#selectId").append(option)
            $("#selectId").val(0)
        }
    })
}

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

//获取表格数据
function refresh() {
    let startTime = $("#startTime").val();
    let endTime = $("#endTime").val();
    var lineGrade = $("#selectId").val();
    var isRight =$("#isRight").val()

    var i;
    $.ajax({
        type: "get",
        url: "/pqs9900/inStrategy/getOnlineRateCldList",
        dataType: "json",
        data: {
            startTime: startTime,
            endTime: endTime,
            lineGrade:lineGrade,
            colorFlag:isRight
        },
        beforeSend: function () {
            i = 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;
            }
            startQuery();
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    });
}

//实例化表格
function initTable(data) {
    var height = $(window).height() - 155;
    var length = Math.floor(height / 28);
    if (length < 1) {
        length = 1;
    }
    dataTable = $("#onlineRatelist").DataTable();
    dataTable.clear().draw();
    dataTable = $('#onlineRatelist').DataTable({
        order: [1, "asc"],
        columnDefs: [
            {
                orderable: false,
                targets: [0]
            }
        ],
        columns: [
            {width: "5%", data: null},
            {width: "10%", data: "provinceName"},
            {width: "10%", data: "gdName"},
            {width: "16%", data: "subName"},
            {width: "16%", data: "devName"},
            {
                width: "10%", data: "onlineRate",
                render: function (data, type, full, meta) {
                    var temp = Number(data);
                    if (temp > 100) {
                        return 100;
                    } else if (temp < 0) {
                        return 0;
                    } else {
                        return temp;
                    }
                }
            },
            {width: "10%", data: "offCount"},
            {
                width: "8%", data: "lineGrade",
                render: function (data, type, full, meta) {

                    if (data == 0) {
                        return '极重要';
                    } else if (data == 1) {
                        return '重要';
                    } else if (data == 2) {
                        return '普通';
                    } else if (data == 3) {
                        return '不重要';
                    } else {
                        return '/';
                    }
                }
            }

        ],
        destroy: true,
        data: data,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollY: height,
        // scrollX: false,
        lengthMenu: [length],
        language: {
            url: '/pqs9900/json/chine.json'
        },
        createdRow: function (row, data, index) {
            if (data.colorFlag == 0) {
                $('td', row).eq(5).css('color', '#ED1C24')
            }


        },
        fnDrawCallback: function (oTable) {
            $("#onlineRatelist_info").addClass("fl");
            $("#onlineRatelist_paginate").addClass("fr");
            $("#onlineRatelist_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 = $("#onlineRatelist").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',
                title:'终端在线率'+new Date().getTime(),//导出csv名称
                exportOptions: {
                    columns: [0, 1, 2, 3, 4, 5, 6,7],
                    format: {
                        body: function (data, rID, cID) {
                            if (cID === 0) {
                                return rID + 1;
                            } else {
                                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]);
}

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

//特殊处理，表头紧缩问题--start
var it = null;

function startQuery() {
    //实时刷新时间单位为毫秒
    it = setInterval('refreshQuery()', 100);

}

/* 刷新查询 */
function refreshQuery() {
    var width = $("#onlineRatelist").width();
    if (width > 100) {
        initTable(resultData);
        window.clearInterval(it)
    }
}

//特殊处理，表头紧缩问题--end
$("#infomation").click(function () {
    layer.open({
        type: 2,
        title: '告警策略查看',
        maxmin : false,
        shadeClose: true, //点击遮罩关闭层
        area : ['100%' , '100%'],
        content : '/pqs9900/business/terminalrunstrategy',
        scrollbar : false

    });
})
