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

/**
 * 获取邀请记录信息
 */
function getinviteList(count, page, keyword) {
    $('#loadingDiv').show();
    $("#inviteList-body").empty();
    inviteList = [];
    $.get('invite/list', {
        count: count,
        page: page,
        keyword: keyword,
        fromDate: $('#fromDate').val(),
        endDate: $('#endDate').val()
    }, function (data) {
        $('#loadingDiv').hide();
        inviteList = data.content;
        var pageNumber = data.number;
        var pageSize = data.size;
        var total = data.totalElements;
        var totalPage = data.totalPages;
        var tbody = document.getElementById("inviteList-body");
        var page = document.getElementById("page");
        //noinspection JSAnnotator
        $('#inviteList-total').val('共 ' + total + ' 条');
        if (inviteList.length > 0) {
            for (var i = 0; i < inviteList.length; i++) {
                var trow = getDataRow(inviteList[i], i); //定义一个方法,返回tr数据
                tbody.appendChild(trow);
                // 编辑
                (function(i) {
                    $("#edit_" + inviteList[i].uu).click(function () {
                        editInfo(inviteList[i]);
                    });
                })(i);
                // 删除
                (function(i) {
                    $("#delete_" + inviteList[i].uu).click(function () {
                        deleteInfo(inviteList[i].uu);
                    });
                })(i);
            }

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

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

/**
 * 编辑
 *
 * @param invite 邀请信息
 */
var uu = null;
function editInfo(invite) {
    $('#para-title').empty();
    $("<p style='font-size: 16px; font-weight: bold'>").text(invite.enName).appendTo("#para-title");
    $('#inviteInfo').modal('show');
    $("#in_name").val(invite.enName);
    $("#in_inviteName").val(invite.inviteEnName);
    $("#in_inviteUU").val(invite.inviteUU);
    $("#in_inviteUserName").val(invite.inviteUserName);
    $("#in_inviteUserTel").val(invite.inviteUserTel);
    $("#in_inviteUserUU").val(invite.inviteUserUU);
    uu = invite.uu;
}

/**
 * 清除邀请记录
 *
 * @param uu uu号
 */
function deleteInfo(enUU) {
    $.ajax('invite/delete/' + enUU, {
        method: 'POST',
        async: false,
        success: function(data) {
            if (data.success) {
                alert(data.content);
                getinviteList(20, 1, $('#keyword').val());
            }
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
}
/**
 * 确定修改
 */
$('#okay').click(function () {
    var data = {
        enUU: uu,
        userName: $('#in_inviteUserName').val(),
        userTel: $('#in_inviteUserTel').val(),
        userUU: $('#in_inviteUserUU').val(),
        enterName: $('#in_inviteName').val(),
        enterUU: $('#in_inviteUU').val()
    };
    $.ajax('invite/edit', {
        data: data,
        method: 'POST',
        async: false,
        success: function(data) {
            if (data.content) {
                alert('更新成功');
                getinviteList(20, 1, $('#keyword').val());
            }
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
})

/**
 * 导出数据
 */
$('#export').click(function () {
   window.open("/invite/xls?keyword=" + $('#keyword').val() + "&fromDate=" + $('#fromDate').val() + "&endDate=" + $('#endDate').val());
});

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

    // 邀请用户名称
    var inviteUserNameCell = document.createElement('td');
    inviteUserNameCell.setAttribute("class", "text-center");
    inviteUserNameCell.innerHTML = highLightKeywords(invite.inviteUserName, keyword, null) || '-';
    row.appendChild(inviteUserNameCell);

    // 邀请人联系方式
    var inviteUserTelCell = document.createElement('td');
    inviteUserTelCell.setAttribute("class", "text-center");
    inviteUserTelCell.innerHTML = invite.inviteUserTel || '-';
    row.appendChild(inviteUserTelCell);

    // 注册企业名称
    var enNameCell = document.createElement('td');
    enNameCell.setAttribute("class", "text-center");
    enNameCell.innerHTML = highLightKeywords(invite.enName, keyword, null);
    row.appendChild(enNameCell);

    // 注册时间
    var enDateCell = document.createElement('td');
    enDateCell.setAttribute("class", "text-center");
    enDateCell.innerHTML = formatDateTime(invite.enDate);
    row.appendChild(enDateCell);

    // 联系人
    var adminNameCell = document.createElement('td'); //detail
    adminNameCell.setAttribute("class", "text-center");
    adminNameCell.innerHTML = highLightKeywords(invite.adminName, keyword, null) || '-';
    row.appendChild(adminNameCell);

    // 物料数
    var countCell = document.createElement('td');
    countCell.setAttribute("class", "text-center");
    countCell.innerHTML = invite.productCount;
    row.appendChild(countCell);

    // 物料最近上传时间
    var prodDateCell = document.createElement('td');
    prodDateCell.setAttribute("class", "text-center");
    prodDateCell.innerHTML = invite.lastProductDate == null ? '-' : formatDateTime(invite.lastProductDate);
    row.appendChild(prodDateCell);

    // 编辑按钮
    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_' + invite.uu);
    editButton.innerHTML = '编辑';
    handleCell.appendChild(editButton);
    var deleteButton = document.createElement("button");
    deleteButton.setAttribute("class", "btn btn-danger btn-sm");
    deleteButton.setAttribute("id", 'delete_' + invite.uu);
    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;
    getinviteList(count, 1, null);
});