define(['zepto','medtap'],function($,medtap){
	var myEmr = {
		userId:medtap.getRequest('userId'),
		init:function(){
			myEmr.getMyEmr();
			myEmr.bindEve();
		},
		bindEve:function(){
			/* $('.change_emr').unbind().bind('click',function(){
				medtap.pushWindow('createPatientInfo.html?id=' + myEmr.emrId);
			});
			$('.add_new_btn').unbind().bind('click',function(){
				medtap.pushWindow('createPatientInfo.html');
			}) */
			$('.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 = JSON.stringify(data.emr)
						if(emrInfo != {}){
							//有病历信息
							$('.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');
						}
					}
				}
			})
		}
	}
	myEmr.init();
})