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

/**
 * 获取日志列表
 */
function getList(count, page, keyword) {
    $('#loadingDiv').show();
    $("#logList-body").empty();
    logList = [];
    $.get('schedular/list', {
        count: count,
        page: page,
        keyword: keyword,
    }, function (data) {
        $('#loadingDiv').hide();
        schedularList = 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 (schedularList.length > 0) {
            for (var i = 0; i < schedularList.length; i++) {
                var trow = getDataRow(schedularList[i], i); //定义一个方法,返回tr数据
                tbody.appendChild(trow);
                // 编辑
                (function(i) {
                    $("#edit_" + schedularList[i].id ).click(function () {
                        editInfo(schedularList[i]);
                    });
                })(i);
                // 访问
                (function(i) {
                    $("#link_schedular_" + schedularList[i].id ).click(function () {
                       window.open(schedularList[i].url);
                    });
                })(i);
                // 删除
                (function(i) {
                    $("#delete_" + schedularList[i].id ).click(function () {
                        deleteInfo(schedularList[i].id);
                    });
                })(i);
            }

            // 点击搜索
            $('#p_search').unbind('click').click(function () {// 每次先解除上次绑定的事件，防止重复发送请求
                $("#logList-body").load(location.href + " #logList-body");
                getList(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");
                    getList(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");
                getList(count, api.getCurrent(), $('#keyword').val());
            }
        }, function (api) {
            $('.now').text(api.getCurrent());
        });
    })
}

/**
 * 编辑
 *
 * @param id 主表id
 */
var id = null;
function editInfo(schedular) {
    $('#para-title').empty();
    $("<p style='font-size: 16px; font-weight: bold'>").text(schedular.name).appendTo("#para-title");
    $('#schedularInfo').modal('show');
    $("#sc_name").val(schedular.name);
    $("#sc_url").val(schedular.url);
    id = schedular.id;
}

/**
 * 删除
 *
 * @param id id
 */
function deleteInfo(id) {
    $.ajax('schedular/delete/' + id, {
        method: 'POST',
        async: false,
        success: function() {
            alert('删除成功');
            getList(20, 1, null);
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
}

/**
 * 新增
 */
$('#addInfo').click(function () {
    $('#para-title').empty();
    $('#sc_url').val('');
    $('#sc_name').val('');
    $('#schedularInfo').modal('show');
    id = null;
    $("<p style='font-size: 16px; font-weight: bold'>").text('新增地址信息').appendTo("#para-title");
});

$('#okay').click(function () {
    var schedular = {
        name: $('#sc_name').val(),
        url: $('#sc_url').val(),
        id: id
    };
    $.ajax('schedular/save', {
        data: schedular,
        method: 'POST',
        async: false,
        success: function() {
            if (id == null) {
                alert('添加成功');
            } else {
                alert('修改成功');
            }
            getList(20, 1, null);
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
});

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

    var urlCell = document.createElement('td'); //访问路径
    urlCell.setAttribute("class", "text-center");
    urlCell.innerHTML = highLightKeywords(schedular.url, keyword, null);
    urlCell.setAttribute("id", "link_schedular_" + schedular.id);
    row.appendChild(urlCell);

    var dateCell = document.createElement('td'); //最近一次编辑时间
    dateCell.setAttribute("class", "text-center");
    dateCell.innerHTML = formatDateTime(schedular.date); //填充数据
    row.appendChild(dateCell);

    var uuCell = document.createElement('td'); //最近一次编辑人员
    uuCell.setAttribute("class", "text-center");
    uuCell.innerHTML = schedular.user; //填充数据
    row.appendChild(uuCell);

    var handleCell = document.createElement('td'); //msg
    handleCell.setAttribute("class", "text-center");
    var editButton = document.createElement("button");
    editButton.setAttribute("class", "btn btn-info btn-sm");
    editButton.setAttribute("id", 'edit_' + schedular.id);
    editButton.innerHTML = '编辑';
    handleCell.appendChild(editButton);
    var deleteButton = document.createElement("button");
    deleteButton.setAttribute("class", "btn btn-danger btn-sm");
    deleteButton.setAttribute("id", 'delete_' + schedular.id);
    deleteButton.setAttribute("style", "margin-left: 5px");
    deleteButton.innerHTML = '删除';
    handleCell.appendChild(deleteButton);
    row.appendChild(handleCell);

    return row;
}

$(function() {
    'use strict';

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

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

});