/**
 * 引入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 logList = [];

/**
 * 获取日志列表
 */
function getLogList(count, page, keyword, fromDate, endDate) {
    $('#loadingDiv').show();
    $("#logList-body").empty();
    logList = [];
    $.get('log/errPage', {
        count: count,
        page: page,
        keyword: keyword,
        fromDate: fromDate,
        endDate: endDate
    }, function (data) {
        $('#loadingDiv').hide();
        logList = data.content;
        var pageNumber = data.number;
        var pageSize = data.size;
        var total = data.totalElements;
        var totalPage = data.totalPages;
        var tbody = document.getElementById("logList-body");
        var page = document.getElementById("page");
        //noinspection JSAnnotator
        $('#logList-total').val('共 ' + total + ' 条');
        if (logList.length > 0) {
            for (var i = 0; i < logList.length; i++) {
                var trow = getDataRow(logList[i], i); //定义一个方法,返回tr数据
                tbody.appendChild(trow);
                // 查询错误详情
                (function(i) {
                    $("#para_detail_" + logList[i].id ).click(function () {
                        getPara(logList[i].id, logList[i].enName, logList[i].url);
                        $('#loadingDiv').show();
                    });
                })(i);
                // 查询参数详情
                (function(i) {
                    $("#pa_detail_" + logList[i].id ).click(function () {
                        getDetail(logList[i].detail, logList[i].enName);
                    });
                })(i);
                // 查询错误列表
                (function(i) {
                    if (logList[i].uuid != null && logList[i].uuid != "") {
                        $("#not_exit_data_" + logList[i].id ).click(function () {
                            getNotExistData(logList[i].uuid, logList[i].msg, logList[i].enName);
                            $('#loadingDiv').show();
                        });
                    }
                })(i);
            }

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

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

/**
 * 获取参数详情
 *
 * @param id 主表id
 */
function getPara(id, name, url) {
    $.get('log/para', {id: id}, function (data) {
        $('#para-content').empty();
        $('#para-title').empty();
        $("<p style='font-size: 16px; font-weight: bold'>").text(name).appendTo("#para-title");
        var result = JSON.stringify(JSON.parse(data.content), null, 4);
        $("<pre>").html(syntaxHighlight(result)).appendTo("#para-content");
        $('#loadingDiv').hide();
        $('#paraDetail').modal('show');
    });
}

/**
 * 获取详情
 */
function getDetail(detail, name) {
    $('#para-content').empty();
    $('#para-title').empty();
    $("<p style='font-size: 16px; font-weight: bold'>").text(name).appendTo("#para-title");
    var result = JSON.stringify(JSON.parse(detail), null, 4);
    $("<pre>").html(syntaxHighlight(result)).appendTo("#para-content");
    $('#loadingDiv').hide();
    $('#paraDetail').modal('show');
}

/**
 * 获取错误列表
 */
function getNotExistData(uuid, msg, name) {
    $.get('log/notExistData', {sign: uuid, msg: msg}, function (data) {
        $('#para-content').empty();
        $('#para-title').empty();
        $("<p style='font-size: 16px; font-weight: bold'>").text(name).appendTo("#para-title");
        var result = JSON.stringify(JSON.parse(data.content), null, 4);
        $("<pre>").html(syntaxHighlight(result)).appendTo("#para-content");
        $('#loadingDiv').hide();
        $('#paraDetail').modal('show');
    });
}

/**
 * 建立表格数据
 *
 * @param log 日志
 * @param i 行号
 * @returns {Element}
 */
function getDataRow(log, 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 dateCell = document.createElement('td'); //时间
    dateCell.setAttribute("class", "text-center");
    dateCell.innerHTML = formatDateTime(log.date); //填充数据
    row.appendChild(dateCell);

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

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

    var urlCell = document.createElement('td'); //enName
    urlCell.setAttribute("class", "text-center");
    urlCell.innerHTML = log.url; //填充数据
    row.appendChild(urlCell);

    var msgCell = document.createElement('td'); //msg
    msgCell.setAttribute("class", "text-center");
    msgCell.setAttribute("style", "max-height: 400px; overflow-y: auto;");
    msgCell.innerHTML = log.msg; //填充数据
    if (log.uuid != null && log.uuid != "") {
        msgCell.setAttribute("title", "查看详情");
        msgCell.setAttribute("id", "not_exit_data_" + log.id);
        var button = "<a style='color: red'><i class='icon-edit'>more>></i></a>";
        msgCell.innerHTML += button;
    }
    row.appendChild(msgCell);

    var detailCell = document.createElement('td'); //detail
    detailCell.setAttribute("class", "text-center");
    detailCell.setAttribute("title", log.detail);
    detailCell.setAttribute("id", "pa_detail_" + log.id);
    detailCell.setAttribute("title", "查看详情");
    detailCell.innerHTML = "详情"; //填充数据
    row.appendChild(detailCell);

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

    return row;
}

$(function() {
    'use strict';

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

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