mui.ready(function() {
	marquee_Show();
	getCartNum();
	mui.each(document.querySelectorAll('#menu-lease-other .mui-control-item'), function(index, el) {
		el.addEventListener('tap', function() {
			switch(index) {
				case 0:
					mui.openWindow({
						url: base_path + '/agentWechat/agentNext'
					});
					break;
				case 1:
					mui.openWindow({
						url: 'after_service_watero.html'
					});
					break;
				case 2:
					mui.confirm('服务时间:工作日9:00-18:00', '客服电话:<a>400-8006692</a>', ['取消', '拨打'], function(e) {
						if(e.index == 1) {
							location.href = 'tel:4008006692'
						}
					});
					break;
				case 3:
					mui.openWindow({
						url: 'faq.html'
					});
					break;
				default:
					break
			}
		}, false)
	});
	mui.each(document.querySelectorAll('#menu-nav .mui-control-item'), function(index, el) {
		var th = this;
		el.addEventListener('tap', function() {
			mui.openWindow({
				url: th.getAttribute("href")
			});
		}, false)
	});
	$.ajax(base_path + '/agent/member?dates=' + new Date().getTime(), {
		dataType: 'json',
		type: 'post',
		timeout: 15000,
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		success: function(dt) {
			if(dt.isRedirect) {
				location.href = dt.redirectURL;
			} else {
				var data = dt.data;
				if(dt.status) {
					if(data.userHead != null && typeof(data.userHead) != "undefined" && data.userHead != '') {
						var imgObj = $("<img src='" + data.userHead + "?" + new Date().getTime() + "'/>");
						$("#headDiv").empty().append(imgObj)
					}
					if(data.userNickname != null && typeof(data.userNickname) != "undefined" && data.userNickname != '') {
						$("#userNickname").html(decodeURI(data.userNickname))
					}
					var userNickname = $("#userNickname").html();
					if(data.shareType != null && typeof(data.shareType) != "undefined" && data.shareType == 2) {
						$("#2WM").show();
						if(userNickname != "") {
							$("#userNickname").html(userNickname + "(会员)")
						}
					} else {
						if(userNickname != "") {
							$("#userNickname").html(userNickname + "(用户)")
						}
					}
					$.ajax({
						type: 'post',
						url: base_path + '/probationOrder/membersProbationOrder',
						data: {},
						dataType: 'json',
						timeout: 15000,
						xhrFields: {
							withCredentials: true
						},
						crossDomain: true,
						success: function(dts) {
							if(dts.isRedirect) {
								location.href = dts.redirectURL;
							} else {
								if(dts.status) {
									var str = '';
									if(dts.data.probationOrderDtoList.length == 0) {
										$("#try_order_list").hide()
									}
									mui(dts.data.probationOrderDtoList).each(function(index) {
										switch(this.probationStatus) {
											case 1:
												str = "待派送";
												break;
											case 2:
												str = "派送中";
												break;
											case 3:
												str = "待支付";
												break;
											case 4:
												str = "暂停试用";
												break;
											case 5:
												str = "支付完成";
												break;
											default:
												str = "订单关闭";
												break
										}
										var str1 = '',
											str2 = '';
										if(str == "待支付") {
											str1 = '<a data-loading-icon="mui-spinner mui-spinner-custom" data-loading-text="正在支付" class="mui-btn try-btn-pay mui-pull-right" style="margin-right: 12px;" proid="' + this.probationOrderid + '">立即支付</a>';
											str2 = '<div>试用剩余时间:<span id="time_out' + (index + 1) + '"></span></div>'
										}
										$("#try_order_list").append('<li class="mui-table-view-cell mui-media"><a class="go_try_order" proid="' + this.probationOrderid + '" style="margin-left: -20px;"><span class="mui-media-object mui-pull-left wh-80"><img src="' + this.productIntroduceImg + '"></span><div class="mui-media-body center-middle"><div>' + str + '</div><div>订单号:' + this.probationOrderid + '</div>' + str2 + '</div></a><div class="btn-box">' + str1 + '</div></li>');
										if(str == "待支付") {
											addTimer('time_out' + (index + 1), this.probationRemainingTime / 1000)
										}
									})
								} else {
									mui.alert(dts.data.message)
								}
							}
						},
						error: function(xhr, type, errorThrown) {
							console.log(xhr);
							mui.alert("请检查网络或刷新页面重试!")
						}
					});
					document.querySelector('.loading-bg').style.display = 'none'
				} else {
					var btnArray = ['否', '是'];
					mui.confirm(dt.message, '提示', btnArray, function(e) {
						if(e.index == 1) {
							location.reload(true)
						}
					})
				}
			}
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr)
		}
	});
	if(marquee != '') {
		$('body').append('<div class="marquee-show"><div></div></div>')
	}
});
mui('body').on('tap', '#rent_wait_deliver', function() {
	mui.openWindow({
		url: '/watero/view/rent/lease_order_details.html?rentStatus=-1'
	})
});
mui('body').on('tap', '#rent_already_deliver', function() {
	mui.openWindow({
		url: '/watero/view/rent/lease_order_details.html?rentStatus=5'
	})
});
mui('body').on('tap', '.icon-shebeiliebiao', function() {
	mui.openWindow({
		url: '/watero/view/rent/my_equipment.html'
	})
});
mui('body').on('tap', '#xieyi-dalog', function() {
	mui.openWindow({
		url: '/watero/view/rent/try_xieyi.html'
	});
});
mui('body').on('tap', '.go_try_order', function() {
	mui.openWindow({
		url: base_path + '/probation/try_order?orderId=' + this.getAttribute("proid")
	})
});
var isPay = true;
mui('body').on('tap', '.try-btn-pay', function() {
	var th = this;
	if(isPay) {
		mui(this).button('loading');
		isPay = false;
		$.ajax({
			type: 'post',
			url: base_path + '/probation/pay',
			data: {
				"orderId": th.getAttribute("proid")
			},
			dataType: 'json',
			timeout: 15000,
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			success: function(dt) {
				if(dt.isRedirect) {
					location.href = dt.redirectURL;
				} else {
					if(dt.status) {
						var obj = JSON.parse("{" + dt.data.info + "}");
						WeixinJSBridge.invoke('getBrandWCPayRequest', obj, function(res) {
							if(res.err_msg == 'get_brand_wcpay_request:ok') {
								$.ajax({
									type: 'post',
									url: base_path + '/probation/check_probation_pay_success',
									data: {
										"orderId": th.getAttribute("proid")
									},
									dataType: 'json',
									timeout: 15000,
									xhrFields: {
										withCredentials: true
									},
									crossDomain: true,
									success: function(data) {
										if(data.isRedirect) {
											location.href = data.redirectURL;
										} else {
											if(data.status) {
												if(data.data == 5) {
													mui.alert("支付成功", '', function() {
														mui.openWindow({
															url: base_path + '/probation/try_order?orderId=' + th.getAttribute("proid")
														})
													})
												} else {
													mui.alert(data.message, '', function() {
														mui(th).button('reset')
													})
												}
											} else {
												mui(th).button('reset');
												mui.alert(data.message)
											}
										}
									},
									error: function(xhr, type, errorThrown) {
										isPay = true;
										console.log(xhr);
										mui(th).button('reset');
										mui.alert("请检查网络或刷新页面重试!")
									}
								})
							} else {
								isPay = true;
								mui(th).button('reset');
								mui.alert("对不起,支付失败!请稍后重试!")
							}
						})
					} else {
						mui(th).button('reset');
						mui.alert(dt.message)
					}
				}
			},
			error: function(xhr, type, errorThrown) {
				isPay = true;
				console.log(xhr);
				mui(th).button('reset');
				mui.alert("请检查网络或刷新页面重试!")
			}
		})
	} else {
		mui.alert("正在请求支付中~~请稍后!");
		return
	}
});
var addTimer = function() {
	var list = [],
		interval;
	return function(id, time) {
		if(!interval) {
			interval = setInterval(go, 1000)
		}
		list.push({
			ele: document.getElementById(id),
			time: time
		})
	};

	function go() {
		for(var i = 0; i < list.length; i += 1) {
			list[i].ele.innerHTML = getTimerString(list[i].time ? list[i].time -= 1 : 0);
			if(!list[i].time) {
				list.splice(i--, 1)
			}
		}
	}

	function getTimerString(time) {
		d = Math.floor(time / 86400), h = Math.floor((time % 86400) / 3600), m = Math.floor(((time % 86400) % 3600) / 60), s = Math.floor(((time % 86400) % 3600) % 60);
		if(time > 0) {
			return d + "天" + h + "时" + m + "分" + s + "秒"
		} else {
			return "试用已结束"
		}
	}
}();