/**
 * 引入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>");

/**
 * 初始化noticeList
 *
 * @type {Array}
 */
var noticeList = [];
/**
 * 获取日志列表
 */
function getNoticeList(count, page, keyword, method) {
    $('#loadingDiv').show();
    $("#noticeList-body").empty();
    logList = [];
    $.get('noticeRecord/list', {
        count: count,
        page: page,
        keyword: keyword,
        method: method
    }, function (data) {
        $('#loadingDiv').hide();
        noticeList = data.content;
        var pageNumber = data.number;
        var pageSize = data.size;
        var total = data.totalElements;
        var totalPage = data.totalPages;
        var tbody = document.getElementById("noticeList-body");
        var page = document.getElementById("page");
        var method = document.getElementById("method");
        //noinspection JSAnnotator
        $('#noticeList-total').val('共 ' + total + ' 条');
        if (noticeList.length > 0) {
            for (var i = 0; i < noticeList.length; i++) {
                var trow = getDataRow(noticeList[i], i); //定义一个方法,返回tr数据
                tbody.appendChild(trow);
                // 查询详情
                (function(i) {
                    $("#notice_detail_" + noticeList[i].id ).click(function () {
                        getDetail(noticeList[i].id);
                        $('#loadingDiv').show();
                    });
                })(i);
            }

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

            // 键盘确认按钮搜索
            document.onkeydown = function(event) {
                var e = event || window.event;
                if (e && e.keyCode == 13) { // enter 键
                    $("#logList-body").load(location.href + " #logList-body");
                    getNoticeList(count, 1, $('#keyword').val(), method);
                }
            };
        }
        $('#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());
                $("#noticeList-body").load(location.href + " #logList-body");
                getNoticeList(count, api.getCurrent(), $('#keyword').val(), method);
            }
        }, function (api) {
            $('.now').text(api.getCurrent());
        });
    })
}

/**
 * 获取详情
 */
function getDetail(id) {
    window.location.href = "/notice_detail?id=" + id;
}

/**
 * 建立表格数据
 *
 * @param notice 内容
 * @param i 行数
 * @returns {Element}
 */
function getDataRow(notice, i) {
    var keyword = $('#keyword').val();

    var row = document.createElement('tr'); //创建行
    /*if (notice.errorCount > 0) {
        row.setAttribute('class', 'danger');
    }*/

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

    var dateCell = document.createElement('td'); //时间
    dateCell.setAttribute("class", "text-center");
    dateCell.innerHTML = formatDateTime(notice.date); //填充数据
    row.appendChild(dateCell);

    var uuCell = document.createElement('td'); //enUU
    uuCell.setAttribute("class", "text-center");
    uuCell.innerHTML = highLightKeywords(notice.enUU, keyword, null); //填充数据
    row.appendChild(uuCell);

    var nameCell = document.createElement('td'); //enName
    nameCell.setAttribute("class", "text-center");
    nameCell.innerHTML = highLightKeywords(notice.enName, keyword, null); //填充数据
    row.appendChild(nameCell);

    var describeCell = document.createElement('td'); //备注
    describeCell.setAttribute("class", "text-center");
    describeCell.innerHTML = notice.describe; //填充数据
    row.appendChild(describeCell);

    var msgCell = document.createElement('td'); //msg
    msgCell.setAttribute("class", "text-center");
    if (notice.errorCount == 0) {
        msgCell.innerHTML = '数据正常';
    } else  {
        msgCell.innerHTML = '存在异常数据' + '<span style="color: red;"><strong>' + notice.errorCount  + '</strong></span>' + '条，请注意查看';
    }
    row.appendChild(msgCell);

    var hanldeCell = document.createElement('td'); //parameters
    hanldeCell.setAttribute("class", "text-center");
    hanldeCell.setAttribute("title", "查看参数详情");
    hanldeCell.innerHTML = "详情"; //填充数据
    hanldeCell.setAttribute("id", "notice_detail_" + notice.id)
    row.appendChild(hanldeCell);

    return row;
}

$(function() {
    'use strict';

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

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