define(['zepto', 'medtap'], function($, medtap) {
	var detail = {
		orderSn: medtap.getRequest('id'),
		wechatId: medtap.getRequest('wechatId'),
		doctorId:0,
		init: function() {
			detail.getDetail();
			detail.bindEve();
			detail.getWechatTicket();
		},
		bindEve: function() {
			$('.pay').unbind().bind('click', function() {
				medtap.pushWindow(
					'https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_wxPay/wxPay.html?orderType=imgConsult&orderId=' +
					detail.orderSn + '&wechatId=' + detail.wechatId + '&wxLungToken=' + localStorage.wxLungToken)
			});

			$('.cancel').unbind().bind('click', function() {
				detail.cancelOrder();
			})
			$('.cancel_btn').unbind().bind('click', function() {
				detail.cancelOrder();
			});
			
		},
		getDetail: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/trade/order/getOrderByOrderSn',
				type: 'get',
				async: false,
				data: {
					orderSn: detail.orderSn
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var detail = res.content.order;
						var imgConsult = detail.imageConsultOrder;
						detail.doctorId = imgConsult.doctorId;
						//alert(detail.doctorId)
						$('.service_type').html(detail.payDesc);
						$('.service_doctor').html(imgConsult.doctorName);
						$('.service_price').html(detail.payFee + '元');
						var sex = '';
						if (imgConsult.sex == 'M') {
							sex = '男';
						} else {
							sex = '女';
						}
						$('.patient_name').html(imgConsult.patientName + ' (' + sex + ' ' + imgConsult.age + '岁' + ')');
						$('.patient_disease').html(imgConsult.diseaseDiagnosisDesc);
						$('.patient_disease_type').html(imgConsult.pathologyTypeDesc);
						$('.disease_area').html(imgConsult.describe);
						$('.help_area').html(imgConsult.expectationHelp);
						if (detail.status.key == '0') {
							$('.order_status_type').html(detail.status.value);
							$('.pay_btn').show();
						} else {
							$('.order_status_type').html(imgConsult.serviceStatus.value);
							if (detail.status.key == '1' && imgConsult.serviceStatus.key == '0' || imgConsult.serviceStatus.key ==
								'1' || imgConsult.serviceStatus.key == '2') {
								$('.cancel_btn').show();
							} else if (detail.status.key == '1' && imgConsult.serviceStatus.key == '3') {
								$('.consult_now').show();
								$('.consult_now').unbind().bind('click',function(){
									medtap.pushWindow('../../wechat_lung_chat/pages/imgConsultChat.html?orderSn=' + detail.orderSn + '&doctorId=' + imgConsult.doctorId);
								})
							}

						}
						if (imgConsult.describePic.length > 0) {
							var str = '';
							for (var i = 0; i < imgConsult.describePic.length; i++) {
								str += '<img src="' + imgConsult.describePic[i] + '" >'
							}
							$('.content_item_imgList').show();
							$('.content_item_imgList').html(str);
						} else {
							$('.content_item_imgList').hide();
						}
						$('.order_orderSn').html(detail.orderSn);
						$('.order_time').html(detail.createTime.substring(0, 16));
						if (detail.hasOwnProperty('payTime')) {
							$('.pay_box').show();
							$('.order_pay_time').html(detail.payTime)
						} else {
							$('.pay_box').hide();
						}
					}
				}
			})
		},
		cancelOrder: function() {
			if (!confirm('确定要取消订单吗？')) {
				return;
			}
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/trade/order/cancelOrder',
				type: 'post',
				contentType: "application/json",
				async: false,
				data: {
					orderSn: detail.orderSn
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						medtap.toast({
							message: '取消订单成功',
							time: 1500
						});
						setTimeout(function() {
							window.location.reload();
						}, 1500);
					}
				}
			})
		},
		getWechatTicket: function() {
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/wechat/lung/fetchWechatTicket',
				type: 'GET',
				async: false,
				data: {
					url: window.location.href
				},
				success: function(res) {
					var data = res.content;
					wx.config({
						debug: false,
						appId: data.appId, // 必填，公众号的唯一标识
						timestamp: data.timestamp, // 必填，生成签名的时间戳
						nonceStr: data.nonceStr, // 必填，生成签名的随机串
						signature: data.signature, // 必填，签名，见附录1
						jsApiList: [
							'previewImage'
						]
					})
				}
			})
		}, 
		//图片预览
		previewImage: function() {
			$('.content_item_imgList').on('click', 'img', function() {
				//alert(1)
				var imgArray = [];
				var curImageSrc = $(this).attr('src');
				if (curImageSrc) {
					$('.content_item_imgList img').each(function(index, el) {
						var itemSrc = $(this).attr('src');
						imgArray.push(itemSrc);
					});
					wx.previewImage({
						current: curImageSrc,
						urls: imgArray
					});
				}
			});
		}
	}
	detail.init();
	detail.previewImage();
})
