define(['zepto','medtap'],function($,medtap){
	var pmd = {
		doctorId:medtap.getRequest('doctorId'),
		wxLungToken:localStorage.wxLungToken,
		emrId:'',
		patientName:'',
		init:function(){
			pmd.getDoctorDetail();
			pmd.bindEve();
			if(!medtap.getRequest('emrId')){
				pmd.getPatientList();
			}else{
				pmd.emrId = medtap.getRequest('emrId');
			}
			if(!medtap.getRequest('patientName')){
				
			}else{
				pmd.patientName = medtap.getRequest('patientName');
				$('.service_patient').html(medtap.getRequest('patientName'))
			}
		},
		bindEve:function(){
			$('.btn_icon').unbind().bind('click',function(){
				$(this).toggleClass('btnSelect');
				if ($(this).hasClass('btnSelect')) {
					$(this).attr('select', 'yes')
				} else {
					$(this).attr('select', 'no')
				}
			});
			$('.pmd_agreement').unbind().bind('click',function(){
				medtap.pushWindow('https://review-formal.iplusmed.com/App/dev/FF499211739FD801DOCTOR/AppHtml/DoctorServiceLicense.html')
			})
			
			$('.submit_order').unbind().bind('click',function(){
				var flag = $('.btn_icon').attr('select');
				if(flag == 'yes'){
					pmd.createPmdOrder();
				}else if(flag == 'no'){
					medtap.toast({
						message:'<p style="text-align:center">请阅读并同意<br/>《私人医生咨询服务订单协议》</p>',
						time:1500
					})
				}
			})
		},
		getDoctorDetail:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/doctor/getDoctorDetail',
				type:'get',
				async:false,
				data:{
					doctorId:pmd.doctorId
				},
				success:function(res){
					medtap.loading(0);
					var doctorInfo = res.content.doctorItem;
					var str = ''
					$('.doctor_name').html(doctorInfo.doctorName);
					$('.doctor_profile').attr('src',doctorInfo.profile);
					$('.doctor_title').html(doctorInfo.hasOwnProperty('technologyProfessional') ? doctorInfo.technologyProfessional.value : '');
					str = '<span class="doctor_department">'+doctorInfo.departmentName+'</span>';
					$('.doctor_hospital').html(doctorInfo.hasOwnProperty('hospital') ? doctorInfo.hospital.hospitalName + str : '');
					$('.service_price').html(doctorInfo.hasOwnProperty('pmdPrice') ? doctorInfo.pmdPrice : '');
					$('.footer_pmd_price').html(doctorInfo.hasOwnProperty('pmdPrice') ? doctorInfo.pmdPrice : '');
				}
			})
		},
		confirmOrder:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/trade/pmdConsultOrder/confirmOrder',
				type:'post',
				async:false,
				contentType: 'application/json',
				data:{
					emrId:pmd.emrId,
					doctorId:pmd.doctorId
				},
				success:function(res){
					medtap.loading(0);
				}
			})
		},
		getPatientList:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/user/patientEMR/listPatientEMR',
				type:'post',
				async:false,
				contentType: 'application/json',
				data:{
					pageNum:1,
					pageSize:10,
					condition:{
						
					}
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var patientList = res.content.list;
						for(var i = 0;i < patientList.length;i++){
							if(patientList[i].defaultFlg == true){
								$('.service_patient').html(patientList[i].patientName);
								pmd.emrId = patientList[i].emrId;
							}
						}
					}
				}
			})
		},
		createPmdOrder:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/trade/pmdConsultOrder/submitOrder',
				type:'post',
				async:false,
				contentType: 'application/json',
				data:{
					emrId:pmd.emrId,
					doctorId:pmd.doctorId
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var orderDetail = res.content.order;
						var orderSn = orderDetail.orderSn;
						medtap.pushWindow('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_wxPay/wxPay.html?orderType=pmd&orderId=' + orderSn + '&wxLungToken=' + pmd.wxLungToken + '&wechatId=' + medtap.getRequest('wechatId'));
					}else{
						setTimeout(function(){
							medtap.toast({
								message:'<p>您与当前医生有进行中的订单<br>请处理后再进行购买</p>',
								time:2300
							})
						},1800)
					}
				}
			})
		}
	}
	pmd.init();
})