define(['zepto', 'medtap'], function($, medtap) {
	var myEmr = {
		userId: medtap.getRequest('userId'),
		init: function() {
			myEmr.getMyEmr();
			myEmr.bindEve();
			myEmr.getUserAttention();
		},
		bindEve: function() {
			//原生路由跳转
			//发消息
			$('.sendMsg').unbind().bind('click', function() {
				//window.location.href = 'medtapdoc://medtap.cn/chat/touser?userId=' + myEmr.userId;
				var status = $('.attention').attr('data-attention');
				if(status == 'yes'){
					window.location.href = 'medtapdoc://medtap.cn/chat/touser?userId=' + myEmr.userId;
				}else{
					medtap.winPop('相互关注后才可以发消息')
				}
			});
			//反馈互动
			$('.feedback').unbind().bind('click', function() {
				window.location.href = 'medtapdoc://medtap.cn/user/feedback?userId=' + myEmr.userId;
			})
			//备注
			$('.remark').unbind().bind('click', function() {
				window.location.href = 'medtapdoc://medtap.cn/user/remark?userId=' + myEmr.userId;
			})
			//分组
			$('.grouping').unbind().bind('click', function() {
				window.location.href = 'medtapdoc://medtap.cn/user/group?userId=' + myEmr.userId;
			});

			//关注
			$('.attention').unbind().bind('click', function() {
				var status = $(this).attr('data-attention');
				if (status == 'yes') {
					$('#showDialog').show();
					$('.dialog-btn-confirm').unbind().bind('click', function(e) {
						$('#showDialog').hide();
						myEmr.cancelAttentionUser();
					})
					$('.dialog-btn-cancel').unbind().bind('click', function() {
						$('#showDialog').hide();
					})
				} else if (status == 'no') {
					window.location.href = 'medtapdoc://medtap.cn/user/follow?userId=' + myEmr.userId;
				}
			});

			$('.img_warp img').unbind().bind('click', function() {
				var imgObject = {};
				var imgSelect = $(this).index();
				var imgIndex = 0;
				var urls = [],
					picUrl = "";
				[].forEach.call($('.img_warp img'),function(item,index){
					if($(item).index() == imgSelect){
						imgIndex = index;
					}
					urls.push($(item).attr('src'));
				})
				imgObject.imgList = urls;
				imgObject.imgIndex = imgIndex;
				try {
					if (!WebAPI) {
				
					}
					try {
						WebAPI.showImages(imgObject);
					} catch (e) {
				
					}
				} catch (e) {
				
				}
			})
		},
		getMyEmr: function() {
			medtap.jzz(1);
			$.ajax({
				url: 'https://testdevgw.medtap.cn/user/patientEMR/getPatientEMRInfo',
				type: 'post',
				async: false,
				contentType: 'application/json',
				headers: medtap.getHeaders(),
				data: JSON.stringify({
					userId: myEmr.userId
				}),
				success: function(res) {
					medtap.jzz(0);
					if (res.success == true) {
						var data = res.content;
						//填充用户信息
						var userInfo = data.userInfo;
						$('.user_profile').attr('src', userInfo.profile);
						$('.user_name').text(userInfo.username);
						$('.user_type').text('(' + (userInfo.hasOwnProperty('healthType') ? userInfo.healthType.value : '') + ')');
						$('.user_sex').text(userInfo.sex == 'M' ? '男' : '女');
						var emrInfo = data.emr
						if ($.isEmptyObject(emrInfo) == false) {
							//有病历信息
							$('.user_patient_info_detail').show();
							$('.add_new_patient').hide();
							$('body').css('background', '#F6F6FC');
							myEmr.emrId = data.emr.emrId;
							$('.patient_name').text(data.emr.patientName);
							$('.patient_sex').text(data.emr.sex == 'M' ? '男' : '女');
							$('.patient_age').text(data.emr.age);
							if (data.emr.hasOwnProperty('height')) {
								$('.patient_height').text(data.emr.height);
							} else {
								$('.patient_height').text('未知').css('color', '#999999');
							}
							if (data.emr.hasOwnProperty('weight')) {
								$('.patient_weight').text(data.emr.weight);
							} else {
								$('.patient_weight').text('未知').css('color', '#999999');
							}
							$('#diseases').text(data.emr.diseaseDiagnosisName);
							$('#pathologyType').text(data.emr.pathologyTypeName);
							if (data.emr.hasOwnProperty('tumorStageItem')) {
								$('#tumorStage').text(data.emr.tumorStageItem.value);
							} else {
								$('#tumorStage').text('未知').css('color', '#999999');
							}
							if (data.emr.hasOwnProperty('tnmStage') && data.emr.tnmStage !== '') {
								$('#tnmStage').text(data.emr.tnmStage);
							} else {
								$('#tnmStage').text('未知').css('color', '#999999');
							}
							if (data.emr.hasOwnProperty('transferNidusItem')) {
								$('#transferNidus').text(data.emr.transferNidusItem.value);
							} else {
								$('#transferNidus').text('未知').css('color', '#999999');
							}
							if (data.emr.hasOwnProperty('ecogGradeItem')) {
								$('#ecogGrade').text(data.emr.ecogGradeItem.value.substring(0, 2));
							} else {
								$('#ecogGrade').text('未知').css('color', '#999999');
							}
							if(data.emr.hasOwnProperty('smokingFlg')){
								if(data.emr.smokingFlg == true){
									var smokeStr = data.emr.smokingDailyNum + '支/天 ' + data.emr.smokingYear +'年';
									$('#somke').text(smokeStr);
								}else if(data.emr.smokingFlg == false){
									$('#somke').text('无');
								}
							}else{
								$('#somke').text('未知').css('color','#999999');
							}
							if (data.emr.hasOwnProperty('patientHospitalIdNum') && data.emr.patientHospitalIdNum !== '') {
								$('#patientHospitalIdNum').text(data.emr.patientHospitalIdNum);
							} else {
								$('#patientHospitalIdNum').text('未知').css('color', '#999');
							}
							if (data.emr.hasOwnProperty('areaItem')) {
								$('#area').text(data.emr.areaItem.fullName);
							} else {
								$('#area').text('未知').css('color', '#999');
							}
							if (data.emr.hasOwnProperty('targetedMutationsItem')) {
								var str = [];
								for (var i = 0; i < data.emr.targetedMutationsItem.length; i++) {
									str.push(data.emr.targetedMutationsItem[i].value);
								}
								str.join(',');
								$('#targetedMutations').text(str);
							} else {
								$('#targetedMutations').text('未知').css('color', '#999');
							}
							if(data.emr.hasOwnProperty('acceptingCureItem') && data.emr.acceptingCure !=''){
								var str = [];
								for(var i = 0; i < data.emr.acceptingCureItem.length;i++){
									str.push(data.emr.acceptingCureItem[i].value);
								}
								str.join(',');
								$('#acceptingCureItem').text(str);
							}else if(data.emr.acceptingCure == ''){
								$('#acceptingCureItem').text('无');
							}else{
								$('#acceptingCureItem').text('未知').css('color','#999');
							}
							if(data.emr.hasOwnProperty('acceptedCureItem') && data.emr.acceptedCure != ''){
								var str = [];
								for(var i = 0; i < data.emr.acceptedCureItem.length;i++){
									str.push(data.emr.acceptedCureItem[i].value);
								}
								str.join(',');
								$('#acceptedCureItem').text(str);
							}else if(data.emr.acceptedCure == ''){
								$('#acceptedCureItem').text('无')
							}else{
								$('#acceptedCureItem').text('未知').css('color','#999');
							}
							if (data.emr.curePicList == '') {
								var str = '<img src="images/post_no picture@2x.png" >';
								$('.img_warp').html(str);
							} else {
								var html = '';
								for (var i = 0; i < data.emr.curePicList.length; i++) {
									html += ' <img src="' + data.emr.curePicList[i] + '" >'
								}
								$('.img_warp').html(html);
							}
						} else {
							//无病历信息
							$('.user_patient_info_detail').hide();
							$('.add_new_patient').show();
							$('body').css('background', '#fff');
						}
					}
				}
			})
		},
		//获取医生关注用户状态
		getUserAttention: function() {
			medtap.jzz(1);
			$.ajax({
				url: 'https://testdevgw.medtap.cn/doctor/fans/getUserDetail',
				type: 'get',
				headers: medtap.getHeaders(),
				data: {
					userId: myEmr.userId
				},
				success: function(res) {
					medtap.jzz(0);
					if (res.success == true) {
						var friend = res.content.friend;
						if (friend.doctorUserFriendType == 'FOLLOW_TYPE_FRIEND') {
							$('.attention img').attr('src', 'images/case_focus_butten_png@2x.png');
							$('.attention_desc').text('相互关注');
							$('.attention').attr('data-attention', 'yes');
						} else if (friend.doctorUserFriendType == 'FOLLOW_TYPE_FOLLOWER') {
							$('.attention img').attr('src', 'images/case_focus no_butten_png@2x.png');
							$('.attention_desc').text('加关注');
							$('.attention').attr('data-attention', 'no');
						}
					}
				}
			})
		},
		cancelAttentionUser: function() {
			medtap.jzz(1);
			$.ajax({
				url: 'https://testdevgw.medtap.cn/doctor/fans/attentUser',
				type: 'post',
				contentType: 'application/json',
				headers: medtap.getHeaders(),
				data: JSON.stringify({
					userId: myEmr.userId,
					follow: false
				}),
				success: function(res) {
					medtap.jzz(0);
					if (res.success == true) {
						medtap.winPop('取消关注成功');
						setTimeout(function() {
							window.location.reload();
						}, 2000)
					}
				}
			})
		}
	}
	myEmr.init();
})
