<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<meta name="Expires" content="0">
	<meta name="Cache-Control" content="no-store, no-cache, must-revalidate">
	<meta name="Cache-Control" content="post-check=0, pre-check=0">
	<meta name="Pragma" content="no-cache">
	<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" type="text/css" href="css/mui.min.css" />
    <link rel="stylesheet" type="text/css" href="css/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="css/style.css" />
	<link rel="stylesheet" type="text/css" href="css/clearing.css" />
	<style type="text/css">
		.buy-footer1:after{display: none;}
	</style>
	<script type="text/javascript">
        // 屏蔽分享
        window.hiddenAllWechatMenu = true;
	</script>
</head>
<body style="background: #fff">
<div class="loading-bg">
	<div class="mui-loading">
		<div class="mui-spinner">
		</div>
		&nbsp;&nbsp;加载中...
	</div>
</div>
<div class="mui-content" style="padding-bottom: 100px !important;background: #FFF;">
	<div class="address-cantainner">
		<ul class="mui-table-view mui-table-view-striped my-grid-view-no mui-table-view-condensed">
			<li class="mui-table-view-cell">
				<div class="mui-table">
					<!--没有选地址时显示的div-->
					<div class="mui-table-cell mui-col-xs-12 table-middle btn-add-address">
						<div class="add-address"><span class="iconfont icon-jia"></span>添加收货地址</div>
					</div>
					<div class="mui-table-cell mui-col-xs-1 mui-text-right table-middle btn-add-address">
						<p><span class="mui-icon mui-icon-arrowright"></span></p>
					</div>
					<!--选择微信地址后显示的div-->
					<div id="user-address-div" style="display: none;">
						<!--<div class="mui-table-cell mui-col-xs-1 mui-text-left table-middle">
                        <p><span class="iconfont icon-dingwei"></span></p>
                    </div>
                    <div class="mui-table-cell mui-col-xs-10 mui-text-left table-middle">
                        <div>收货人:林笑笑 <span class="mui-pull-right">16899996678</span></div>
                        <p>收货地址:广东省深圳市福田区莲花街道深圳特区 报业大厦12b</p>
                    </div>-->

					</div>
				</div>
			</li>
		</ul>
	</div>
	<!--<ul class="mui-table-view more">-->
		<!--<li class="mui-table-view-cell line-color" style="padding:20px 10px;">-->
			<!--<div style="display: none;" id="user-address-div"></div>-->
			<!--<a class="check_address" id="open-wechat-addres-a">-->
				<!--<span>请选择收货地址!</span><span class="mui-icon mui-icon-forward icon-r-c"></span>-->
			<!--</a>-->
		<!--</li>-->
	<!--</ul>-->
	<ul class="mui-table-view mui-table-view-chevron my-grid-view-no" id="products-list-ul">
		<li class="mui-table-view-cell mui-media" id="load-li">
			&nbsp;&nbsp;加载产品信息中...
		</li>
	</ul>
	<ul class="mui-table-view my-yhq-tab">
		<li class="mui-table-view-cell forward" style="margin: 0">
			<a href="#forward" class="mui-navigate-right">
				<div class="mui-media-body">
					优惠券<span class="yhq-right"><span class="cl-red" id="yhq_count">0</span>张可使用</span>
				</div>
			</a>
		</li>
		<div style="padding: 10px 15px 0 15px;display: none;">可用余额:<span class="cl-red" id="used_balance">¥0.00</span></div>
		<li class="mui-table-view-cell pay_ipt_box" style="padding: 0;display: none;">
			<div class="mui-input-group">
				<div class="mui-input-row">
					<label>输入使用金额:</label>
					<input id="pay_ipt" type="number" placeholder="(如699)">
				</div>
			</div>
		</li>
		<li class="mui-table-view-cell" style="padding: 0;margin: 0;">
			<div class="mui-input-group">
				<div class="mui-input-row">
					<label>订单备注</label>
					<input style="font-size: 12px" type="text" placeholder="(选填:特殊要求,可以在此备注)" id="textarea">
				</div>
			</div>
		</li>
		<li class="mui-table-view-cell" style="padding: 0;margin: 0;display: none;">
			<div class="mui-input-group">
				<div class="mui-input-row">
					<label>商品金额:</label>
					<input style="font-size: 12px;text-align: right;" type="text" readonly="readonly" placeholder="(选填:订单要求,可以在此备注)" id="total">
				</div>
			</div>
		</li>
		<li class="mui-table-view-cell" id="vipSub" style="display: none;">
			<div><span>VIP专享(1年滤芯套装)</span><span class="mui-pull-right"><span id="vip_price">- ¥0</span></span></div>
		</li>
		<li class="mui-table-view-cell" id="accSub">
			<div><span id="yhq_name"></span><span class="mui-pull-right"><span id="yhq_price"></span></span></div>
			<div>账户余额:<span class="mui-pull-right cl-red">- ¥<span id="used_buy">0</span></span></div>
		</li>
		<li class="mui-table-view-cell" id="user2000" style="display: none;">
			<div><span>全场购满2000元</span><span class="mui-pull-right"><span id="user_2000">赠Aiberle电动牙刷1支</span></span></div>
		</li>
		<li class="mui-table-view-cell" id="user3000" style="display: none;">
			<div><span>全场购满3000元</span><span class="mui-pull-right">- ¥<span id="user_3000">100</span></span></div>
		</li>
	</ul>
	<!--<div class="clearing-count" style="text-align: right;">订单合计:<font class="cl-red">¥</font><span class="cl-red" id="user-pay-money-span">加载中...</span></div>-->
	
