/**
 * 首页终端异常
 */
var i;

function reflush() {
    $.ajax({
        type: "POST",
        url: "/pqs9200/device/getSbyc",
        dataType: "text",
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            //没有权限的请求
            if (data == 300) {
                location.href = '/pqs9200/auth/unauthorized';
                //没有登陆的请求
            } else if (data == 301) {
                location.href = '/pqs9200';
            } else {
                var info = $.parseJSON(data);
                if (info.code == 500) {
                    //没有数据
                } else {
                    /**
                     * 处理数据写表格
                     */
                    var JSONObj = '[';

                    $.each(info.body, function (index, item) {
                        if (index + 1 == info.body.length) {
                            JSONObj += str2JSON(index + 1, item[0], item[1],
                                item[3], item[4], item[5],
                                item[6], item[7], item[8],
                                item[9], item[10], item[11]) + ']';
                            return false;
                        }

                        JSONObj += str2JSON(index + 1, item[0], item[1],
                            item[3], item[4], item[5],
                            item[6], item[7], item[8],
                            item[9], item[10], item[11]) + ',';
                    });

                    initTable($.parseJSON(JSONObj));
                }
            }
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    });
}

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

/**
 * @param data
 * @returns
 * 获取kfka推送消息并添加到dataTable表格中
 */
var table = null;

function getKfkaData(data) {
    var tableData = table.cache[1];
    var length = tableData.length;
    var table1 = layui.table;
    //处理kfka数据
    var obj = new Object();//定义新的对象按照格式存放数据
    if (data.comState == 0) {
        var statuss = data.comState;
        obj.gdname = data.gdName;
        obj.name = data.deviceName;
        obj.subname = data.subName;
        obj.time = data.connectTime;
        obj.devtype = data.deviceType;
        obj.ip = data.deviceIP;
        obj.portid = data.portID;
        obj.status = "<font style='color: #FF2D2D'>" + "中断" + "</font>";
        obj.updatetime = data.updateTime;

        tableData.unshift(obj);

        if (length == 100) {
            tableData.pop();
        }

        obj = null; //销毁new出来的对象

        initTable(tableData);
    } else if (data.comState == 1) {
        for (var i = 0; i < table1.cache[1].length; i++) {
            if (table1.cache[1][i].name == data.deviceName) {
                reflush();
            }
        }
    }
}

/**
 * @param data
 * @returns
 * 初始化表格
 */
function initTable(data) {
    layui.use('table', function () {
        table = layui.table;

        //展示已知数据
        table.render({
            elem: '#deviceState'
            , data: data
            , height: 200
            , cols: [[ //标题栏
                {field: 'id', title: '序号', width: window.screen.width * 0.05, sort: true, align: 'center'},
                {field: 'gdname', title: '供电公司', width: window.screen.width * 0.12, sort: true, align: 'center'},
                {field: 'name', title: '终端名称', width: window.screen.width * 0.12, sort: true, align: 'center'},
                {field: 'subname', title: '变电站', width: window.screen.width * 0.12, sort: true, align: 'center'},
                {field: 'time', title: '投运时间', width: window.screen.width * 0.12, sort: true, align: 'center'},
                {field: 'devtype', title: '终端型号', width: window.screen.width * 0.12, sort: true, align: 'center'},
                {field: 'ip', title: 'ip地址', width: window.screen.width * 0.10, sort: true, align: 'center'},
                {field: 'portid', title: '端口', width: window.screen.width * 0.07, sort: true, align: 'center'},
                {
                    field: 'status',
                    title: '通讯状态',
                    width: window.screen.width * 0.10,
                    sort: true,
                    align: 'center',
                    templet: '#stateTpl',
                },
                {field: 'updatetime', title: '中断时间', width: window.screen.width * 0.12, sort: true, align: 'center'}
            ]]
            , skin: 'line,row' //表格风格
            , page: false //是否显示分页
            , limit: data.length,
            even: true,
            language: {
                url: '../json/Chinese.json'
            }
        });
    });
}

/**
 * @param str1
 * @param str2
 * @param str3
 * @returns
 * string转JSON
 */
function str2JSON(id, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10) {
    var JSONstr = '{"id":' + id + ',"gdname":"' + str1
        + '","name":"' + str2 + '","subname":"' + str3
        + '","time":"' + str4 + '","devtype":"' + str5
        + '","ip":"' + str6 + '","portid":"' + str7
        + '","status":"' + str8 + '","updatetime":"' + str9
        + '","evaluate":"' + str10 + '"}';
    return JSONstr;
}
