var myScroll, pullUpEl, pullUpOffset;
var p = { limit: 5, offset: 0 };

function getData(){
    // layer.open({type:2})
    g_ajax("/weixin/primaryPage/query",p ,function(json){
        var el, li, i;
        var rows = json.rows;
        var total = json.total;
        $("#total_row").text("共 "+total+" 笔");
        el = document.getElementById('billed-list-new');
        if(rows.length==0) { //长度为空，或者小于limit条数，说明是已经到最后了
            $("#pullUp").hide();
            $("#ert").text('数据已经加载完了！').show();
            return;
        }else{
            p.offset += p.limit;
        }
        for(var i in rows){
            //var status = rows[i].status=="0"?"未结算":"已结算";
            //2018-05-14 修改为取月结单的状态
            var status = rows[i].monthStatus=="0"?"未结算":"已结算";
            if(weixin_login_distribution_level=="2" || weixin_login_distribution_level=="3" ){
                status = "";
            }
            var projectName =  rows[i].project_name ?rows[i].project_name : rows[i].product_name;
            if(rows[i].activity_name!=undefined){
                projectName = rows[i].activity_name;
            }
            console.log(projectName)
            li = document.createElement('li');
            li.innerHTML =
                '<div class="bill-head"><span>单号：'+ rows[i].order_no + '</span><a href="/weixin/bill/detail/'+rows[i].id+'">点击查看详情 &gt;</a></div>'
                + '<div class="bill-text"><p><span><img src="/static/weixin/images/home-aa.png">'+ projectName +'</span></p>'
                + '<p class="jie"><span>'+status+'</span><i>+'+rows[i].actual_brokerage+'元</i></p>'
                + '<p class="jie"><span>'+ rows[i].order_date + '</span><span>'+ rows[i].company_name  + '</span></p>'
                + '</div>';
            el.appendChild(li, el.childNodes[0]);
        }
        // layer.closeAll();
    });
}

function getSum(){
    //增加6个条件
    if(sessionStorage.getItem("sel_region")!=null){
        var sel_region = JSON.parse(sessionStorage.getItem("sel_region")); //选择地区
        p.area = sel_region.region_id;
    }else{
        delete p.area;
    }

    if(sessionStorage.getItem("sel_startTime")!=null) {
        var startTime = JSON.parse(sessionStorage.getItem("sel_startTime")); //开始时间
        p.startDate = startTime;
    }else{
        delete p.startDate;
    }

    if(sessionStorage.getItem("sel_endTime")!=null) {
        var endTime = JSON.parse(sessionStorage.getItem("sel_endTime")); //结束时间
        p.endDate = endTime;
    }else{
        delete p.endDate;
    }

    if(sessionStorage.getItem("sel_project")!=null) {
        var sel_project = JSON.parse(sessionStorage.getItem("sel_project")); //选择地区
        p.project_name = sel_project;
    }else{
        delete p.project_name;
    }

    if(sessionStorage.getItem("sel_company")!=null) {
        var sel_company = JSON.parse(sessionStorage.getItem("sel_company")); //选择地区
        p.sys_company_id = sel_company.company_id;
    }else{
        delete p.sys_company_id;
    }

    if(sessionStorage.getItem("sel_product_type")!=null) {
        var sel_product_type = JSON.parse(sessionStorage.getItem("sel_product_type")); //选择地区
        p.product_type = sel_product_type;
    }else{
        delete p.product_type;
    }

    p2 = clone(p);
    // p2.offset=0;
    // p2.limit=99999999;
    // layer.open({type:2})
    // var sumIncome=0,lowLevelIncome=0,realIncome=0;

    //查询账单信息
    layer.open({type:2})
    $.ajax({
        type: 'POST',
        url: "/weixin/bill/query-sum-belwo-bill",
        dataType: "json",
        async: true,
        success: function (json) {
            layer.closeAll();
            $("#lowLevelIncome").html(json[0].total_below.toFixed(2));
        }
    });

}(getSum());

function pullUpAction () {
    getData();
    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);


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