var tabindex = 0, pageNumber = 0, pagesize = 10;
mui.init({
    pullRefresh: {
        container: '#pullrefresh_yhq_list',
        down: {},
        up: {contentrefresh: '正在加载...', callback: pullupRefresh}
    }
});
function pullupRefresh() {
    pageNumber += 1;
    var btnArray = ['否', '是'];
    $.ajax(base_path + '/coupon/couponItemList', {
        data: {
            "pageSize": pagesize,
            "useStatus": tabindex,
            "pageNO": pageNumber
        }, 
        dataType: 'json',
        type: 'post', 
        timeout: 15000, 
        xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
        success: function (dt) {
        	if (dt.isRedirect) {
				location.href = dt.redirectURL;
			} else {
            if (dt.status) {
                var coupon_data = [];
                coupon_data = dt.data.itemList;
                mui('#pullrefresh_yhq_list').pullRefresh().endPullupToRefresh(coupon_data.length < pagesize);
                var table = document.body.querySelector('.my-yhq-list');
                mui(coupon_data).each(function (index) {
                    var li = document.createElement('li');
                    li.className = 'my-yhq-cell';
                    var ptrice;
                    if (this.couponType == 1) {
                        ptrice = accDiv(this.couponReduce, 100) + '元'
                    } else {
                        ptrice = "-" + (100 - this.couponReduce) + "%"
                    }
                    if (this.couponUseStatus == 1 && tabindex <= 1 && this.couponItemRemark == "normal") {
                        li.innerHTML = '<img width="100%" src="/watero/common/shop/images/yhq_used_bg.png" /><div class="fixed-box"><div class="table"><div class="left-cell"><span class="tit">' + this.couponName + '</span><p class="time">有效期至' + this.couponUseEndDate + '</p><span class="condition">满' + accDiv(this.couponConsumeEnough, 100) + '元可使用</span></div><div class="right-cell"><div class="money"><span>' + ptrice + '</span></div><span class="my-btn go_web_shop">立即使用</span></div></div></div'
                    } else {
                        li.innerHTML = '<img width="100%" src="/watero/common/shop/images/yhq_overdue_bg.png" /><div class="fixed-box grayscale"><div class="table"><div class="left-cell"><span class="tit grayscale">' + this.couponName + '</span><p class="time grayscale">有效期至' + this.couponUseEndDate + '</p><span class="condition">满' + accDiv(this.couponConsumeEnough, 100) + '元可使用</span></div><div class="right-cell"><div class="money grayscale"><span>' + ptrice + '</span></div><span class="my-btn grayscale">立即领取</span></div></div></div>'
                    }
                    table.appendChild(li)
                })
            } else {
                mui.confirm(dt.message + '是否重新加载页面?', '爱贝源-提示', btnArray, function (e) {
                    if (e.index == 1) {
                    }
                })
            }
            }
        }, error: function (xhr, type, errorThrown) {
            console.log(xhr)
        }
    })
};mui.ready(function () {
    mui('#pullrefresh_yhq_list').pullRefresh().pullupLoading();
    getCartNum();
    document.querySelector('.loading-bg').style.display = 'none'
});
mui.each(document.querySelectorAll('.tab-yhq-list .mui-control-item'), function (index, el) {
    el.addEventListener('tap', function () {
        pageNumber = 0;
        switch (index) {
            case 0:
                tabindex = 0;
                break;
            case 1:
                tabindex = 1;
                break;
            case 2:
                tabindex = 2;
                break;
            default:
                tabindex = 3;
                break
        }
        $('.my-yhq-list').empty();
        mui('#pullrefresh_yhq_list').pullRefresh().refresh(true);
        mui('#pullrefresh_yhq_list').pullRefresh().pullupLoading()
    }, false)
});