define(['zepto', 'medtap'], function($, medtap) {
	var doctor = {
		doctorId: medtap.getRequest('doctorId'),
		wechatId: medtap.getRequest('wechatId'),
		attention: false,
		doctorName: '',
		pmdOrderSn: '',
		needAttention:medtap.getRequest('needAttention'),
		init: function() {
			if(!doctor.needAttention){
				
			}else{
				doctor.attentionDoctor();
			}
			doctor.getPmdOrderStatus();
			doctor.getDoctorDetail();
			doctor.getAttention();
			doctor.bindEve();
		},
		bindEve: function() {
			$('.show_more_btn').unbind().bind('click', function() {
				$('.doctor_info_open').show();
				$('.doctor_info_close').hide();
				$(this).hide();
				$('.show_close_btn').show();
			});
			$('.show_close_btn').unbind().bind('click', function() {
				$('.doctor_info_open').hide();
				$('.doctor_info_close').show();
				$(this).hide();
				$('.show_more_btn').show();
			});

			$('.attention').unbind().bind('click', function() {
				if ($(this).hasClass('follow')) {
					doctor.cancleAttentionDoctor();
				} else {
					doctor.attentionDoctor();
				}
			});
			//发消息
			$('.sendMsg').unbind().bind('click', function() {
				//medtap.pushWindow('../../wechat_lung_chat/pages/freeChat.html?doctorId=' + doctor.doctorId);
				if (doctor.attention == true) {
					medtap.pushWindow('../../wechat_lung_chat/pages/freeChat.html?doctorId=' + doctor.doctorId + '&wechatId=' + medtap.getRequest('wechatId'));
				} else {
					medtap.toast({
						message: '请先关注医生'
					})
				}
			});

			//私人医生服务购买
			$('.go_pmd').unbind().bind('click', function() {
				if ($(this).hasClass('cardDis')) {

				} else {
					/* if (doctor.pmdOrderSn != '') {
						medtap.pushWindow('../../wechat_lung_prvivateDoctor/pages/pmdDetail.html?doctorId=' + doctor.doctorId +
							'&id=' + doctor.pmdOrderSn + '&wechatId=' + medtap.getRequest('wechatId'));
					} else {
						medtap.pushWindow('../../wechat_lung_prvivateDoctor/index.html?doctorId=' + doctor.doctorId + '&wechatId=' +
							medtap.getRequest('wechatId'));
					} */
					var status = doctor.checkUserPatient();
					if(status == true){
						if (doctor.pmdOrderSn != '') {
							medtap.pushWindow('../../wechat_lung_prvivateDoctor/pages/pmdDetail.html?doctorId=' + doctor.doctorId +
								'&id=' + doctor.pmdOrderSn + '&wechatId=' + medtap.getRequest('wechatId'));
						} else {
							medtap.pushWindow('../../wechat_lung_prvivateDoctor/index.html?doctorId=' + doctor.doctorId + '&wechatId=' +
								medtap.getRequest('wechatId'));
						} 
					}else{
						$('#showDialog').show();
						$('.dialog-btn-cancel').unbind().bind('click',function(){
							$('#showDialog').hide();
						});
						$('.dialog-btn-confirm').unbind().bind('click',function(){
							$('#showDialog').hide();
							medtap.pushWindow('createPatientInfo.html?doctorId='  + doctor.doctorId + '&operationType=' +medtap.getRequest('operationType'))
						})
					}
				}
			})

			//电话咨询服务购买
			$('.consult_btn').unbind().bind('click', function() {
				if ($(this).hasClass('cardDis')) {

				} else {
					medtap.pushWindow('../../wechat_lung_consult/rukou.html?doctorId=' + doctor.doctorId + '&pmdOrderSn=' +
						doctor.pmdOrderSn + '&doctorname=' + doctor.doctorName + '&wechatId=' + medtap.getRequest('wechatId'));
				}
			});

			//图文咨询服务购买
			$('.img_consult_btn').unbind().bind('click', function() {
				if ($(this).hasClass('cardDis')) {

				} else {
					var status = doctor.checkUserPatient();
					//alert(status)
					if(status == true){
						medtap.pushWindow('../../wechat_lung_imgConsult/index.html?doctorId=' + doctor.doctorId + '&pmdOrderSn=' +
							doctor.pmdOrderSn + '&wechatId=' + medtap.getRequest('wechatId'));
					}else{
						$('#showDialog').show();
						$('.dialog-btn-cancel').unbind().bind('click',function(){
							$('#showDialog').hide();
						});
						$('.dialog-btn-confirm').unbind().bind('click',function(){
							$('#showDialog').hide();
							medtap.pushWindow('createPatientInfo.html?doctorId='  + doctor.doctorId + '&operationType=' +medtap.getRequest('operationType'))
						})
						
					}
					/* medtap.pushWindow('../../wechat_lung_imgConsult/index.html?doctorId=' + doctor.doctorId + '&pmdOrderSn=' +
						doctor.pmdOrderSn + '&wechatId=' + medtap.getRequest('wechatId')); */
				}
			})

		},
		getDoctorDetail: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/doctor/getDoctorDetail',
				type: 'get',
				async: false,
				data: {
					doctorId: doctor.doctorId
				},
				success: function(res) {
					medtap.loading(0);
					var detail = res.content.doctorItem;
					$('.doctor_profile').attr('src', detail.profile);
					$('.doctor_name').text(detail.doctorName);
					doctor.doctorName = detail.doctorName;
					document.title = detail.doctorName;
					$('.doctor_professional').text(detail.hasOwnProperty('technologyProfessional') ? detail.technologyProfessional
						.value : '');
					$('.doctor_hospital').text(detail.hospitalName + ' ' + detail.departmentName);
					$('.doctor_goodAt').text(detail.goodAt);
					var diseaseList = [];
					if (detail.hasOwnProperty('treatDisease')) {
						for (var i = 0; i < detail.treatDisease.length; i++) {
							diseaseList.push(detail.treatDisease[i].diseaseName);
							diseaseList.join(',');
						}
						$('.doctor_diseases').text(diseaseList);
						$('.more_disease').text(diseaseList);
					} else {
						$('.doctor_diseases').text('');
						$('.more_disease').text('');
					}

					//$('.doctor_diseases').text(diseaseList);
					$('.doctor_introduce').text(detail.personageDescription);

					//更多内容
					$('.doctor_goodAt_info').text(detail.goodAt);
					//$('.more_disease').text(diseaseList);
					$('.more_introduce').text(detail.personageDescription);

					//服务价格
					//电话咨询
					if (detail.openConsult == false) {
						$('.consult_price').html('未开通').css('color', '#999');
						$('.consult_btn').addClass('cardDis');
					} else {

						$('.consult_price').html(detail.consultPrice);
						$('.consult_btn').removeClass('cardDis');
					}
					//图文咨询
					if (detail.isOpenImageConsult == false) {
						$('.img_consult_price').html('未开通').css('color', '#999');
						$('.img_consult_btn').addClass('cardDis');
					} else {
						var str = '<span class="txt_gray">/次</span>';
						$('.img_consult_price').html(detail.imageConsultPrice + str);
						$('.img_consult_btn').removeClass('cardDis');
					}
					//私人医生
					if (detail.isOpenPmd == false) {
						$('.pmd_price').html('未开通').css('color', '#999');
						$('.go_pmd').hide();
					} else {
						$('.pmd_price').html(detail.pmdPrice);
					}

				}
			})
		},
		//关注医生
		attentionDoctor: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/friend/userAttentDoctor',
				type: 'post',
				contentType: "application/json",
				async: false,
				data: {
					doctorId: parseInt(doctor.doctorId),
					operationType: medtap.getRequest('operationType')
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var status = res.content.doctorUserFriendType;
						if (status == 'FOLLOW_TYPE_FOLLOWING') {
							$('.attention').addClass('follow').text('已关注');
							doctor.attention = true;
						} else if (status == 'FOLLOW_TYPE_UNFOLLOW') {
							doctor.attention = false;
							$('.attention').removeClass('follow').text('加关注');
						} else if (status == 'FOLLOW_TYPE_FRIEND') {
							doctor.attention = true;
						}
					}
				}
			})
		},
		//取消关注医生
		cancleAttentionDoctor: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/friend/userCancelAttentDoctor',
				type: 'post',
				contentType: "application/json",
				async: false,
				data: {
					doctorId: parseInt(doctor.doctorId)
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var status = res.content.doctorUserFriendType;
						if (status == 'FOLLOW_TYPE_FOLLOWING') {
							$('.attention').addClass('follow').text('已关注');
						} else if (status == 'FOLLOW_TYPE_UNFOLLOW') {
							$('.attention').removeClass('follow').text('加关注');
						}
					}
				}
			})
		},
		//获取关注状态
		getAttention: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/friend/getAttention',
				type: "get",
				async: false,
				data: {
					doctorId: parseInt(doctor.doctorId)
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var status = res.content.doctorUserFriendType;
						if (status == 'FOLLOW_TYPE_FOLLOWING' || status == 'FOLLOW_TYPE_FRIEND') {
							$('.attention').addClass('follow').text('已关注');
							doctor.attention = true;
						} else if (status == 'FOLLOW_TYPE_UNFOLLOW') {
							$('.attention').removeClass('follow').text('加关注');
							doctor.attention = false;
						}
					}
				}
			})
		},
		getPmdOrderStatus: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/trade/pmdConsultOrder/listValidOrder',
				type: 'post',
				contentType: "application/json",
				async: false,
				data: {
					doctorId: parseInt(doctor.doctorId)
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var pmdOrderList = res.content.list;

						if (pmdOrderList.length == 0) {
							doctor.pmdOrderSn = '';
							$('.go_pmd').html('立即开通');
						} else {
							$('.go_pmd').html('已开通');
							for (var i = 0; i < pmdOrderList.length; i++) {
								if (pmdOrderList[i].doctorId == parseInt(doctor.doctorId)) {
									doctor.pmdOrderSn = pmdOrderList[i].orderSn;
								}
							}
						}
					}
				}
			})
		},
		checkUserPatient:function(){
			var flag = false;
			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 patientInfo = res.content.list;
						if(patientInfo.length == 0){
							//未完善患者信息
							flag = false;
						}else{
							//跳转至医生主页
							flag = true;
						}
					}
				}
			})
			return flag;
			alert(flag)
		}
	}
	doctor.init();
})
