define(["zepto", "medtap"], function($, medtap) {
	var wxPay = {
		zf_appId: '',
		zf_nonceStr: '',
		zf_timestamp: '',
		zf_prepay_id: '',
		zf_sign: '',
		zf_signType: '',
		timestamp: '',
		nonceStr: '',
		orderId: medtap.getRequest('orderId'),
		wechatId: medtap.getRequest('wechatId'),
		orderType: medtap.getRequest('orderType'),
		wechatToken: medtap.getRequest('wxLungToken'),
		wechatId:medtap.getRequest('wechatId'),
		checkWx: function() { //微信认证
			$.ajax({
				url: 'https://testdevgw.medtap.cn/wechat/lung/fetchWechatTicket',
				type: 'get',
				async: false,
				contentType: "application/json",
				data: {
					url: window.location.href
				},
				success: function(data) {
					var res = data.content;
					wxPay.zf_appId = res.appId;
					wxPay.initWxOrder();
					
				},
				error: function() {}
			})
		},
		initWxOrder: function() { //初始化微信订单
			var _this = this;
			$.ajax({
				url: 'https://testdevgw.medtap.cn/pay/wechat/payOrder',
				type: 'post',
				contentType: "application/json",
				headers: {
					'apptype': 'wechat_lung',
					'user-os': 'wechat',
					'token':medtap.getRequest('wxLungToken'),
				},
				data:JSON.stringify({
					orderSn:wxPay.orderId,
					wechatId:wxPay.wechatId
				}),
				success: function(res) {
					if (res.success == true) {
						var data = res.content.payParams;
						_this.zf_prepay_id = data.package;
						_this.zf_sign = data.paySign;
						_this.zf_signType = data.signType;
						_this.zf_appId = data.appId;
						_this.zf_nonceStr = data.nonceStr;
						_this.zf_timestamp = data.timeStamp;
						if (typeof WeixinJSBridge == "undefined") {
							if (document.addEventListener) {
								document.addEventListener('WeixinJSBridgeReady', wxPay.pay, false);
							} else if (document.attachEvent) {
								document.attachEvent('WeixinJSBridgeReady', wxPay.pay);
								document.attachEvent('onWeixinJSBridgeReady', wxPay.pay);
							}
						} else {
							wxPay.pay();
						}
					}
				}
			})
		},
		pay: function() {
			WeixinJSBridge.invoke('getBrandWCPayRequest', {
					"appId": wxPay.zf_appId,
					"timeStamp": wxPay.zf_timestamp,
					"nonceStr": wxPay.zf_nonceStr,
					"package": wxPay.zf_prepay_id,
					"signType": wxPay.zf_signType,
					"paySign": wxPay.zf_sign
				},
				function(res) {
					switch (res.err_msg) {
						case 'get_brand_wcpay_request:cancel':
							medtap.toast({
								message:'用户取消支付！',
								time:1500
							})
							setTimeout(function() {
								history.back();
							}, 1500);
							break;
						case 'get_brand_wcpay_request:fail':
							medtap.toast({
								message:'支付失败！',
								time:1500
							})
							setTimeout(function() {
								history.back();
							}, 1500);
							break;
						case 'get_brand_wcpay_request:ok':
							if(wxPay.orderType == 'pmd'){
								medtap.toast({
									message:'支付成功！',
									time:1500
								});
								setTimeout(function(){
									medtap.pushWindow('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_prvivateDoctor/pages/success.html?id=' + medtap.getRequest('orderId')+ '&wechatId=' + wxPay.wechatId)
								},1500)
							}else if(wxPay.orderType == 'imgConsult'){
								medtap.toast({
									message:'支付成功！',
									time:1500
								});
								setTimeout(function(){
									medtap.pushWindow('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_imgConsult/pages/success.html?id=' + medtap.getRequest('orderId')+ '&wechatId=' + wxPay.wechatId)
								},1500);
							}else if(wxPay.orderType == 'consult'){
								medtap.toast({
									message:'支付成功！',
									time:1500
								});
								setTimeout(function(){
									medtap.pushWindow('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_consult/pages/consultDetail.html?id=' + medtap.getRequest('orderId') + '&wechatId=' + wxPay.wechatId )
								},1500);
							}
							break;
						default:
							alert(JSON.stringify(res));
							break;
					}
				}
			);
		},
		payWxOrder: function() { //启动微信支付
			alert('payWxOrder');
			var _this = this;
			medtap.loading(0);
			
		},
		init: function() {
			medtap.loading(1);
			var _this = this;
			this.checkWx();
		}
	}

	wxPay.init();
});
