/**
 * 终端状态
 */
var name = $("#sel").val();
var dataTable = null;
var i;
var resultData = null;

var devIndexs = null;
function getFlowDetail() {
    $.ajax({
        type: "POST",
        url: "/pqs9900/devstate/getDevStateDetail",
        data: '',
        async: false,
        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);
        }
    })
}

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

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

}

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


//特殊处理，表头紧缩问题--end
var height = $(window).height() - 30;

//实例化暂降事件表格
function initTable(data) {
    var height = $(window).height() - 155;
    var length = Math.floor(height / 28);
    if (length < 1) {
        length = 1;
    }
    // var heightTemp = screen.height;
    // var length;
    // if (heightTemp === 768) {
    //     length = 10;
    // } else if (heightTemp === 1024) {
    //     length = 20;
    // } else if (heightTemp === 960) {
    //     length = 15;
    // } else if (heightTemp === 1050) {
    //     length = 18;
    // } else {
    //     length = 15;
    // }
    dataTable = $("#example").DataTable();
    dataTable.clear().draw();
    dataTable = $('#example').DataTable({
        columnDefs: [
            {
                orderable: false,
                targets: [3, 8]
            }, {
                //visible: false, targets: 0
            }
        ],
        columns: [
            {width: "10%", data: "gdName"},
            {width: "10%", data: "subName"},
            {width: "10%", data: "devName"},
            {width: "10%", data: "ip"},
            {width: "7%", data: "cpuOne"},
            {width: "6%", data: "cpuTwo"},
            {width: "6%", data: "ram"},
            {width: "6%", data: "spaceMemory"},
            {
                width: "10%", data: "devIndex",
                render: function (data, type, full, meta) {
                    var bxck;
                    bxck = "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);'  onclick='getDetail(\"" + data + "\")' >查看详情</button>";
                    // uploadck = "<button type='button' class='btn themeBGColor'" +
                    //     " style='margin-right:10px;' href='javascript:void(0);' onclick='flush(\"" + data +
                    //     "\")' >信息更新</button>";

                    return bxck;
                }
            }
        ],
        "createdRow": function (row, data, index) {
            if (data.subName.length > 8) {
                var realValue = data.subName;
                var s = data.subName.substr(0, 8) + '...'
                $('td', row).eq(1).prevObject[1].innerHTML = "<span title='" + realValue + "'>" + s + "</span>";
            }
        },
        destroy: true,
        data: data,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollX: true,
        pageLength: length,
        language: {
            url: '/pqs9900/json/chine.json'
        }
    });
    new $.fn.dataTable.Buttons(dataTable, {
        buttons: [
            {
                extend: 'csv',
                title: '终端状态列表',//导出csv名称
                exportOptions: {
                    columns: [0, 1, 2, 3, 4, 5, 6, 7, 8]
                }
            }
        ]
    });
    modifyCss(dataTable);
}

$(document).ready(function () {
    getFlowDetail();
});

function modifyCss(table) {
    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();
}

function getDetail(devIndex) {
    devIndexs = devIndex;
    layer.open({
        type: 2,
        title: '终端状态统计',
        shadeClose: true,
        resize: false,
        shade: 0.3,
        area: ['100%', '100%'],
        content: 'devStateinfo', //iframe的url
        scrollbar: false,
        end: function () {
            layer.close();
        }
    });
}