var myScroll, pullUpEl, pullUpOffset;
var num = 1;

function  getData(num){
    var total =num*5;
    var html='';
    var searchText=$("#searchText").val();
    var li,el;
    el = document.getElementById('admin-test');
    $.ajax({
        type: 'POST',
        url: "/weixin/partner/query",
        data:{offset:total,limit:5,searchText:searchText},
        dataType:"json",
        async: false,
        success: function(data){
            if(data.rows.length>0){
                for (var i=0;i<data.rows.length;i++)
                {
                    html="";
                    li = document.createElement('li');
                    html= html+'<div class="admin-head">'
                        + '<p>'+data.rows[i].companyName+'</p>';
                    if(data.rows[i].status=='1'){
                        html=html + '<p class="fei sblock">启用</p>'
                    }else{
                        html=html + '<p class="fei">禁用</p>'
                    }
                    html=html +'</div>'
                        + '<div class="admin-cont">'
                        + '<p>'+data.rows[i].contactsList_name+'</p>'
                        + '<p>'+data.rows[i].contactsList_contactsWay+'</p>'
                        + '</div>'
                        + '<div class="admin-foot">';
                           if(data.rows[i].cnt){
                               html=html+ '<span>已分配产品 X'+data.rows[i].cnt+'</span>';
                           }else{
                               html=html+ '<span>已分配产品 X 0</span>';
                           }
                    html=html+ '<a onclick=getCompany(\"'+data.rows[i].id+'\")>点击查看详情'
                        + '<img src="/static/weixin/images/left.png" /></a>'
                        + '</div>';
                    li.innerHTML=html;
                    el.appendChild(li,el.childNodes[0]);
                }
            }else{

                //$("#pullUp").html('<span class="pullUpLabel">数据已经加载完了</span>');
                // layer.open({content:"数据已经加载完了！",btn: '我知道了'});
                $("#pullUp").hide();
                $("#ert").text('数据已经加载完了！').show();
            }
        }
    });


}
function  getCompany(obj){

    window.location.href=baseUrl+"/weixin/partner/detatil/"+obj;

}

function pullUpAction () {
   getData(num);
    num++;
    myScroll.refresh();
    // 数据加载完成后，调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
}




/**
 * 初始化iScroll控件
 */
function loaded() {

    pullUpEl = document.getElementById('pullUp');
    pullUpOffset = pullUpEl.offsetHeight;

    myScroll = new iScroll('wrapper', {
        scrollbarClass: 'myScrollbar', /* 重要样式 */
        scrollbars: true,
        mouseWheel: true,//允许滑轮滚动
        useTransition: false, /* 此属性不知用意，本人从true改为false */
        onRefresh: function () {
            if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
            }
        },
        onScrollMove: function () {
            if (this.y < (this.maxScrollY - 55) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
                this.maxScrollY = this.maxScrollY;
            } else if (this.y > (this.maxScrollY + 55) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                this.maxScrollY = pullUpOffset;
            }
        },
        onScrollEnd: function () {
            if (pullUpEl.className.match('flip')) {
                pullUpEl.className = 'loading';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '正在加载...';
                pullUpAction();	// Execute custom function (ajax call?)
            }
        }
    });

    setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}

//初始化绑定iScroll控件
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);
