var pageNumber = 1, pagesize = 100;
var typeId = getParam("typeId");
mui.ready(function () {
    var btnArray = ['否', '是'];
    $.ajax(base_path + '/coupon/couponTypeList', {
        data: {
            "pageSize": pagesize,
            "pageNO": pageNumber,
            "typeId": typeId
        }, 
        dataType: 'json', 
        type: 'post', 
        timeout: 15000, 
        xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
        success: function (dt) {
        	if (dt.isRedirect) {
				location.href = dt.redirectURL;
			} else {
            console.log(dt);
            if (dt.status) {
                var coupontype_data = [];
                coupontype_data = dt.data.typeList;
                var table = document.body.querySelector('.my-yhq');
                if (coupontype_data.length == 0) {
                    $(".no_yhq").show()
                }
                mui(coupontype_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.couponId != 1 && this.couponId != 2) {
                        li.innerHTML = '<img width="100%" src="//s.iamberry.com/watero/images/shop/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.couponEndDate + '</p><span class="condition">满' + accDiv(this.couponConsumeEnough, 100) + '元可使用</span></div><div class="right-cell my-btn" id=' + this.couponId + ' money=' + ptrice + '><div class="money"><span>' + ptrice + '</span></div><span class="my-btn-lq">立即领取</span></div></div></div>';
                        table.appendChild(li)
                    } else {
                        $(".no_yhq").show()
                    }
                })
            } else {
                var r = confirm(dt.message + '是否重新加载页面?');
                if (r == true) {
                    location.href = domanUrl + base_path + "/couponWechat/coupon?dates=" + new Date().getTime()
                }
            }
            }
        }, error: function (xhr, type, errorThrown) {
            if (xhr.readyState == 4 && xhr.status == 200) {
            } else {
                location.href = domanUrl + base_path + "/couponWechat/coupon?dates=" + new Date().getTime()
            }
        }
    });
    getCartNum();
    document.querySelector('.loading-bg').style.display = 'none'
});
mui('body').on('tap', '.x-close', function () {
    mui.closePopup();
    location.reload(true)
});
mui('body').on('tap', '.my-btn', function () {
    var couponid = this.getAttribute('id');
    var money = this.getAttribute('money');
    $.ajax(base_path + '/coupon/getCoupon', {
        data: {"couponId": couponid},
        dataType: 'json',
        type: 'post',
        timeout: 15000,
        xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
        success: function (dt) {
        	if (dt.isRedirect) {
				location.href = dt.redirectURL;
			} else {
            console.log(dt);
            if (dt.status) {
                var btnArray = ['查看优惠券', '立 即 使 用'];
                mui.confirm('<span>' + money + ' </span>优惠券<img class="x-close" src="/watero/common/shop/images/x.png" />', '恭喜您成功领取一张', btnArray, function (e) {
                    if (e.index == 1) {
                        location.href = 'chanpin_list.html'
                    } else {
                        location.href = base_path + '/couponWechat/couponList'
                    }
                })
            } else {
                var r = confirm(dt.message);
                if (r == true) {
                    location.reload(true)
                }
            }
            }
        },
        error: function (xhr, type, errorThrown) {
            if (xhr.readyState == 4 && xhr.status == 200) {
            } else {
                location.href = domanUrl + base_path + "/couponWechat/coupon?dates=" + new Date().getTime()
            }
        }
    });
});