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

/**
 * 获取企业列表
 */
function getEnterpriseList(count, page, keyword) {
    $('#loadingDiv').show();
    enterpriseList = [];
    $("#enterprise-body").empty();
    $.get('enterprise/list', {
        count: count,
        page: page,
        keyword: keyword
    }, function (data) {
        $('#loadingDiv').hide();
        enterpriseList = data.data;
        var pageNumber = data.page;
        var pageSize = data.size;
        var total = data.totalElements;
        var totalPage = data.totalPages;
        var tbody = document.getElementById("enterprise-body");
        var page = document.getElementById("page");
        //noinspection JSAnnotator
        $('#enterprise-total').val('共 ' + total + ' 条');
        if (enterpriseList.length > 0) {
            for (var i = 0; i < enterpriseList.length; i++) {
                var trow = getDataRow(enterpriseList[i], i); //定义一个方法,返回tr数据
                tbody.appendChild(trow);
                (function(i) {
                    $("#link_b2b_" + i ).click(function(url) {
                        window.open('enterprise/authed/redirect/' + enterpriseList[i].uu + (url ? ('?url=' + encodeURI(url)) : ''));
                    });
                })(i)
            }

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

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

/**
 * 建立表格数据
 *
 * @param en 企业信息
 * @param i 行号
 * @returns {Element}
 */
function getDataRow(en, 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'); //UU
    uuCell.setAttribute("class", "text-center");
    uuCell.innerHTML = highLightKeywords(en.uu, keyword, null); //填充数据
    row.appendChild(uuCell);

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

    var addressCell = document.createElement('td'); //企业地址
    addressCell.setAttribute("class", "text-center");
    addressCell.innerHTML = highLightKeywords(en.enAddress || "-", keyword, null); //填充数据
    row.appendChild(addressCell);

    var busiCodeCell = document.createElement('td'); //营业执照
    busiCodeCell.setAttribute("class", "text-center");
    busiCodeCell.innerHTML = highLightKeywords(en.enBusinessCode, keyword, null); //填充数据
    row.appendChild(busiCodeCell);

    var dateCell = document.createElement('td'); //注册日期
    dateCell.setAttribute("class", "text-center");
    if (en.enDate != null) {
        dateCell.innerHTML = formatDateTime(en.enDate); //填充数据
    } else {
        dateCell.innerHTML = '-';
    }
    row.appendChild(dateCell);

    var handleCell = document.createElement('td'); // 操作
    handleCell.setAttribute('class', 'text-center redirect_b2b');
    var id = 'link_b2b_' + i;
    handleCell.setAttribute('id', id);
    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;
    getEnterpriseList(count, 1, null);
});