/**
 * 引入toaster方法
 */
document.write("<script language=javascript src='static/js/common/toastr.js'></script>");

/**
 * 引入分页
 */
document.write("<script language=javascript src='static/lib/jquery/jquery.pagination.js'></script>");

/**
 * 引入base方法
 */
document.write("<script language=javascript src='static/js/common/base.js'></script>");

var listenList = [];

/**
 * 获取监听列表
 */
function getlistenList(count, page, keyword) {
    $('#loadingDiv').show();
    $("#listen-body").empty();
    listenList = [];
    $.get('/listen/list', {
        count: count,
        page: page,
        keyword: keyword,
    }, function (data) {
        $('#loadingDiv').hide();
        listenList = data.data;
        var pageNumber = data.page;
        var pageSize = data.size;
        var total = data.totalElements;
        var totalPage = data.totalPages;
        var tbody = document.getElementById("listen-body");
        var page = document.getElementById("page");
        //noinspection JSAnnotator
        $('#listen-total').val('共 ' + total + ' 条');
        if (listenList.length > 0) {
            for (var i = 0; i < listenList.length; i++) {
                var trow = getDataRow(listenList[i], i); //定义一个方法,返回tr数据
                tbody.appendChild(trow);
            }

            // 点击搜索
            $('#p_search').unbind('click').click(function () {// 每次先解除上次绑定的事件，防止重复发送请求
                $("#listen-body").load(location.href + " #listen-body");
                getlistenList(count, 1, $('#keyword').val());
            });

            // 键盘确认按钮搜索
            document.onkeydown = function(event) {
                var e = event || window.event;
                if (e && e.keyCode == 13) { // enter 键
                    $("#listen-body").load(location.href + " #listen-body");
                    getlistenList(count, 1, $('#keyword').val());
                }
            };
        }
        $('#m-page').pagination({
            pageCount: totalPage,
            totalData: total,
            current: pageNumber,
            showData: pageSize,
            coping: true,
            homePage: '首页',
            endPage: '末页',
            prevContent: '<<',
            nextContent: '>>',
            jump: true,
            jumpBtn: '跳转',
            callback: function (api) {
                $('.now').text(api.getCurrent());
                $("#listen-body").load(location.href + " #listen-body");
                getlistenList(count, api.getCurrent(), $('#keyword').val());
            }
        }, function (api) {
            $('.now').text(api.getCurrent());
        });
    })
}

/**
 * 建立表格数据
 *
 * @param log 日志
 * @param i 行号
 * @returns {Element}
 */
function getDataRow(listen, i) {
    var keyword = $('#keyword').val();

    var row = document.createElement('tr'); //创建行

    var indexCell = document.createElement('td'); //序号
    indexCell.setAttribute("class", "text-center");
    indexCell.innerHTML = i + 1; //填充数据
    row.appendChild(indexCell);

    var macAddressCell = document.createElement('td'); //macAddress
    macAddressCell.setAttribute("class", "text-center");
    macAddressCell.innerHTML = highLightKeywords(listen.macAddress); //填充数据
    row.appendChild(macAddressCell);


    var nameCell = document.createElement('td'); //name line-limit-length
    nameCell.setAttribute("class", "text-center line-limit-length");
    //nameCell.setAttribute("class", "line-limit-length");
    nameCell.setAttribute("data-toggle", "tooltip");
    nameCell.setAttribute("data-placement", "right");
    nameCell.setAttribute("title", listen.name);
    nameCell.innerHTML = highLightKeywords(listen.name, keyword, null); //填充数据
    row.appendChild(nameCell);

    var dateCell = document.createElement('td'); //更新日期
    dateCell.setAttribute("class", "text-center");
    if (listen.date != null) {
        dateCell.innerHTML = formatDateTime(listen.date); //填充数据
    } else {
        dateCell.innerHTML = '-';
    }
    row.appendChild(dateCell);

    var statusCell = document.createElement('td'); //状态
    statusCell.setAttribute("class", "text-center fonts-color");
    statusCell.setAttribute("data-toggle", "tooltip");
    statusCell.setAttribute("data-placement", "right");
    statusCell.setAttribute("title", listen.description);
    statusCell.innerHTML = highLightKeywords(listen.status, "异常", null); //填充数据
    row.appendChild(statusCell);

    return row;
}

$(function() {
    'use strict';

    // 监听页面滚动
    $(window).scroll(function() {
        if($(window).scrollTop() >= 400) {
            $('#nav').addClass('on');
        } else {
            $('#nav').removeClass('on');
        }
    });

    // 设置分页大小
    var count = 20;
    getlistenList(count, 1, null);
});
