/**
 * 终端更改日志
 */
var dataTable = null;
var resultData = null;
$(document).ready(function () {
    load();
});

function load() {
    var i;
    $.ajax({
        type: "post",
        url: "/pqs9000/device/getLogsDetail",
        data: {},
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        error: function (data) {
            layer.msg("暂无数据", {
                icon: 5,
                time: 3000
            });
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        },
        success: function (data) {
            if (data == "") {
                resultData = null;
                //没有数据
                initTable(null);
            } else {
                var info = $.parseJSON(data);
                if (info.code == 500) {
                    resultData = null;
                    //没有数据
                    initTable(null);
                } else {
                    resultData = info;
                    initTable(info);
                }
            }
            startQuery();
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    });
}
function initTable(data) {
    var height = $(window).height() - 160;
    var length = Math.floor(height / 28);
    if (length < 1) {
        length = 1;
    }
    dataTable = $("#terminallogid").DataTable();
    dataTable.clear().draw();
    dataTable = $('#terminallogid').DataTable({
        // order: [[10, 'asc']],
        columnDefs: [
            {
                orderable: false,
                targets: [0,2,3]
            }
        ],
        autoWidth: false,
        serverSide: false,
        destroy: true,
        data: data,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollY: height,
        scrollX: true,
        pageLength: length,
        language: {
            url: '/pqs9000/json/chine.json'
        },
        fnDrawCallback: function (oTable) {
            $("#terminallogid_info").addClass("fl");
            $("#terminallogid_paginate").addClass("fr");
            $("#terminallogid_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 = $("#terminallogid").dataTable();
            $('#dataTable-btn').click(function (e) {
                if ($("#changePage").val() && $("#changePage").val() > 0) {
                    var redirectpage = $("#changePage").val() - 1;
                } else {
                    var redirectpage = 0;
                }
                table.fnPageChange(redirectpage);
            });
        }
    });
    dataTable.on('order.dt search.dt',
        function () {
            dataTable.column(0, {
                search: 'applied',
                order: 'applied'
            }).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        }).draw();
    new $.fn.dataTable.Buttons(dataTable, {
        buttons: [
            {
                extend: 'csv',//导出格式
                title: '终端更改日志列表',//导出csv名称
                exportOptions: {
                    columns: [1, 2, 3, 4],//导出需要的列
                    format: {
                        body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                            return data;
                        }
                    }
                }
            }
        ]
    });
    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 = $("#terminallogid").width();
    if (width > 100) {
        initTable(resultData);
        window.clearInterval(it)
    }
}

//特殊处理，表头紧缩问题--end