var layerId = -1;
var objTable = null;

$(document).ready(function () {
    query();
    importLoading();
    deleteLoading();
    window.setInterval("importLoading()", 60000);
    window.setInterval("deleteLoading()", 60000);
});

$('#query').click(function () {
    query();
});

function query() {
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    queryTranslateState(startTime, endTime);
}

var deleteLay = null;
var importLay = null;


function importLoading() {
    $.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/device/queryimportLoading',
        success: function (data) {
            if (importLay != null) {
                layer.close(importLay);
                query();
            }

            if (deleteLay != null) {
                layer.close(deleteLay);
                query();
            }

            if (data.code === 0) { //弹框提示
                var msg = '当前理论数据' + data.body.allCount + '条，入库' + data.body.realCount + '条';

                importLay = layer.confirm(msg, {
                    btn: []
                    , icon: 16
                    , shade: 0.01
                    , time: '-1'
                });
            }
        }
    });
}

function deleteLoading() {
    $.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/device/deleteLoading',
        success: function (data) {
            if (deleteLay != null) {
                layer.close(deleteLay);
                query();
            }

            if (importLay != null) {
                layer.close(importLay);
                query();
            }

            if (data.code === 0) { //弹框提示
                deleteLay = layer.confirm('后台数据删除中', {
                    btn: []
                    , icon: 16
                    , shade: 0.01
                    , time: '-1'
                });
            }
        }
    });
}

function queryTranslateState(startTime, endTime) {
    layerId = SHOW_LOAD_LAYER(layer);

    $.ajax({
        type: 'POST',
        dataType: "json",
        data: {
            start: startTime,
            end: endTime
        },
        url: '/pqs9900/device/queryTranslateState',
        success: function (data) {
            var d = data.body === null ? [] : data.body;
            initTable(d);
        },
        error: function () {
            layer.msg("离线数据解析状态列表查询失败！");
            initTable([]);
        }
    });
}

//搜索框内容改变结果记录
function valueChange(value) {
    objTable.search(value).draw();
}

function initTable(data) {
    CLOSE_LOAD_LAYER(layer, layerId);
    var tableData = '';

    for (var i = 0; i < data.length; i++) {
        var color = '#000000';

        switch (data[i].state) {
            case 0:
                color = '#ffff00';
                break;
            case 2:
            case 3:
                color = 'red';
                break;
            case 4:
                color = '#3f48cc';
                break;
        }

        tableData += '["' + data[i].guid + '","","'
            + data[i].imptime + '","'
            + data[i].time + '","'
            + data[i].gdName + '","'
            + data[i].bdName + '","'
            + data[i].pointName + '","'
            + data[i].fileName + '","'
            + data[i].allCount + '","'
            + data[i].realCount + '","'
            + "<div style='color:" + color + "'>"
            + data[i].describe + '</div>"'
            + ',""],';
    }

    tableData = tableData.substring(0, tableData.lastIndexOf(','));
    tableData = eval('[' + tableData + ']');

    objTable = $('#tableId')
        .DataTable({
            data: tableData,
            lengthChange: false,
            bDestroy: true,
            scrollX: true,
            autowidth: true,
            order: [[2, 'desc']],
            columnDefs: [{
                targets: [0],
                visible: false
            }, {
                targets: [11],
                orderable: false
            }],
            'searching': true,
            dom: 'lrt<<"myStyle"ip>>',
            ordering: true,
            scrollCollapse: true,
            language: {
                url: '/pqs9900/json/chine.json'
            }, createdRow: function (row, data, index) {
                var $btnView = $('<button type="button" class="btn themeBGColor translate">删除</button>');
                $('td', row).eq(10).append($btnView);

                if (data[7].length > 5) {
                    var realValue = data[7];
                    var s = data[7].substr(0, 5) + '...';
                    $('td', row).eq(6).prevObject[6].innerHTML = "<span title='" + realValue + "'>" + s + "</span>";
                }
            },
        });

    objTable.on('draw.dt', function () {
        objTable.column(1, {
            draw: 'applied',
        }).nodes().each(function (cell, i) {
            cell.innerHTML = i + 1;
        });
    }).draw();
}

$('#tableId').on("click", ".translate", function () {
    if (objTable != null) {
        var item = objTable.row($(this).closest('tr')).data();
    }

    var layid = layer.confirm('是否确认删除？', {
        btn: ['删除', '取消']
    }, function () {
        layer.close(layid);

        $.ajax({
            type: 'POST',
            async: false,
            data: {
                guid: item[0]
            },
            url: '/pqs9900/device/deleteTranslate',
            success: function (data) {
                deleteLay = layer.confirm('后台数据删除中', {
                    btn: []
                    , icon: 16
                    , shade: 0.01
                    , time: '-1'
                });

                if (data.code === 0) {
                    layer.msg("后台数据删除中，请稍候重试");
                }
            }
        });
    }, function () {
        layer.close(layid);
    });
});