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

/**
 * 获取企业列表
 */
function getDeputyOrderList(count, page, keyword) {
    $('#loadingDiv').show();
    deputyOrderList = [];
    $("#deputyList-body").empty();
    $.get('deputyOrder/list', {
        count: count,
        page: page,
        keyword: keyword
    }, function (data) {
        $('#loadingDiv').hide();
        deputyOrderList = data.data;
        var pageNumber = data.page;
        var pageSize = data.size;
        var total = data.totalElements;
        var totalPage = data.totalPages;
        var tbody = document.getElementById("deputyList-body");
        var page = document.getElementById("page");
        //noinspection JSAnnotator
        $('#deputyList-total').val('共 ' + total + ' 条');
        if (deputyOrderList.length > 0) {
            for (var i = 0; i < deputyOrderList.length; i++) {
                var trow = getDataRow(deputyOrderList[i], i); //定义一个方法,返回tr数据
                tbody.appendChild(trow);
                // 查询详情
                (function(i) {
                    $("#edit_one_" + deputyOrderList[i].id ).click(function () {
                        editOne(deputyOrderList[i]);
                    });
                })(i);
            }

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

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

/**
 * 编辑代采订单
 *
 * @param deputyOrder
 */
var id = null;
function editOne(order) {
    $('#para-title').empty();
    $("<p style='font-size: 16px; font-weight: bold'>").text(order.code).appendTo("#para-title");
    $('#deputyOrderInfo').modal('show');
    $("#order_code").append(order.code);
    $("#order_salecode").append(order.salecode);
    $("#order_enName").append(order.enName);
    $("#order_deputyname").append(order.deputyname);
    $("#order_tervendor").append(order.tervendor);
    id = order.id;
}

/**
 * 编辑代采订单
 */
$('#okay').click(function () {
    $.ajax('deputyOrder/reDownOrder/' + id, {
        method: 'POST',
        async: false,
        success: function(data) {
            if (data.code == 0) {
                alert('修改成功');
            } else {
                alert('修改失败');
            }
            getDeputyOrderList(20, 1, null);
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
});

/**
 * 建立表格数据
 *
 * @param en 企业信息
 * @param i 行号
 * @returns {Element}
 */
function getDataRow(order, 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 uuCell = document.createElement('td');
    uuCell.setAttribute("class", "text-left");
    uuCell.innerHTML = "流水号： " + highLightKeywords(order.code, keyword, null); //填充数据
    uuCell.innerHTML += "<br>订单编号： " + highLightKeywords(order.salecode || "-", keyword, null);
    row.appendChild(uuCell);

    // 采购企业
    var nameCell = document.createElement('td');
    nameCell.setAttribute("class", "text-left");
    nameCell.innerHTML = "企业名称： " + order.enName; //填充数据
    nameCell.innerHTML += "<br>企业UU： " + order.enuu;
    row.appendChild(nameCell);

    // 终端供应商
    var vendorCell = document.createElement('td');
    vendorCell.setAttribute("class", "text-left");
    vendorCell.innerHTML = "代采企业:  " + order.deputyname;
    vendorCell.innerHTML += "<br>终端供应商： " + order.tervendor;
    row.appendChild(vendorCell);

    // 单据状态
    var orderStatusCell = document.createElement('td'); //营业执照
    orderStatusCell.setAttribute("class", "text-center");
    orderStatusCell.innerHTML = "录入状态： " + order.entrystatus;
    orderStatusCell.innerHTML += "<br>下载状态： " + order.downloadstatus;
    row.appendChild(orderStatusCell);

    // 日期
    var dateCell = document.createElement('td'); //日期
    dateCell.setAttribute("class", "text-center");
    dateCell.innerHTML = "录入时间： " + formatDateTime(order.entrydate) || "-"; //填充数据
    dateCell.innerHTML += "<br>提交时间： " + formatDateTime(order.date) || "-"; //填充数据
    row.appendChild(dateCell);

    // 操作
    var handleCell = document.createElement('td');
    handleCell.setAttribute('class', 'text-center redirect_b2b');
    handleCell.setAttribute('id', "edit_one_" + order.id);
    handleCell.setAttribute("title", "请注意：点击更新后会让单据重新下载");
    handleCell.innerHTML = '更新';
    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;
    getDeputyOrderList(count, 1, null);
});