var isPay = true;
var isEfficacious = true;
mui
		.ready(function() {
			var orderId = getParam("orderId");
			$('#check-luck-draw').attr("value", orderId);
			var table = document.body.querySelector('.shopping-cart-list');
			mui
					.ajax(
							base_path + '/cart/getShopOrderByOrderId',
							{
								data : {
									orderId : orderId
								},
								dataType : 'json',
								type : 'post',
								timeout : 15000,
								success : function(dt) {
									if (dt.status) {
										var bannerdata = dt.data;
										isEfficacious = bannerdata.efficacious;
										$('#exchangeOrder').hide();
										$('#salesReturn').hide();
										$('#refund').hide();
										$('#cancelOrder').hide();
										$('#orderPay').hide();
										$('#oldStatus').val(
												bannerdata.salesStatus);
										if (bannerdata.salesStatus >= 5) {
											$('#showLogistics').show()
										} else {
											$('#showLogistics').hide()
										}
										if (bannerdata.salesStatus == 1) {
											$('#salesStatus').html('待支付');
											$('#cancelOrder').show();
											$('#orderPay').show()
										} else if (bannerdata.salesStatus == 2) {
											$('#salesStatus').html('已支付，待发货');
											$('#refund').show();
											$("#choujiang").show()
										} else if (bannerdata.salesStatus == 3) {
											$('#salesStatus').html('退款申请中')
										} else if (bannerdata.salesStatus == 4) {
											$('#salesStatus').html('已退款')
										} else if (bannerdata.salesStatus == 5) {
											$('#salesStatus').html('已发货');
											$('#exchangeOrder').show();
											$('#salesReturn').show()
										} else if (bannerdata.salesStatus == 6) {
											$('#salesStatus').html('申请退货中')
										} else if (bannerdata.salesStatus == 7) {
											$('#salesStatus').html('退货中')
										} else if (bannerdata.salesStatus == 8) {
											$('#salesStatus').html('订单退货，订单关闭')
										} else if (bannerdata.salesStatus == 9) {
											$('#salesStatus').html('换货中')
										} else if (bannerdata.salesStatus == 10) {
											$('#salesStatus').html('换货完成')
										} else if (bannerdata.salesStatus == 11) {
											$('#salesStatus').html('申请换货中')
										} else if (bannerdata.salesStatus == 12) {
											$('#salesStatus').html('订单完成');
											$('#exchangeOrder').show()
										} else if (bannerdata.salesStatus == 0) {
											$('#salesStatus').html('取消订单')
										}
										$('#salesOrderid').html(
												bannerdata.salesOrderid);
										$('#salesCreateDate').html(
												bannerdata.salesCreateDate);
										$('#salesAmount')
												.html(
														'¥'
																+ accDiv(
																		bannerdata.salesAmount,
																		100));
										$('#salesYetAmount')
												.html(
														'¥'
																+ accDiv(
																		bannerdata.salesYetAmount,
																		100));
										$('#salesAddressInfo').html(
												bannerdata.salesAddressInfo);
										$('#salesAddressName').html(
												bannerdata.salesAddressName);
										$('#salesAddressTel').html(
												bannerdata.salesAddressTel);
										$('#salesUserRemark').html(
												bannerdata.salesUserRemark);
										$('#salesLockType').val(
												bannerdata.salesLockType);
										if (bannerdata.salesStatus != 0
												&& bannerdata.salesStatus != 1
												&& bannerdata.couponReduce != null
												&& bannerdata.couponReduce != '') {
											$('#couponReduce')
													.html(
															'¥'
																	+ accDiv(
																			bannerdata.couponReduce,
																			100));
											$('#couponMoney').show()
										} else {
											$('#couponMoney').hide()
										}
										if (null != bannerdata.salesPostFirm
												&& bannerdata.salesPostFirm != ''
												&& bannerdata.salesPostNum != null
												&& bannerdata.salesPostNum != '') {
											if (bannerdata.salesPostFirm == 'sto') {
												$('#salesPostFirm')
														.html('申通快递')
											}
											if (bannerdata.salesPostFirm == 'yto') {
												$('#salesPostFirm')
														.html('圆通快递')
											}
											if (bannerdata.salesPostFirm == 'sf') {
												$('#salesPostFirm')
														.html('顺丰快递')
											}
                                            if (bannerdata.salesPostFirm == 'ems') {
                                                $('#salesPostFirm')
                                                    .html('邮政EMS')
                                            }
                                            if (bannerdata.salesPostFirm == 'zto') {
                                                $('#salesPostFirm')
                                                    .html('中通快递')
                                            }
                                            if (bannerdata.salesPostFirm == 'zjs') {
                                                $('#salesPostFirm')
                                                    .html('宅急送')
                                            }
                                            if (bannerdata.salesPostFirm == 'yunda') {
                                                $('#salesPostFirm')
                                                    .html('韵达快递')
                                            }
                                            if (bannerdata.salesPostFirm == 'cces') {
                                                $('#salesPostFirm')
                                                    .html('cces快递')
                                            }
                                            if (bannerdata.salesPostFirm == 'pick') {
                                                $('#salesPostFirm')
                                                    .html('上门提货')
                                            }
                                            if (bannerdata.salesPostFirm == 'htky') {
                                                $('#salesPostFirm')
                                                    .html('汇通快递')
                                            }
                                            if (bannerdata.salesPostFirm == 'ttkdex') {
                                                $('#salesPostFirm')
                                                    .html('天天快递')
                                            }
                                            if (bannerdata.salesPostFirm == 'stars') {
                                                $('#salesPostFirm')
                                                    .html('星晨急便')
                                            }
                                            if (bannerdata.salesPostFirm == 'jd') {
                                                $('#salesPostFirm')
                                                    .html('京东快递')
                                            }
                                            if (bannerdata.salesPostFirm == '01') {
                                                $('#salesPostFirm')
                                                    .html('其他')
                                            }
                                            if (bannerdata.salesPostFirm == '02') {
                                                $('#salesPostFirm')
                                                    .html('上门送货')
                                            }
											if (bannerdata.salesPostFirm == 'longbanwuliu') {
												$('#salesPostFirm')
														.html('龙邦快递')
											}
											$('#salesPostNum').html(
													bannerdata.salesPostNum)
										} else {
											$('#salesPostFirm').html('暂无')
										}
										$('#logistics').attr("postfirm",
												bannerdata.salesPostFirm);
										$('#logistics').attr("postnum",
												bannerdata.salesPostNum);
										$('#logistics')
												.attr(
														"gourl",
														'/tooth/pay/goOrderInfo?orderId='
																+ bannerdata.salesOrderid);
										var item = bannerdata.orderItemList;
										if (item != null) {
											$(item)
													.each(
															function(index) {
																/*var pro_color = switchcolor(this.itemColorId);
																 var colorImg = switchColorImg(this.itemColorId);*/
                                                                var pro_color = this.productColor;
                                                                var colorImg = this.itemProductPic;
																var li = document
																		.createElement('li');
																li.className = 'mui-table-view-cell mui-media bg-f';
																li.innerHTML = '<a proid="'
																		+ this.itemProductId
																		+ '" colorid="' + this.itemColorId + '" class="go_chanpin_details"><table class="my-table-order"><tr><td rowspan="3"><div class="wh-80"><img src="'
																		+ colorImg
																		+ '"></div></td><td style="white-space: pre-line;">'
																		+ this.itemProductName
																		+ '</td><td><span><h5 class="mui-text-center">x'
																		+ this.itemNum
																		+ '</h5></span></td></tr><tr><td><span><h5>颜色:'
																		+ pro_color
																		+ '</h5></span></td></tr><tr><td><span>单价：<span>¥'
																		+ accDiv(
																				this.itemProductDiscount,
																				100)
																		+ '</span></span></td><td><span>合计：<span class="cl-red">¥'
																		+ accDiv(
																				this.itemTotal,
																				100)
																		+ '</span></span></td></tr><table></a>';
																if (this.productStatus != 1) {
																	li.innerHTML = '<a proid="'
																			+ this.itemProductId
																			+ '" class=""><table class="my-table-order" width="100%"><tr><td rowspan="3"><div class="wh-80"><img src="'
																			+ this.itemProductPic
																			+ '"></div></td><td rowspan="3"><span>赠送'
																			+ (this.itemNum / 3)
																			+ '套三年滤芯</span></td><td></td></tr><tr><td><p class="mui-ellipsis delete-cart-style cl-red"></p></td></tr><table></a>'
																}
																table
																		.appendChild(li)
															})
										}
										document.querySelector('.loading-bg').style.display = 'none'
									}
								},
								error : function(xhr, type, errorThrown) {
									console.log(xhr)
								}
							});
			getCartNum()
		});
