var dataTable;
var resultData = null;
var areaInfo;
var globalLineGrade;
var globalArea;


var height = $(window).height() - 135;
$("#interval").val("月份");

/**
 * 电压等级排序自定义比较end
 */
$(function () {
    initselect()
    refresh();
    $.ajax({
        type: "POST",
        url: "/pqs9900/theme/getThemeInfo",
        async: false,
        dataType: 'json',
        success: function (data) {
            companyname = data.body.name;
        }
    });




});

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

//获取表格数据
function refresh(area, startTime, endTime) {
    var startTime =$("#startTime").val();
    var endTime = $("#endTime").val();
    var lineGrade = $("#selectId").val();
    globalLineGrade = lineGrade
    var i;
    $.ajax({
        type: "POST",
        url: "/pqs9900/inStrategy/getRunExceptionList",
        dataType: "json",
        data: {
            startTime: startTime,
            endTime: endTime,
            lineGrade:lineGrade
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            if (data.code === 500) {
                resultData = null
                layer.msg(data.message, {icon: 2, time: 2000})
                //没有数据
            } else {
                resultData = data.body
            }
            startQuery();
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    });
}

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

    var length = Math.floor(height / 28);
    if (length < 1) {
        length = 1;
    }
    dataTable = $("#runException").DataTable();
    dataTable.clear().draw();
    dataTable = $('#runException').DataTable({
        order: [0, "desc"],
        columnDefs: [
            {
                orderable: false,
                targets: [2]
            }
        ],
        columns: [

            {width: "25%", data: "timeId", render:function (data, type, full, meta) {
                    return formatTime(data,2);
                }},


            {width: "50%", data: null, render:function (data, type, full, meta) {
                   let time = formatTime(full.timeId,2)
                    return time + ' 终端异常共 '+full.countes+' 台';
                }},


            {width: "25%", data: null, render:function (data, type, full, meta) {
                    let time = formatTime(full.timeId,2)
                    return '<button class="btn btn-primary" onclick="detailInfo(\''+time+'\')">查看详情</button>';
                }},
        ],
        destroy: true,
        data: data,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollY: height,
        scrollX: true,
        lengthMenu: [length],
        language: {
            url: '/pqs9900/json/chine.json'
        },
        fnDrawCallback: function (oTable) {
            $("#runException_info").addClass("fl");
            $("#runException_paginate").addClass("fr");
            $("#runException_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 = $("#runException").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:'终端运行通告',//导出csv名称
                exportOptions: {
                    columns: [0, 1]
                }
            }
        ]
    });
    modifyCss(dataTable);
}

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 = $("#runException").width();
    if (width > 100) {
        initTable(resultData);
        window.clearInterval(it)
    }
}

//特殊处理，表头紧缩问题--end
/**
 * 电压等级排序自定义比较start
 */
jQuery.fn.dataTableExt.oSort['number-fate-asc'] = function (s1, s2) {
    s1 = s1.replace('kV', '');
    s2 = s2.replace('kV', '');
    return Number(s1) - Number(s2);
};

jQuery.fn.dataTableExt.oSort['number-fate-desc'] = function (s1, s2) {
    s1 = s1.replace('kV', '');
    s2 = s2.replace('kV', '');
    return Number(s2) - Number(s1);
};



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

//查看详情
function detailInfo(timeId) {
    layer.open({
        type: 2,
        title: timeId+'日 终端异常通告详情 ',
        maxmin : false,
        area : ['100%' , '100%'],
        content: 'terminalrundetailes?timeId='+timeId+'&lineGrade='+globalLineGrade,
        scrollbar: false
    });
}
