define(['zepto', 'medtapCore', 'mui', 'mui.zoom', 'mui.picker', 'mui.previewimage'], function($, medtapCore, mui) {
	Date.prototype.Format = function(fmt) { //author: meizz 
		var o = {
			"M+": this.getMonth() + 1, //月份 
			"d+": this.getDate(), //日 
			"h+": this.getHours(), //小时 
			"m+": this.getMinutes(), //分 
			"s+": this.getSeconds(), //秒 
			"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
			"S": this.getMilliseconds() //毫秒 
		};
		if(/(y+)/.test(fmt))
			fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		for(var k in o)
			if(new RegExp("(" + k + ")").test(fmt))
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
	}

	var detail = {
		typeDic: {
			'HWYL': '海外医疗',
			'ZLFXPG': '肿瘤风险评估',
			'QQDEYJ': '全球第二意见',
			'FMJY': '赴美就医',
			'RBZZZL': '日本质子治疗',
			'GAXYZL': '港澳新药治疗',
			'QQZY': '全球找药',
			'GNZY': '国内找药'
		},
		statusDic: {
			'0': '未支付',
			'1': '已支付',
			'2': '已无效',
			'3': '已取消',
			'4': '申请中'
		},
		wxLungToken:localStorage.wxLungToken,
		init: function() {
			medtapCore.loading(1);
			if(!!medtapCore.getRequest('id') == false) {
				alert('未能获取到详情页面的id');
			}
			$.ajax({
				url: 'https://testdevgw.medtap.cn/trade/order/getOrderByOrderSn',
				contentType: "application/json",
				type: 'get',
				async: true,
				dataType: 'json',
				headers:{
					'apptype':'wechat_lung',
					'user-os':'wechat',
					token:localStorage.wxLungToken
				},
				data:{
					orderSn: medtapCore.getRequest('id'),
				},
				success: function(data) {
					medtapCore.loading(0);
					if(data.success) {
						var data = data.content.order;
						$('#orderSn').val(!!data.orderSn ? data.orderSn : '');
						$('#consultType').val(!!data.hasOwnProperty('orderType') ? data.orderType.value : '');
						$('#status').val(!!data.status ? data.status.value : '');
						$('#createTime').val(!!data.createTime ? data.createTime : '');
						$('#mobile').val(!!data.hasOwnProperty('consultOrder') ? data.consultOrder.mobile : '');
						$('#doctorName').val(!!data.consultOrder.hasOwnProperty('doctorName') ? data.consultOrder.doctorName : '未确定');
						$('#consultTime').val(!!data.consultOrder.hasOwnProperty('consultTime') ? data.consultOrder.consultTime : '');
						$('#price').val(!!data.consultOrder.hasOwnProperty('payFee') ? data.consultOrder.payFee + '元' : '');
						$('#diseaseDescription').val(!!data.consultOrder.hasOwnProperty('diseaseDescription') ? data.consultOrder.diseaseDescription : '');
						$('#expectationHelp').val(!!data.consultOrder.hasOwnProperty('expectationHelp') ? data.consultOrder.expectationHelp : '');
						if(!!data.status){
							$('.by-status[status="' + data.status.key + '"]').show();
						}
						detail.bind();
						if(data.consultOrder.hasOwnProperty('picUrl')){
							var pics =  data.consultOrder.picUrl,
								str = '';
							for(var i=0, len=pics.length; i<len; i++){
								str += 
									'<div class="mui-col-sm-3 mui-col-xs-3">' + 
										'<img src="' + pics[i] + '" data-preview-src="" data-preview-group="1" alt="显示失败了"/>' + 
									'</div>';
							}
							if(pics.length > 0){
								$('.img-box').html(str);
								mui.previewImage();
							}
						}
					} else {
						alert(data.resultCode);
					}
				},
				error: function(err) {
					medtapCore.loading(0);
					console.log(err);
				}
			});

		},
		bind: function(){
			$('.cancel').on('click', function(){
				if(!confirm('确定要取消订单吗？')){
					return;
				}
				$.ajax({
					url: 'https://testdevgw.medtap.cn/trade/order/cancelOrder',
					contentType: "application/json",
					type: 'post',
					async: true,
					dataType: 'json',
					headers:{
						'apptype':'wechat_lung',
						'user-os':'wechat',
						'token':localStorage.wxLungToken
					},
					data: JSON.stringify({
						orderSn: medtapCore.getRequest('id'),
					}),
					success: function(data) {
						medtapCore.loading(0);
						if(data.success) {
							alert('取消订单成功');
							window.location.reload();
						} else {
							alert(data.resultDesc);
						}
					},
					error: function(err) {
						medtapCore.loading(0);
						console.log(err);
					}
				});	
			});
			$('.pay').on('click', function(){
				window.location.href = 'https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_wxPay/wxPay.html?orderType=consult&orderId=' + medtapCore.getRequest('id') + '&wxLungToken=' + detail.wxLungToken + '&wechatId=' + medtapCore.getRequest('wechatId');
			
			});
		}
	};

	detail.init();
//	detail.bind();
});