define(['zepto', 'medtap'], function($, medtap) {
	var order = {
		wechatId: medtap.getRequest('wechatId'),
		init: function() {
			//order.getOrderList();
			order.bindEve();
			order.checkUser();
		},
		bindEve: function() {

		},
		getOrderList: function(type) {
			var requestUrl = '';
			switch (type) {
				case 'TWZX':
					requestUrl = 'https://gateway.medtap.cn/trade/imageConsultOrder/listOrder';
					break;
				case 'DHZX':
					requestUrl = 'https://gateway.medtap.cn/trade/consult/listOrder';
					break;
				case 'PMD':
					requestUrl = 'https://gateway.medtap.cn/trade/pmdConsultOrder/listOrder';
					break;
				default:
					break;
			}
			medtap.loading(1);
			medtap.submitAjax({
				url: requestUrl,
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					offset: 1,
					limit: 20,
					condition: {
						serviceStatusList: ['3', '10']
					}
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var list = res.content.list;
						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') {
									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].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].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].describe +
										'</span>' +
										'</p>' +
										'</div>' +
										'</div>';
								} else if (type == 'PMDTWZX') {
									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].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].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].patientName +
										'</span>' +
										'</p>' +
										'</div>' +
										'</div>';
								} else if (type == 'DHZX') {
									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].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].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://gateway.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);
						
					} else {

					}
				},
				error: function(res) {
					console.log(res.resultDesc);
				}
			})
		},
		checkUser: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://gateway.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://gateway.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 {
							var type = medtap.getRequest('orderType')
							order.getOrderList(type);
						}

					}
				}
			})
		}
	}
	order.init();
})