function goOrderPay() {
	if (!isEfficacious) {
		mui.alert("您的订单有商品下架，请重新生成订单购买!");
		return false
	}
	var salesOrderid = $('#salesOrderid').html();
	var dataArgs = {
		"orderId" : salesOrderid
	};
	if (isPay) {
		layer.open({
			type : 2,
			shadeClose : false,
			content : "正在请求加载订单信息..."
		});
		isPay = false;
		mui
				.ajax(
						base_path + '/cart/orderPay',
						{
							data : dataArgs,
							dataType : 'json',
							type : 'POST',
							timeout : 15000,
							success : function(dt) {
								layer.closeAll();
								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 : "正在请求加载订单信息..."
																	});
															mui
																	.ajax(
																			base_path
																					+ '/order/checkOrderPaySuccess',
																			{
																				data : orderIds,
																				dataType : 'json',
																				type : 'post',
																				timeout : 15000,
																				success : function(
																						dt) {
																					if (dt.status) {
																						window.location.href = base_path
																								+ "/order/paySuccess?orderId="
																								+ orderIds.orderId
																								+ "&dates="
																								+ new Date()
																										.getTime()
																					} else {
																						isPay = false;
																						setTimeout(
																								checkOrderStatus(orderIds),
																								1000)
																					}
																				},
																				error : function(
																						xhr,
																						type,
																						errorThrown) {
																					isPay = true;
																					layer
																							.closeAll();
																					mui
																							.alert("您的网络异常，请刷新重试!");
																					return
																				}
																			})
														} else {
															isPay = true;
															mui
																	.alert("对不起，支付失败!请稍后重试!"
																			+ dt.data.orderId
																			+ "支付失败!");
															window.location.href = "/tooth/pay/goOrderInfo?orderId="
																	+ dt.data.orderId
																	+ "&dates="
																	+ new Date()
																			.getTime();
															;
														}
													})
								} else {
									isPay = true;
									mui.alert(dt.message)
								}
							},
							error : function(xhr, type, errorThrown) {
								isPay = true;
								layer.closeAll();
								mui.alert("您的网络异常，请刷新重试!");
								return
							}
						})
	} else {
		mui.alert("正在请求支付中~~请稍后!");
		return
	}
};
function operation(num) {
	var url = '';
	var data = new Array;
	var status = $('#oldStatus').val();
	var orderId = $('#salesOrderid').html();
	var salesLockType = $('#salesLockType').val();
	var changeOrderId = $("#changeOrderId").val();
	var returnOrderId = $("#returnOrderId").val();
	var info = "";
	var message = "";
	if (status == 1) {
		if (salesLockType == 2) {
			mui.alert("该订单不能取消!");
			return false
		}
		info = "您确定要取消订单吗?";
		url = base_path + '/cart/cancelOrder';
		data = {
			orderId : orderId,
			changeOrderId : changeOrderId,
			status : status
		}
	} else if (status == 2) {
		if (salesLockType == 2) {
			mui.alert("您申请了微代理,此单已被锁定,无法进行退款操作!");
			return false
		}
		info = "您确定要申请退款?";
		message = "此订单不能退款!";
		url = base_path + '/cart/returnOrder';
		var returnReason = $('#return-money-id').val();
        returnReason = trimRight(trimLeft(returnReason));
        var pattern=/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/;



        if (returnReason == null || returnReason == '') {
			mui.alert("备注必填！请输入1-99个字!");
			return false
		} else {
			/*if (!checkAddress(returnReason)) {
			 mui.alert("您输入的格式有误,请重新输入!");
			 return false
			 }*/
            if(pattern.test(returnReason)){
                mui.alert("您输入的格式有误,请重新输入!");
                return false
            }
		}
		data = {
			orderId : orderId,
			remark : returnReason,
			status : status
		}
	} else if (status == 5 || status == 12) {
		if (num == 1) {
			info = "您确定要申请换货吗?";
			message = "超出换货期限,不能换货!";
			url = base_path + '/cart/exchangeOrder';
			data = {
				orderId : orderId,
				changeOrderId : changeOrderId,
				status : status
			}
		} else {
			if (salesLockType == 2) {
				mui.alert("您申请了微代理,此单已被锁定,无法进行退货操作!");
				return false
			}
			info = "您确定要申请退货吗?";
			message = "超出退货期限,不能退货!";
			url = base_path + '/cart/returnOrder';
			var returnReason = $('#return-pay-id').val();
            returnReason = trimRight(trimLeft(returnReason));
            var pattern=/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/;


            if (returnReason == null || returnReason == '') {
				mui.alert("备注必填！请输入1-99个字!");
				return false
			} else {
				/*if (!checkAddress(returnReason)) {
					mui.alert("您输入的格式有误,请重新输入!");
					return false
				}*/
                if(pattern.test(returnReason)){
                    mui.alert("您输入的格式有误,请重新输入!");
                    return false
                }
			}
			data = {
				orderId : orderId,
				remark : returnReason,
				returnOrderId : returnOrderId,
				status : status
			}
		}
	}
	var btnArray = [ '否', '是' ];
	mui.confirm(info, 'WaterO-提示', btnArray, function(e) {
		if (e.index == 1) {
			$.ajax({
				data : data,
				url : url,
				dataType : 'json',
				type : 'post',
				timeout : 15000,
				success : function(dt) {
					if (dt.status) {
						var bannerdata = dt.data;
						location.reload()
					} else {
						mui.alert(message)
					}
				}
			})
		}
	})


    //去掉左边的空白
    function trimLeft(s) {
        if (s == null) {
            return "";
        }
        var whitespace = new String(" \t\n\r");
        var str = new String(s);
        if (whitespace.indexOf(str.charAt(0)) != -1) {
            var j = 0, i = str.length;
            while (j < i && whitespace.indexOf(str.charAt(j)) != -1) {
                j++;
            }
            str = str.substring(j, i);
        }
        return str;
    }

//去掉右边的空白
    function trimRight(s) {
        if (s == null) return "";
        var whitespace = new String(" \t\n\r");
        var str = new String(s);
        if (whitespace.indexOf(str.charAt(str.length - 1)) != -1) {
            var i = str.length - 1;
            while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1) {
                i--;
            }
            str = str.substring(0, i + 1);
        }
        return str;
    }
};