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

function  getData(num){
    var companyId= $("#companyId").val();
    var productIds=$("#productIds").val();
    var total =num*99;
    var html='';
    var searchText=$("#searchText").val();
    var order = $("#order").html();
    var sortcol = $("#sortcol").html();
    var li,el;
    el = document.getElementById('product-list');
    $.ajax({
        type: 'POST',
        url: baseUrl+"/weixin/share/activityShareProductQuery",
        data:{offset:total,limit:99,companyId:companyId,likeValue:searchText,order:order,sort:sortcol},
        dataType:"json",
        async: false,
        success: function(data){
            console.log(data);
            if(data.rows.length>0){
                // el.html("");
                for (var i=0;i<data.rows.length;i++)
                {
                    html="";
                    li = document.createElement('li');
                    var fee = data.rows[i].fee==undefined?"-":data.rows[i].fee+"%";
                        html ='<div class="select">';
                    if(productIds==''){
                        html= html + '<div class="select-wed" name="'+data.rows[i].id+'" onclick="changClass(this)"></div>';
                    }else{
                         if(productIds.indexOf(data.rows[i].id)>-1){
                             html= html + '<div class="select-wed on" name="'+data.rows[i].id+'" onclick="changClass(this)"></div>';
                         }else{
                             html= html + '<div class="select-wed" name="'+data.rows[i].id+'" onclick="changClass(this)"></div>';
                         }
                    }
                    html=html  + '</div>'
                        + '<div class="select-content">'
                        + '<div class="select-left">'
                        + '<p>'+data.rows[i].label+'</p>'
                        + '<span>'+data.rows[i].name+'</span>'
                        // + '<i>创建日期: '+getMyDate(data.rows[i].createTime)+'</i>'
                        + '</div>'
                        + '<div class="select-right">'
                        + '<p>产品价格</p>'
                        + '<p style="margin-top: 10px;margin-bottom: 10px;color: #77a9fd">'+data.rows[i].fixed_premium+'<i style="color: #999;font-style: normal;margin-left: 5px">元</i>'+'</p>'
                        + '<p>预计收益</p>'
                        // + '<span>'+fee+'</span>'
                        + '<p style="margin-top: 10px;color: #77a9fd">'+fee+'</p>'
                        // + '<p style="margin-top: 10px;color: #77a9fd">'+data.rows[i].true_fee+'<i style="color: #999;font-style: normal;margin-left: 5px">元</i>'+'</p>'
                        + '</div>'
                        + '</div>'
                    li.innerHTML=html;
                    el.appendChild(li,el.childNodes[0]);
                }
                $('.select .select-wed').each(function () {
                    if ($(this).hasClass('on')) {
                        $('.pro-steps button.xia').removeAttr("disabled").css({"background": "#93c1ff","box-shadow":"0px 2px 12px #BDD7FA","-moz-box-shadow":"0px 2px 12px #BDD7FA;","-webkit-box-shadow":"0px 2px 12px #BDD7FA;"});
                        return false;
                    } else {
                        $('.pro-steps button.xia').attr('disabled', "true").css({"background": "rgb(205,213,225);","box-shadow":"none"});
                    }
                })
            }else{
                $("#pullUp").hide();
                $("#ert").text('数据已经加载完了！').show();
            }
        }
    });


}

function pullUpAction () {
    getData(num);
    num++;
    myScroll.refresh();
}


//获得年月日      得到日期oTime
function getMyDate(str){
    var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth()+1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间
    return oTime;
};
//补0操作
function getzf(testnum){
    if(parseInt(testnum) < 10){
        testnum = '0'+testnum;
    }
    return testnum;
}
/**
 * 初始化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);


$(document).ready(function(){
    // pullUpAction (); // 初始化加载
    getData(0);
});