<div class="buy-footer1">
<table class="tb-clearing">
<tbody><tr>
<td>
共<span><span id="pro-count">0</span>件</span>
</td>
<td class="mui-text-center">合计:<span class="count_price_box">¥<span class="cl-red" id="user-pay-money-span">0.0</span></span></td>
<td>
<a id="btn-weixin-pay" class="mui-btn mui-pull-right add_gwc btn-weixin-pay" style="min-width: 120px;">支付</a>
</td>
</tr>
</tbody></table>
</div>
	<!--<div class="pay_weixin">
		<button type="button" class="mui-btn mui-btn-success mui-btn-block btn-weixin-pay" id="btn-weixin-pay" style="border-radius: 0;background-color: #55c0dc;border-color: #55c0dc;">立即支付</button>
	</div>-->
</div>
<div id="forward" class="mui-popover mui-popover-action mui-popover-bottom">
	<div class="mui-input-group mui-table-view-radio my-popover ">

	</div>
	<ul class="mui-table-view">
		<li class="mui-table-view-cell">
			<a href="#forward"><b>确定</b></a>
		</li>
	</ul>
</div>
<div class="dhq-dalog">
	<div>
		<div class="titles">请选择免费兑换产品<p>(您有1张T1/T2滤芯兑换券)</p></div>
		<div>
			<form class="mui-input-group">
				<div class="mui-input-row mui-radio mui-left">
					<label>兑换T1滤芯</label>
					<input name="radio2" type="radio" value="7"/>
				</div>
				<div class="mui-input-row mui-radio mui-left">
					<label>兑换T2滤芯</label>
					<input name="radio2" type="radio" value="8"/>
				</div>
			</form>
		</div>
		<div class="btn-boxs">
			<button type="button" class="mui-btn nexts">下次兑换</button>
			<button type="button" class="mui-btn enter">确认兑换</button>
		</div>
	</div>
