define(['zepto','medtap'],function($,medtap){
	var order = {
		wechatId:medtap.getRequest('wechatId'),
		init:function(){
			//order.getOrderList();
			order.bindEve();
			order.getToken();
		},
		bindEve:function(){
			$('.tab_item').on('click',function(){
				$(this).addClass('tabOn');
				$(this).siblings('.tab_item').removeClass('tabOn');
				if($('.all').hasClass('tabOn')){
					//获取全部
					order.getOrderList(99);
					/* order.bindEve(); */
				}else if($('.bePaid').hasClass('tabOn')){
					//获取待支付列表
					order.getOrderList(0);
					/* order.bindEve(); */
				}else if($('.inService').hasClass('tabOn')){
					//获取服务中列表
					order.getOrderList(1);
					/* order.bindEve(); */
				}else if($('.completed').hasClass('tabOn')){
					//获取已完成列表
					order.getOrderList(2);
					/* order.bindEve(); */
				}
			});
			
			
		},
		getOrderList:function(status){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/trade/order/listConsultOrder',
				type:'get',
				async:false,
				data:{
					offset:1,
					limit:20,
					orderStatus:status
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var list = res.content.orderList;
						var html = '';
						if(list.length != 0){
							$('.myOrder_list_warp').show();
							$('.no_order').hide();
							for(var i = 0; i < list.length;i++){
								var type = list[i].orderType.key;
								if(type == 'TWZX'){
										if(list[i].payStatus.key == '0'){
											 html += '<div class="myOrder_list_item img_consult" data-id="'+list[i].orderSn+'">'+
														'<div class="myOrder_list_item_header clearfix">'+
															'<img src="../images/order list_twzx_png@2x.png" >'+
															'<span class="myOrder_list_item_type">图文咨询</span>'+
															'<span class="myOrder_list_item_status">'+list[i].payStatus.value+'</span>'+
														'</div>'+
														'<div class="myOrder_list_detail">'+
															'<p class="myOrder_list_detail_item clearfix mar-b">'+
																'<span class="myOrder_list_detail_item_title">医生姓名:</span>'+
																'<span class="myOrder_list_detail_item_desc doctor_name">'+list[i].imageConsultOrder.doctorName+'</span>'+
															'</p>'+
															'<p class="myOrder_list_detail_item clearfix">'+
																'<span class="myOrder_list_detail_item_title">咨询问题:</span>'+
																'<span class="myOrder_list_detail_item_desc questioon">'+list[i].imageConsultOrder.describe+'</span>'+
															'</p>'+
														'</div>'+
													'</div>';
										}else{
											html += '<div class="myOrder_list_item img_consult" data-id="'+list[i].orderSn+'">'+
														'<div class="myOrder_list_item_header clearfix">'+
															'<img src="../images/order list_twzx_png@2x.png" >'+
															'<span class="myOrder_list_item_type">图文咨询</span>'+
															'<span class="myOrder_list_item_status">'+list[i].imageConsultOrder.serviceStatus.value+'</span>'+
														'</div>'+
														'<div class="myOrder_list_detail">'+
															'<p class="myOrder_list_detail_item clearfix mar-b">'+
																'<span class="myOrder_list_detail_item_title">医生姓名:</span>'+
																'<span class="myOrder_list_detail_item_desc doctor_name">'+list[i].imageConsultOrder.doctorName+'</span>'+
															'</p>'+
															'<p class="myOrder_list_detail_item clearfix">'+
																'<span class="myOrder_list_detail_item_title">咨询问题:</span>'+
																'<span class="myOrder_list_detail_item_desc questioon">'+list[i].imageConsultOrder.describe+'</span>'+
															'</p>'+
														'</div>'+
													'</div>';
										}
								}else if(type == 'PMDTWZX'){
									 if(list[i].payStatus.key == '0'){
										 html += '<div class="myOrder_list_item pmd" data-id="'+list[i].orderSn+'">'+
													'<div class="myOrder_list_item_header clearfix">'+
														'<img src="../images/order list_srys_png@2x.png" >'+
														'<span class="myOrder_list_item_type">私人医生</span>'+
														'<span class="myOrder_list_item_status">'+list[i].payStatus.value+'</span>'+
													'</div>'+
													'<div class="myOrder_list_detail">'+
														'<p class="myOrder_list_detail_item clearfix mar-b">'+
															'<span class="myOrder_list_detail_item_title">医生姓名:</span>'+
															'<span class="myOrder_list_detail_item_desc doctor_name">'+list[i].pmdConsultOrder.doctorName+'</span>'+
														'</p>'+
														'<p class="myOrder_list_detail_item clearfix">'+
															'<span class="myOrder_list_detail_item_title">患者姓名:</span>'+
															'<span class="myOrder_list_detail_item_desc patient_name">'+list[i].pmdConsultOrder.patientName+'</span>'+
														'</p>'+
													'</div>'+
												'</div>';
									 }else{
										 html += '<div class="myOrder_list_item pmd" data-id="'+list[i].orderSn+'">'+
													'<div class="myOrder_list_item_header clearfix">'+
														'<img src="../images/order list_srys_png@2x.png" >'+
														'<span class="myOrder_list_item_type">私人医生</span>'+
														'<span class="myOrder_list_item_status">'+list[i].pmdConsultOrder.serviceStatus.value+'</span>'+
													'</div>'+
													'<div class="myOrder_list_detail">'+
														'<p class="myOrder_list_detail_item clearfix mar-b">'+
															'<span class="myOrder_list_detail_item_title">医生姓名:</span>'+
															'<span class="myOrder_list_detail_item_desc doctor_name">'+list[i].pmdConsultOrder.doctorName+'</span>'+
														'</p>'+
														'<p class="myOrder_list_detail_item clearfix">'+
															'<span class="myOrder_list_detail_item_title">患者姓名:</span>'+
															'<span class="myOrder_list_detail_item_desc patient_name">'+list[i].pmdConsultOrder.patientName+'</span>'+
														'</p>'+
													'</div>'+
												'</div>';
									 }
								}else if(type == 'DHZX'){
									if(list[i].payStatus.key == '0'){
										html += '<div class="myOrder_list_item consult" data-id="'+list[i].orderSn+'">'+
													'<div class="myOrder_list_item_header clearfix">'+
														'<img src="../images/order list_dhzx_png@2x.png" >'+
														'<span class="myOrder_list_item_type">电话咨询</span>'+
														'<span class="myOrder_list_item_status">'+list[i].payStatus.key+'</span>'+
													'</div>'+
													'<div class="myOrder_list_detail">'+
														'<p class="myOrder_list_detail_item clearfix mar-b">'+
															'<span class="myOrder_list_detail_item_title">医生姓名:</span>'+
															'<span class="myOrder_list_detail_item_desc doctor_name">'+list[i].consultOrder.doctorName+'</span>'+
														'</p>'+
														'<p class="myOrder_list_detail_item clearfix mar-b">'+
															'<span class="myOrder_list_detail_item_title">预约时间:</span>'+
															'<span class="myOrder_list_detail_item_desc time">'+list[i].formatTime+'</span>'+
														'</p>'+
														'<p class="myOrder_list_detail_item clearfix">'+
															'<span class="myOrder_list_detail_item_title">咨询问题:</span>'+
															'<span class="myOrder_list_detail_item_desc askQuestion">'+list[i].consultOrder.diseaseDescription+'</span>'+
														'</p>'+
													'</div>'+
												'</div>';
									}else{
										html += '<div class="myOrder_list_item consult" data-id="'+list[i].orderSn+'">'+
													'<div class="myOrder_list_item_header clearfix">'+
														'<img src="../images/order list_dhzx_png@2x.png" >'+
														'<span class="myOrder_list_item_type">电话咨询</span>'+
														'<span class="myOrder_list_item_status">'+list[i].serviceStatus.value+'</span>'+
													'</div>'+
													'<div class="myOrder_list_detail">'+
														'<p class="myOrder_list_detail_item clearfix mar-b">'+
															'<span class="myOrder_list_detail_item_title">医生姓名:</span>'+
															'<span class="myOrder_list_detail_item_desc doctor_name">'+list[i].consultOrder.doctorName+'</span>'+
														'</p>'+
														'<p class="myOrder_list_detail_item clearfix mar-b">'+
															'<span class="myOrder_list_detail_item_title">预约时间:</span>'+
															'<span class="myOrder_list_detail_item_desc time">'+list[i].formatTime+'</span>'+
														'</p>'+
														'<p class="myOrder_list_detail_item clearfix">'+
															'<span class="myOrder_list_detail_item_title">咨询问题:</span>'+
															'<span class="myOrder_list_detail_item_desc askQuestion">'+list[i].consultOrder.diseaseDescription+'</span>'+
														'</p>'+
													'</div>'+
												'</div>';
									}
								}
							}
							$('.myOrder_list_warp').html(html)
						}else{
							$('.no_order').show();
							$('.myOrder_list_warp').hide();
						}
						$('.myOrder_list_item').unbind().bind('click',function(){
							var orderSn = $(this).attr('data-id');
							if($(this).hasClass('img_consult')){
								//console.log('跳转至图文咨询详情');
								medtap.pushWindow('../../wechat_lung_imgConsult/pages/imgConsultDetail.html?id=' + orderSn + '&wechatId=' + order.wechatId);
							}else if($(this).hasClass('pmd')){
								//console.log('跳转至私人医生订单详情')
								medtap.pushWindow('../../wechat_lung_prvivateDoctor/pages/pmdDetail.html?id='+ orderSn + '&wechatId=' + order.wechatId);
							}else if($(this).hasClass('consult')){
								//console.log('跳转至电话咨询订单详情')
								medtap.pushWindow('../../wechat_lung_consult/pages/consultDetail.html?id=' + orderSn + '&wechatId=' + order.wechatId)
							}
						})
					}
				}
			})
		},
		getToken: function() {
			localStorage.wxLungToken = '';
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/auth/token',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					wechatId: order.wechatId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var token = res.content.token;
						localStorage.setItem('wxLungToken', token);
						order.checkUser();
					} else {
		
					}
				},
				error: function(res) {
					console.log(res.resultDesc);
				}
			})
		},
		checkUser: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/wechat/user/getWechatUser',
				type: 'GET',
				async: false,
				data: {
					wechatId: order.wechatId
				},
				success: function(res) {
					medtap.loading(0);
					var info = res.content.wechatUser;
					if (info.hasOwnProperty('userId')) {
						order.getUserDetail();
					} else {
						window.location.replace('../login/login.html?wechatId=' + order.wechatId + '&route=order');
					}
				}
			})
		},
		getUserDetail:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/user/getUserDetail',
				type:'GET',
				async:false,
				data:{
					
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var userInfo = res.content.userInfo;
						if (!userInfo.hasOwnProperty('healthType') || !userInfo['username'] || !userInfo['sex'] || !userInfo['telephone']) {
							window.location.replace('../login/pages/basicUserInfo.html?wechatId=' + order.wechatId  + '&operationType=12&route=my');
						} else {
							order.getOrderList(99);
						}
			
					}
				}
			})
		}
	}
	order.init();
})