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

function  getData(num){
    var companyId= $("#companyId").val();
    var total =num*5;
    var html='';
    var li,el;
    el = document.getElementById('assigned-list');
    $.ajax({
        type: 'POST',
        url: baseUrl+"/weixin/product/distributionQuery",
        data:{index:total,size:5,companyId:companyId},
        dataType:"json",
        async: false,
        success: function(data){
            console.log(data);
            if(data.rows.length>0){
                for (var i=0;i<data.rows.length;i++)
                {
                    html="";
                    li = document.createElement('li');
                     html = '<div class="select-content assigned-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 assigned-right">';
                    if(data.rows[i].fee){
                        html=html+ '<p id="fee_'+data.rows[i].pid+'">'+data.rows[i].fee+'%</p>';
                    }else{
                        html=html+ '<p id="fee_'+data.rows[i].pid+'">请设置</p>';
                    }
                    html=html+ '<span>收入比例</span>'
                    + '<i onclick="assighed(this,\''+data.rows[i].pid+'\',\''+data.rows[i].id+'\')">设置</i>'
                    + '</div>'
                    + '</div>'
                    li.innerHTML=html;
                    el.appendChild(li,el.childNodes[0]);
                }
            }else{
                $("#pullUp").hide();
                $("#ert").text('数据已经加载完了！').show();
            }
        }
    });
}
function assighed(obj,oid,pid) {

    var  fee=$("#fee_"+oid).html();
    var  feerate =$("#feerate").html('');
      $.ajax({
          type: 'POST',
          url: "/weixin/product/getBrokerage",
          data:{productId:pid},
          dataType:"json",
          async: false,
          success: function(data){
              console.log(JSON.stringify(data));
              if(data.success){
                  $('#feerateoid').val(data.data);
                  $("#feerate").html('该产品收入比率不得超过'+data.data+'%');
              }else{
                  $("#feerate").html('该产品收入比率，尚未设置请先设置后，再分配');
              }
          }
      });


    $('.proportion-wind input.sdfoid').val(oid+"");
    $('.proportion-wind input.sdf').val('');
    if(fee.indexOf("%")>0){
        $('.proportion-wind input.sdf').val(fee.substring(0,fee.length-1));
    }

    $('.proportion-wind').show();
    $('.block-mask-more').show();


}

//获得年月日      得到日期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;
}
$('.proporion-sel .sure').click(function () {
    var s = parseFloat($('.proportion-wind input.sdf').val());
    var  oid=    $('.proportion-wind input.sdfoid').val();
    var  feerateoid=    $('#feerateoid').val();
    if(s > feerateoid){
        // $('.proportion-wind .proporion-cont p').css("display","block");
    }else{
        // $('.proportion-wind .proporion-cont p').css("display","none");
        $('.proportion-wind').hide();
        $('.block-mask-more').hide();
        $.ajax({
              type: 'POST',
              url: "/weixin/product/setBrokerage",
              data:{brokerage:s,oid:oid},
              dataType:"json",
              async: false,
              success: function(data){
                  if(data.success){
                      $("#fee_"+oid).html(s+"%");
                  }else{
                      layer.open({content:"设置失败！",btn: '我知道了'});
                  }
              }
         });


    }
});

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);