</div>
<script src="js/mui.min.js"></script>
<script src="js/jquery-2.1.1.min.js"></script>
<script src="js/main.js"></script>
<script src="js/page/wechat-utils-1.0.js"></script>
<script src="js/page/layer.js"></script>
<script>
    var userIntegral = 0; // 用户可用积分
    var meonyProportion = 0; // 兑换比例:1个积分=?分钱
    var payTotal = 0.0; // 支付总金额
    var userInputIntegral = 0; // 抵扣积分
    var isPay = true; // 是否在请求支付,false表示正在请求,true可以请求
    var cartIdStr = ""; // 购物车ID
    var pro_all_count=0;
    var usePayMoney = 0;
    var ipt_balance = 0;
    var yhq_val = 0;
    var used_balance = 0;
    var yhq_type = 1;
    var showIs = false;
    var accVipSub=0;
    var exchangeColorId='';
    var t1=0,t2=0,all_t=0;
    var user38Interests=2;

    mui.ready(function() {
        var name,value;
        var str=location.href; //取得整个地址栏
        var num=str.indexOf("?")
        str=str.substr(num+1); //取得所有参数
        var $productListUl = $("#products-list-ul");

        $.ajax(base_path+'/order/payBefore?'+str, {
            dataType: 'json',
            type: 'POST',
            timeout: 15000,
            xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
            success: function(dt) {
                if(dt.status == true) {
                    // 购物项遍历
                    //console.log(dt)
                    var liNote = "";
                    var cNum = 0;
                    user38Interests=dt.data.user38Interests;
                    if(dt.data.balance == 0) {
                        $(".pay_ipt_box").hide();
                    } else {
                        used_balance = accDiv(dt.data.balance, 100);
                        $('#used_balance').html("¥ " + accDiv(dt.data.balance, 100));
                    }
                    var yhq_str = "";
                    if(dt.data.couponItems.length == 0 || !dt.data.disable) {
                        $(".forward").hide();
                    } else {
                        $("#yhq_count").html(dt.data.couponItems.length);
                        //如果购买商品只有饮水机滤芯,则不能使用优惠券
						var c=0,flag=false;
                        var btnArray = ['否', '是'];
                        mui(dt.data.couponItems).each(function(index) {
                            if(this.couponId==10000){
                                c++;
							}
						});
                        mui(dt.data.cartItems).each(function(index) {
                            if(this.cartColorId==7){
                                t1+=this.cartNum;
                            }else if(this.cartColorId==8){
                                t2+=this.cartNum;
                            }else{
                                all_t++;
							}
                        });
                        if(c>0){
                            if((t1==0 && t2==0) || all_t==0){
                                $(".dhq-dalog").show();
							}else{
                                flag=true;
							}

                            $(document).on('tap', '.enter', function() {
                                if($(".dhq-dalog input[type=radio]:checked").length>0){
                                    flag=true;
                                    exchangeColorId=$(".dhq-dalog input[type=radio]:checked").val();
                                    $.ajax(base_path + '/cart/get_exchange?dates=' + new Date().getTime(), {
                                        data: {
                                            "couponId": 10000
                                        },
                                        dataType: 'json',
                                        xhrFields: {
                                            withCredentials: true
                                        },
                                        crossDomain: true,
                                        type: 'post',
                                        async:false,
                                        timeout: 15000,
                                        success: function(dts) {
                                            console.log(dts);
                                            //if(dts.status==true){
                                            	if (dt.isRedirect) {
                                            		location.href = dt.redirectURL;
                                            	} else {
											console.log("t1="+t1+",t2="+t2+",all_t="+all_t);
                                            mui(dts.data).each(function(index) {
                                                    if(exchangeColorId==this.colorId){
                                                        if((((exchangeColorId==7 && t1==0) || (exchangeColorId==8 && t2==0)) && (all_t<=1) && ((t1+all_t)<=1 || (t2+all_t)<=1)) || (t1+t2==0) && all_t>0 || (((t1+all_t)==0 && t2<2) || ((t2+all_t)==0 && t1<2))){
                                                            $productListUl.append('<li class="mui-table-view-cell mui-media dhq-last-add"><a><span class="mui-media-object mui-pull-left wh-100"> <img src="'+this.colorProductPic+'"></span><div class="mui-media-body" style="padding: 3% 0 3% 0;"><div class="wt-space gwc_tit">'+this.colorProductName+'</div><div><small class="mui-h6">'+this.colorName+'x1</small></div></div><div class="price_sp cl-gwc"  price="'+this.colorDiscount+'">¥'+accDiv(this.colorDiscount,100)+'</div></a></li>');
														}else{
                                                            yhq_val = $("#forward input[yhq_type=3]").val();
                                                            console.log(yhq_val)
                                                            if(yhq_val == undefined) {
                                                                yhq_val = 0
                                                            }else{
                                                                $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
															}
														}
													}
                                                });
                                                }
                                            //}
                                        },
                                        error: function(xhr, type, errorThrown) {
                                            console.log(xhr);
                                        }
                                    });
                                    $("#forward input[yhq_type=3]").prop('checked',true);
                                    yhq_val = $("input[name=radio1]:checked").val();
                                    yhq_type = $("input[name=radio1]:checked").attr("yhq_type");
                                    if(yhq_val == undefined) {
                                        yhq_val = 0
                                    } else {
                                        if(yhq_type == 3){
                                            if(yhq_val > (usePayMoney - ipt_balance)) {
                                                $("input[name=radio1]:checked").removeAttr("checked");
                                                yhq_val = 0;
                                                mui.alert(' ', '当前优惠券不可用', function() {});
                                            }
                                            $("#yhq_name").html($("input[name=radio1]:checked").prev().html());
                                            $("#yhq_price").html("免费兑换1支T1或T2滤芯");
                                            $("#accSub,#accSub>div:first-child").show();
                                            console.log(yhq_val)
                                            if($(".dhq-last-add").length>0){
                                                yhq_val = 0;
                                            }
                                            $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                                        }
                                    }
                                    $(".dhq-dalog").hide();
								}else{
                                    console.log("请选择要兑换的滤芯");
								}
							});
                            //下次兑换
                            $(document).on('tap', '.nexts', function() {
                                $("#forward input[yhq_type=3]").removeAttr('checked').prop("disabled","disabled");
                                $(".dhq-dalog").hide();
                                flag=false;
                                if(user38Interests==1){
                                    console.log(usePayMoney>=2000 && usePayMoney<3000)
                                    if(usePayMoney>=2000 && usePayMoney<3000){
                                        $("#user2000").show();
                                        $("#user-pay-money-span").html(usePayMoney);
                                    }else if(usePayMoney>=3000){
                                        $("#user-pay-money-span").html((usePayMoney-100));
                                        $("#user2000,#user3000").show();
                                    }else{
                                        $("#user2000,#user3000").hide();
                                        $("#user-pay-money-span").html(usePayMoney);
                                    }
                                }else{
                                    $("#user-pay-money-span").html(usePayMoney);
                                }
                                console.log("899")
							});


                        }
                        if (dt.data.disable) {
                            mui(dt.data.couponItems).each(function(index) {
                                var checked='';
                                if(this.couponId==10000 && flag){
                                    checked='checked';
                                }
                                yhq_str += '<div class="mui-input-row mui-radio"><label>' + this.couponName + '</label><input name="radio1" value="' + accDiv(this.couponReduce, 100) + '" type="radio" id="' + this.couponItemId + '" yhq_type="' + this.couponType + '"  '+checked+'/></div>';
                            });
                            $(".my-popover").html(yhq_str + '<div class="mui-input-row mui-radio"><label>不使用优惠券</label><input name="radio1" value="1" type="radio"></div>');
                        }
                    }
                    for(var i = 0; i < dt.data.cartItems.length; i++) {
                    	pro_all_count+=dt.data.cartItems[i].cartNum;
                    	// 购物车小计,分--元
                        var total = dt.data.cartItems[i].cartNum * dt.data.cartItems[i].productPrice / 100; 
                        var pro_color = switchcolor(dt.data.cartItems[i].cartColorId);
                        var colorImg = switchColorImg(dt.data.cartItems[i].cartColorId);
                        total = total.toFixed(2); // 保留两位小数
                        var productId = dt.data.cartItems[i].cartProductId;
                        var colorId=dt.data.cartItems[i].cartColorId;
                        var price = dt.data.cartItems[i].productPrice;
                        var num = dt.data.cartItems[i].cartNum;
                        if (dt.data.cartItems[i].productPrice / 100 == 3280) {
                            liNote = liNote +
                                "<li class='mui-table-view-cell mui-media'>" +
                                "<a>" +
                                "<span class='mui-media-object mui-pull-left wh-100'> <img src='" + dt.data.cartItems[i].colorImg + "'></span>" +
                                "<div class='mui-media-body'  style='padding: 3% 0 3% 0;'>" +
                                "<div class='wt-space gwc_tit'>" + dt.data.cartItems[i].productName + "</div>" +
                                "<div><small class='mui-h6'>" + dt.data.cartItems[i].productColor + " x "+dt.data.cartItems[i].cartNum+"</small></div>"+
                                "</div>" +
                                "<div class='price_sp cl-gwc' id='product-" + productId + "' price='" + price + "'>¥" + total + "</div>"+
                                "</a>" +
                                "</li>";
                            cNum = cNum + num ;
                            showIs = true;
                        } else {
                            liNote = liNote +
                                "<li class='mui-table-view-cell mui-media'>" +
                                "<a>" +
                                "<span class='mui-media-object mui-pull-left wh-100'> <img src='" + dt.data.cartItems[i].colorImg + "'></span>" +
                                "<div class='mui-media-body'  style='padding: 3% 0 3% 0;'>" +
                                "<div class='wt-space gwc_tit'>" + dt.data.cartItems[i].productName + "</div>" +
                                "<div><small class='mui-h6'>" + dt.data.cartItems[i].productColor + " x "+dt.data.cartItems[i].cartNum+"</small></div>"+
                                "</div>" +
                                "<div class='price_sp cl-gwc' id='product-" + productId + "' price='" + price + "'>¥" + total + "</div>"+
                                "</a>" +
                                "</li>";
                        }
                        if(dt.data.cartItems[i].cartColorId==5 && dt.data.cartItems[i].cartProductId==38){
                            accVipSub+=(100* dt.data.cartItems[i].cartNum);
						}
                    }

                    if (showIs){
                        liNote = liNote +
                            "<li class='mui-table-view-cell mui-media'>" +
                            "<a>" +
                            "<span class='mui-media-object mui-pull-left wh-100'> <img src='//s.iamberry.com/watero/wechat/images/2C83538AC3039E693358_20161202182946_8118.jpg'></span>" +
                            "<div class='mui-media-body'>" +
                            "<div class='wt-space gwc_tit'>" +
                            '赠送' + "<span id='giveNum'>" + cNum + '</span>' + '套三年滤芯' +
                            "<span class='price_sp cl-gwc mui-pull-right' id='product-39' price='39'>¥" + 0 + "</span></div>" +
                            "<small class='gwc_pp'>颜色:" + '无' + "</small>" +
                            "</div>" +
                            "</a>" +
                            "</li>"
                    }

                    $productListUl.empty();
                    $productListUl.append(liNote);
                    dt.data.userIntegral = dt.data.userIntegral - 1;
                    // 保存数据
                    meonyProportion = dt.data.meonyProportion; // 兑换比例
                    payTotal = dt.data.payTotal; // 支付金额,单位分
                    userIntegral = dt.data.userIntegral; // 用户可用积分
                    cartIdStr = dt.data.cartId; // 购物车ID


                    // 替换数据
                    usePayMoney = accDiv(dt.data.payTotal, 100);
                    $("#total").val(usePayMoney.toFixed(2) + " 元");
                    $("#user-integral-span").html(dt.data.userIntegral);
                    if(dt.data.userIsVip==2){
                        $("#vip_price").text("- ¥"+(accVipSub).toFixed(2));
                        $("#vipSub").show();
                        usePayMoney-=accVipSub;
					}
                    if(user38Interests==1){
                        if(usePayMoney>=2000 && usePayMoney<3000){
                            $("#user2000").show();
                            $("#user3000").hide();
                            $("#user-pay-money-span").html(usePayMoney);
                        }else if(usePayMoney>=3000){
                            $("#user-pay-money-span").html((usePayMoney-100));
                            $("#user2000,#user3000").show();
                        }else{
                            $("#user-pay-money-span").html(usePayMoney);
                            $("#user2000,#user3000").hide();
                        }
                    }else{
                        $("#user-pay-money-span").html(usePayMoney);
                    }
                    //默认自己拉出地址
                    if(dt.data.addressInfo != undefined && dt.data.addressInfo != null) {
                        $(".btn-add-address").hide();
                        $("#user-address-div").html('<div class="mui-table"><div class="mui-table-cell mui-col-xs-1 mui-text-left table-middle"><p><span class="iconfont icon-dingwei"></span></p></div><div class="mui-table-cell mui-col-xs-10 mui-text-left table-middle"><div>收货人:' + dt.data.addressInfo.salesAddressName + ' <span class="mui-pull-right">' + dt.data.addressInfo.salesAddressTel + '</span></div><p>收货地址:' + dt.data.addressInfo.salesAddressInfo + '</p></div><div class="mui-table-cell mui-col-xs-1 mui-text-right table-middle"><p><span class="mui-icon mui-icon-arrowright"></span></p></div></div>').show();
                        //给微信参数赋值
                        wechatAddr.status = true;
                        wechatAddr.detailInfo = dt.data.addressInfo.salesAddressInfo;
                        wechatAddr.userName = dt.data.addressInfo.salesAddressName;
                        wechatAddr.userTel = dt.data.addressInfo.salesAddressTel;
                        wechatAddr.postalCode = dt.data.addressInfo.salesAddressPostnum;
                    }

                    yhq_val = $("input[name=radio1]:checked").val();
                    yhq_type = $("input[name=radio1]:checked").attr("yhq_type");
                    if(yhq_val == undefined) {
                        yhq_val = 0
                    } else {
                        if(yhq_type == 3){
                            if(yhq_val > (usePayMoney - ipt_balance)) {
                                $("input[name=radio1]:checked").removeAttr("checked");
                                yhq_val = 0;
                                mui.alert(' ', '当前优惠券不可用', function() {});
                            }
                            $("#yhq_name").html($("input[name=radio1]:checked").prev().html());
                            $("#yhq_price").html("免费兑换1支T1或T2滤芯");
                            $("#accSub,#accSub>div:first-child").show();
                            console.log(yhq_val)
                            if($(".dhq-last-add").length>0){
                                yhq_val = 0;
                            }
                            console.log("998")
                            if(user38Interests==1){
                                if((usePayMoney - yhq_val - ipt_balance)>=2000 && (usePayMoney - yhq_val - ipt_balance)<3000){
                                    $("#user2000").show();
                                    $("#user3000").hide();
                                    $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                                }else if((usePayMoney - yhq_val - ipt_balance)>=3000){
                                    $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance-100).toFixed(2));
                                    $("#user2000,#user3000").show();
                                }else{
                                    $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                                    $("#user2000,#user3000").hide();
                                }
                            }else{
                                $("#user2000,#user3000").hide();
                                $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                            }
                        }
                    }
                    $("#pro-count").text(pro_all_count);//总件数
                } else {
                    mui.alert(dt.message);
                }
            },
            error: function(xhr, type, errorThrown) {
                errorfn(xhr);
            }
        });
        // 获取微信共享地址
        $(document).on('tap', '.btn-add-address,#user-address-div', function() {
            editAddrV1(
                function(res) {
                    // 用户成功拉出地址
                    var address = res.provinceName + "-" + res.cityName + "-" + res.countryName + "-" + res.detailInfo;
                    wechatAddr.status = true;
                    wechatAddr.detailInfo = address;
                    wechatAddr.postalCode = res.postalCode;
                    wechatAddr.userName = res.userName;
                    wechatAddr.userTel = res.telNumber;
                    wechatAddr.nationalCode = res.nationalCode;
                    $(".btn-add-address").hide();
                    $("#user-address-div").html('<div class="mui-table"><div class="mui-table-cell mui-col-xs-1 mui-text-left table-middle"><p><span class="iconfont icon-dingwei"></span></p></div><div class="mui-table-cell mui-col-xs-10 mui-text-left table-middle"><div>收货人:' + wechatAddr.userName + ' <span class="mui-pull-right">' + wechatAddr.userTel + '</span></div><p>收货地址:' + wechatAddr.detailInfo + '</p></div><div class="mui-table-cell mui-col-xs-1 mui-text-right table-middle"><p><span class="mui-icon mui-icon-arrowright"></span></p></div></div>').show();
                },
                function() {
                    if($('#user-address-div').html() != null && $('#user-address-div').html() != '') {
                        wechatAddr.status = true;
                    } else {
                        wechatAddr.status = false;
                        // 用户取消拉出地址
                        mui.alert("您没有选择收货地址!");
                    }
                }
            );
        });
        // mui('body').on('tap', '#open-wechat-addres-a,#user-address-div', function() {
        //     editAddrV1(
        //         function(res) {
        //             // 用户成功拉出地址
        //             var address = res.provinceName + "-" + res.cityName + "-" + res.countryName + "-" + res.detailInfo;
        //             wechatAddr.status = true;
        //             wechatAddr.detailInfo = address;
        //             wechatAddr.postalCode = res.postalCode;
        //             wechatAddr.userName = res.userName;
        //             wechatAddr.userTel = res.telNumber;
        //             wechatAddr.nationalCode = res.nationalCode;
        //
        //             $("#user-address-div").empty().append(
        //                 "收货人:" + wechatAddr.userName + "<span class='ml-5'>" + wechatAddr.userTel + "</span>" +
        //                 "<a>" +
        //                 "<p class='shipping_address wt-space'>" + wechatAddr.detailInfo + "<span class='mui-icon mui-icon-arrowright icon-r-c'></span></p>" +
        //                 "</a>");
        //             $("#user-address-div").show();
        //             $("#open-wechat-addres-a").hide();
        //         },
        //         function() {
        //             if($('#user-address-div').html() != null && $('#user-address-div').html() != '') {
        //                 wechatAddr.status = true;
        //             } else {
        //                 wechatAddr.status = false;
        //                 // 用户取消拉出地址
        //                 mui.alert("您没有选择收货地址!");
        //             }
        //         }
        //     );
        //
        // });

        // 积分数量改变时
        mui('body').on('change', '#input-user-integral', function() {
            var inputUserIntegral = $("#input-user-integral").val();
            $.trim(inputUserIntegral);
            var r = /^[1-9]+\d*$/;
            if(!r.test(inputUserIntegral)) {
                $("#input-user-integral").val(userInputIntegral);
                return false;
            }
            inputUserIntegral = parseInt(inputUserIntegral);
            if(inputUserIntegral > userIntegral) {
                $("#input-user-integral").val(userInputIntegral);
                return false;
            }
            if((inputUserIntegral * meonyProportion) > payTotal) {
                $("#input-user-integral").val(userInputIntegral);
                return false;
            }
            // 置换数据
            userInputIntegral = inputUserIntegral;
            var temp = (userInputIntegral * meonyProportion / 100).toFixed(2);
            $("#integral-money-span").html(temp);
            temp = ((payTotal - (userInputIntegral * meonyProportion)) / 100).toFixed(2);
            $("#user-pay-money-span").html(temp);
        });
        // 去支付
        mui('body').on('tap', '.btn-weixin-pay', function() {
            //if($("#forward input[yhq_type=3]").prop("checked") && (((t1+all_t)==0 && t2<=2) || ((t2+all_t)==0 && t1<=2) || ((t1<=1 && t2==0) || (t1==0 && t2<=1)))){
             //   if($(".dhq-last-add").is(":visible")){
            //        console.log(exchangeColorId)
            //    }
           // }
            var yhq_id = $("input[name=radio1]:checked").attr('id');
            if(yhq_id == undefined) {
                yhq_id = "";
            }
            var remark = $("#textarea").val();
            var pattern = new RegExp("[ `~!@#$^&*()=|{}':;'\\[\\]<>/?~!@#¥……&*()——|{}【】‘;:”“'、?]")
            if(remark != "") {
                $.trim(remark);
                if(pattern.test(remark)) {
                    mui.alert("请不要输入中文、字母、数字以外的特殊字符!");
                    return;
                }
                if(!(remark.length >= 0 && remark.length <= 100)) {
                    mui.alert("备注字数长度在0-100之间!");
                    return;
                }
            }

            if(wechatAddr.status) {
                if(isPay) {

                    isPay = false; // 锁定请求按钮
                    layer.open({
                        type: 2,
                        shadeClose: false,
                        content: "正在请求加载订单信息..."
                    }); // 调用加载弹出层

                    /* 组装数据 */
                    var dataArgs = {
                        "orderAddressInfo": wechatAddr.detailInfo,
                        "orderAddressPostNum": wechatAddr.postalCode,
                        "orderAddressMoblie": wechatAddr.userTel,
                        "orderAddressName": wechatAddr.userName,
                        "cartIdStr": cartIdStr,
                        "orderIntegralNum": userInputIntegral,
                        "orderUserRemark": remark,
                        "couponId": yhq_id,
                        "balancePayAmount": accMul(ipt_balance, 100)
                    };
//                  if($("#forward input[yhq_type=3]").prop("checked") && (((t1+all_t)==0 && t2<=2) || ((t2+all_t)==0 && t1<=2) || ((t1<=1 && t2==0) || (t1==0 && t2<=1)))){
                        if($(".dhq-last-add").is(":visible")){
                            dataArgs["exchangeColorId"] = exchangeColorId;
                        }
//                  }
                    $.ajax(base_path+'/order/pay', { // 提交订单,准备支付
                        data: dataArgs,
                        dataType: 'json',
                        type: 'POST',
                        timeout: 15000,
                        xhrFields: {
                        	withCredentials: true
                        },
                        crossDomain: true,
                        success: function(dt) {
                        	if (dt.isRedirect) {
                        		location.href = dt.redirectURL;
                        	} else {
                            layer.closeAll();
                            //mui.alert(dt.resultCode=="500");
                            if(dt.resultCode == "500") {
                                mui.alert(dt.message);
                                isPay = true;
                                return;
                            };
                            if(dt.data.info == "isOK") {
                            	location.href = 'pay-win.html?orderId=' + dt.data.orderId + "&dates=" + new Date().getTime();
                                return;
                            };

                            if(dt.status) {
                                var str = dt.data.info;
                                var obj = JSON.parse("{" + str + "}");
                                WeixinJSBridge.invoke('getBrandWCPayRequest', obj, function(res) {
                                    if(res.err_msg == 'get_brand_wcpay_request:ok') {
                                        var orderIds = {
                                            "orderId": dt.data.orderId
                                        };
                                        layer.open({
                                            type: 2,
                                            shadeClose: false,
                                            content: "正在请求加载订单信息..."
                                        }); // 调用加载弹出层
                                        $.ajax(base_path + '/order/checkOrderPaySuccess?dates=' + new Date().getTime(), {
												data: {
													"orderId": dt.data.orderId
												},
												dataType: 'json',
												xhrFields: {
													withCredentials: true
												},
												crossDomain: true,
												type: 'get',
												timeout: 15000,
												success: function(dataobj) {
													if(dataobj.isRedirect) {
														location.href = dataobj.redirectURL;
													} else {
														if(dataobj.status == true) { // 支付成功
															location.href = "pay_win.html?orderId=" + orderIds.orderId + "&dates=" + new Date().getTime();
														} else {
															setTimeout("checkOrderStatus('" + dt.data.orderId + "')", 1000); // 前往订单详情
														}
													}
												},
												error: function(xhr, type, errorThrown) {
													console.log(xhr);
												}
											});
                                    } else {
                                        isPay = true;
                                        // mui.alert("对不起,支付失败!请稍后重试!" + dt.data.orderId + "支付失败!");
                                        location.href = "order_details.html?orderId=" + dt.data.orderId + "&dates=" + new Date().getTime(); // 前往订单详情
                                    }
                                });
                            } else {
                                isPay = true;
                                mui.alert(dt.message);
                                location.href = "order_details.html?orderId=" + dt.data.orderId + "&dates=" + new Date().getTime(); // 前往订单详情
                            }
                            }
                        },
                        error: function(xhr, type, errorThrown) {
                            isPay = true;
                            layer.closeAll();
                            mui.alert("您的网络异常,请刷新重试!");
                            return;
                        }
                    });
                } else {
                    mui.alert("正在请求支付中~~请稍后!");
                    return;
                }
            } else {
                mui.alert("请选择收货地址!");
                return;
            }
        });
        //选择优惠券
        mui('body').on('tap', '.mui-popover-action li>a', function() {
            var a = this,
                parent;
            //根据点击按钮,反推当前是哪个actionsheet
            for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
                if(parent.classList.contains('mui-popover-action')) {
                    break;
                }
            }
            //关闭actionsheet
            mui('#' + parent.id).popover('toggle');
            yhq_val = $("input[name=radio1]:checked").val();
            yhq_type = $("input[name=radio1]:checked").attr("yhq_type");
            if(yhq_val == undefined) {
                yhq_val = 0
            } else {
                if(yhq_type == 1) {
                    if(yhq_val > (usePayMoney - ipt_balance)) {
                        $("input[name=radio1]:checked").removeAttr("checked");
                        yhq_val = 0;
                        mui.alert(' ', '当前优惠券不可用', function() {});
                    }
                    $("#yhq_name").html($("input[name=radio1]:checked").prev().html());
                    $("#yhq_price").html("- ¥"+parseInt(yhq_val).toFixed(2));
                    $("#accSub,#accSub>div:first-child").show();
                    //$("#user-pay-money-span").html((usePayMoney -yhq_val- ipt_balance).toFixed(2));
                    $(".dhq-last-add").hide();
                    exchangeColorId='';
                }else if(yhq_type == 3){
                    if(yhq_val > (usePayMoney - ipt_balance)) {
                        $("input[name=radio1]:checked").removeAttr("checked");
                        yhq_val = 0;
                        mui.alert(' ', '当前优惠券不可用', function() {});
                    }
                    $("#yhq_name").html($("input[name=radio1]:checked").prev().html());
                    $("#yhq_price").html("免费兑换1支T1或T2滤芯");
                    $("#accSub,#accSub>div:first-child").show();
                    $(".dhq-last-add").show();
                    console.log(yhq_val)
                    if($(".dhq-last-add").length>0){
                        yhq_val = 0;
					}
                    //$("#user-pay-money-span").html((usePayMoney -yhq_val- ipt_balance).toFixed(2));
				} else {
                    yhq_val = accSub(usePayMoney, Math.floor(accMul($("input[name=radio1]:checked").val(), usePayMoney) * 100) / 100);
                    $("#yhq_name").html($("input[name=radio1]:checked").prev().html());
                    $("#yhq_price").html("- ¥"+yhq_val);
                    $("#accSub,#accSub>div:first-child").show();
                    //$("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                    $(".dhq-last-add").hide();
                    exchangeColorId='';
                }
                if(user38Interests==1){
                    if((usePayMoney - yhq_val - ipt_balance)>=2000 && (usePayMoney - yhq_val - ipt_balance)<3000){
                        $("#user2000").show();
                        $("#user3000").hide();
                        $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                    }else if((usePayMoney - yhq_val - ipt_balance)>=3000){
                        $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance-100).toFixed(2));
                        $("#user2000,#user3000").show();
                    }else{
                        $("#user2000,#user3000").hide();
                        $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                    }
                }else{
                    $("#user2000,#user3000").hide();
                    $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                }
            }
        });
        mui.os.ios ? listener_ipt() : keyup_fn();
        var reg = /^(([1-9]\d*))$/;

        function keyup_fn() {
            $('body').on('blur', '#pay_ipt', function(e) {
                ipt_balance = $(this).val();
                if(reg.test(ipt_balance)) {
                    if(ipt_balance > (usePayMoney - yhq_val)) {
                        ipt_balance = (usePayMoney - yhq_val).toFixed(2);
                        if(ipt_balance < 0) {
                            ipt_balance = 0;
                            $(this).val("");
                        } else {
                            $(this).val((usePayMoney - yhq_val).toFixed(2));
                        }
                    }
                    if(ipt_balance > used_balance) {
                        ipt_balance = used_balance;
                        $(this).val(used_balance);
                    }
                    $('#used_buy').html(ipt_balance);
                    var money = (usePayMoney - yhq_val).toFixed(2);
                    money <= 0 ? money = 0 : money;
                    $("#accSub,#accSub>div:last-child").show();
                    $("#user-pay-money-span").html((money - ipt_balance).toFixed(2));
                } else {
                    mui.alert(' ', '您输入的数字有误,请重新输入', function() {
                        $(this).val("");
                        ipt_balance = 0;
                        $('#used_buy').html(ipt_balance);
                        $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                    });
                }
            });
        }

        function listener_ipt() {
            document.getElementById('pay_ipt').onblur = function() {
                var th = this;
                ipt_balance = th.value;
                if(reg.test(ipt_balance)) {
                    if(ipt_balance > (usePayMoney - yhq_val)) {
                        ipt_balance = (usePayMoney - yhq_val).toFixed(2);
                        if(ipt_balance < 0) {
                            ipt_balance = 0;
                            th.value = "";
                        } else {
                            th.value = (usePayMoney - yhq_val).toFixed(2);
                        }
                    }
                    if(ipt_balance > used_balance) {
                        th.value = used_balance;
                        ipt_balance = used_balance;
                    }
                    $('#used_buy').html(ipt_balance);
                    var money = (usePayMoney - yhq_val).toFixed(2);
                    money <= 0 ? money = 0 : money;
                    $("#accSub,#accSub>div:last-child").show();
                    $("#user-pay-money-span").html((money - ipt_balance).toFixed(2));
                } else {
                    mui.alert(' ', '您输入的数字有误<br>请重新输入', function() {
                        th.value = "";
                        ipt_balance = 0;
                        $('#used_buy').html(ipt_balance);
                        $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                    });
                }
            }
        }
        document.querySelector('.loading-bg').style.display = 'none';
    });
    // 校验订单状态
    function checkOrderStatus(orderId) {
        $.ajax(base_path + '/wechat/order/checkOrderPaySuccess?dates=' + new Date().getTime(), {
			data: {
				"orderId": orderId
			},
			dataType: 'json',
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			type: 'get',
			timeout: 15000,
			success: function(dataobj) {	
				if(dataobj.isRedirect) {
					location.href = dataobj.redirectURL;
				} else {
					if(dataobj.status == true) { // 支付成功
						location.href = "pay-win.html?orderId=" + dataobj.data + "&dates=" + new Date().getTime();
					} else {
						location.href = 'order_details.html?orderId=' + orderId + "&dates=" + new Date().getTime(); // 前往订单详情
					}
				}
			},
			error: function(xhr, type, errorThrown) {
				console.log(xhr);
			}
		});
   };

    // 点击加号的事件
    function shoppAddEvent(productId,colorId) {
        $.ajax(base_path + '/cart/addCart', {
            data: {
                "productId": productId,
                "carNum": 1,
                "colorId":colorId
            },
            dataType: 'json',
            type: 'GET',
            timeout: 15000,
            xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
            success: function(dt) {
            	if (dt.isRedirect) {
					location.href = dt.redirectURL;
				} else {
                if(dt.status == true) {
                    // 销售价
                    var productObj = $("#product-" + productId);
                    var price = parseInt(productObj.attr("price"));
                    // total
                    var total = payTotal + price;
                    payTotal = total;
                    // 修改数量
                    var shoppNumObj = $("#shoppNum-" + productId);
                    var num = parseInt(shoppNumObj.val());
                    shoppNumObj.val(num + 1);
                    /* $("#user-pay-money-span").html((payTotal / 100).toFixed(2)); */
                    yhq_val = $("input[name=radio1]:checked").val();
                    yhq_type = $("input[name=radio1]:checked").attr("yhq_type");
                    usePayMoney = accDiv(payTotal, 100);
                    if(yhq_val == undefined) {
                        yhq_val = 0
                    }
                    if(yhq_type == 2) {
                        yhq_val = accSub(usePayMoney, (Math.floor(accMul(yhq_val, usePayMoney) * 100) / 100));
                        $("#yhq_price").html(accSub(usePayMoney, Math.floor(accMul($("input[name=radio1]:checked").val(), usePayMoney) * 100) / 100));
                    }
                    $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                    // 修改小计
                    productObj.html("¥" + ((num + 1) * price / 100).toFixed(2));
                    var temp = new Number($("#shoppNum-all").val());
                    temp = temp + 3;
                    $("#shoppNum-all").val(temp);
                    if(showIs){
                        var gNum = $("#giveNum").html();
                        var rNum = parseInt(gNum) + 1;
                        $("#giveNum").html(rNum);
                    }

                } else {
                    // 失败之后的事件
                    mui.alert(dt.message);
                }
                }
            },
            error: function(xhr, type, errorThrown) {}
        });
    }

    // 购物车减
    function shoppSubEvent(productId,colorId) {
        var shoppNumObj = $("#shoppNum-" + productId);
        var num = parseInt(shoppNumObj.val());

        if(num <= 1) {
            mui.alert("不能继续减少了哦!");
            return;
        }
        $.ajax(base_path + '/cart/deleteCart', {
            data: {
                "productId": productId,
                "cartNum": 1,
                "colorId":colorId
            },
            dataType: 'json',
            type: 'GET',
            timeout: 15000,
            xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
            success: function(dt) {
            	if (dt.isRedirect) {
					location.href = dt.redirectURL;
				} else {
                if(dt.status == true) {
                    // 销售价
                    var productObj = $("#product-" + productId);
                    var price = parseInt(productObj.attr("price"));
                    // total
                    var total = payTotal - price;
                    payTotal = total;
                    // 修改数量
                    var shoppNumObj = $("#shoppNum-" + productId);
                    var num = parseInt(shoppNumObj.val());
                    shoppNumObj.val(num - 1);
                    /*$("#user-pay-money-span").html((payTotal / 100).toFixed(2));*/
                    yhq_val = $("input[name=radio1]:checked").val();
                    yhq_type = $("input[name=radio1]:checked").attr("yhq_type");
                    usePayMoney = accDiv(payTotal, 100);
                    if(yhq_val == undefined) {
                        yhq_val = 0
                    }
                    if(yhq_type == 2) {
                        yhq_val = accSub(usePayMoney, Math.floor(accMul(yhq_val, usePayMoney) * 100) / 100);
                        $("#yhq_price").html(accSub(usePayMoney, Math.floor(accMul($("input[name=radio1]:checked").val(), usePayMoney) * 100) / 100));
                        if(ipt_balance > (usePayMoney - yhq_val)) {
                            if((usePayMoney - yhq_val) <= 0) {
                                ipt_balance = 0;
                                $('#used_buy').html(ipt_balance);
                                $('#pay_ipt').val("");
                            } else {
                                ipt_balance = (usePayMoney - yhq_val).toFixed(2);
                                $('#used_buy').html(ipt_balance);
                                $('#pay_ipt').val(ipt_balance);
                            }
                        }
                        $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                    } else {
                        if((usePayMoney - yhq_val) < ipt_balance) {
                            if((usePayMoney - yhq_val) <= 0) {
                                ipt_balance = 0;
                                $('#used_buy').html(ipt_balance);
                                $('#pay_ipt').val("");
                            } else {
                                ipt_balance = (usePayMoney - yhq_val).toFixed(2);
                                $('#used_buy').html(ipt_balance);
                                $('#pay_ipt').val(ipt_balance);
                            }
                        }
                        if(yhq_val > (usePayMoney - ipt_balance)) {
                            mui.alert(' ', '当前优惠券不可用', function() {
                                $("input[name=radio1]:checked").removeAttr("checked");
                                yhq_val = 0;
                                $("#yhq_price").html(yhq_val);
                                $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                            });
                        }
                        $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
                    }
                    var temp = new Number($("#shoppNum-all").val());
                    temp = temp - 3;
                    $("#shoppNum-all").val(temp+"")
                    productObj.html("¥" + ((num - 1) * price / 100).toFixed(2));
                    if(showIs){
                        var gNum = $("#giveNum").html();
                        var rNum = parseInt(gNum) - 1;
                        $("#giveNum").html(rNum);
                    }
                } else {
                    mui.alert(dt.message);
                }
                }
            },
            error: function(xhr, type, errorThrown) {
                errorfn(xhr);
            }
        });
    }
</script>
</body>
</html>