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

/**
 * 获取详情
 */
function getDetail(id) {
    $.ajax('noticeRecord/' + id, {
        method: 'GET',
        async: false,
        success: function(data) {
            if (data.content) {
                var notice = data.content;
                $('#enName').text(notice.enName);
                $('#UU').text(notice.enUU);
                $('#remark').text(notice.describe);
                $('#date').text(formatDateTime(notice.date));
                var details = notice.details;
                var tbody = document.getElementById("detail-body");
                if (details.length > 0) {
                    for (var i = 0; i < details.length; i++) {
                        var trow = getDataRow(details[i], i); //定义一个方法,返回tr数据
                        tbody.appendChild(trow);
                    }
                }
            }
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
}

/**
 * 建立表格数据
 *
 * @param notice 内容
 * @param i 行数
 * @returns {Element}
 */
function getDataRow(detail, 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 erpIdCell = document.createElement('td'); //erpID
    erpIdCell.setAttribute("class", "text-center");
    erpIdCell.innerHTML = detail.erpId; //填充数据
    row.appendChild(erpIdCell);

    var b2bIdCell = document.createElement('td'); //b2bId
    b2bIdCell.setAttribute("class", "text-center");
    b2bIdCell.innerHTML = detail.b2bId; //填充数据
    row.appendChild(b2bIdCell);

    var needQtyCell = document.createElement('td'); //needQty
    needQtyCell.setAttribute("class", "text-center");
    needQtyCell.innerHTML = detail.needQty; //填充数据
    row.appendChild(needQtyCell);

    var sendQtyCell = document.createElement('td'); //sendQty
    sendQtyCell.setAttribute("class", "text-center");
    sendQtyCell.innerHTML = detail.sendQty; //填充数据
    row.appendChild(sendQtyCell);

    var endCell = document.createElement('td'); //endStatus
    endCell.setAttribute("class", "text-center");
    endCell.innerHTML = detail.end == 1 ? '已结案' : '未结案';
    row.appendChild(endCell);

    var sendCell = document.createElement('td'); //sendStatus
    sendCell.setAttribute("class", "text-center");
    sendCell.setAttribute("title", "查看参数详情");
    sendCell.innerHTML = detail.status == 201 ? '已发货' : '未发货';
    row.appendChild(sendCell);

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

    var remarkCell = document.createElement('td'); //时间
    remarkCell.setAttribute("class", "text-left");
    remarkCell.innerHTML = detail.remark;//填充数据
    row.appendChild(remarkCell);

    var tipCell = document.createElement('td'); //时间
    tipCell.setAttribute("class", "text-center");
    if (detail.okStatus == 1) {
        tipCell.innerHTML = '正常';
    } else {
        if (detail.b2bId == -1) {
            tipCell.innerHTML = '<span style="color: red;">B2B未找到对应单据</span>';
        } else {
            tipCell.innerHTML = '<span style="color: red;">异常</span>'; //填充数据
        }
    }
    row.appendChild(tipCell);

    return row;
}

/**
 * 获取参数值
 *
 * @param name 参数名
 * @returns {*}
 */
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)  {
        return decodeURI(r[2]);
    }
    return "";
}

$(function() {
    'use strict';

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

    var idParam = "id";
    var id = getQueryString(idParam);
    getDetail(id);
});