define(['zepto','medtap'],function($,medtap){
	var myEmr = {
		emrId:0,
		init:function(){
			myEmr.getMyEmr();
			myEmr.bindEve();
			myEmr.getWechatTicket();
		},
		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');
			})
		},
		getMyEmr:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/user/patientEMR/getPatientEMRInfo',
				type:'post',
				async:false,
				contentType: 'application/json',
				data:{
					
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var data = res.content;
						//填充用户信息
						var userInfo = data.userInfo;
						$('.user_name').text(userInfo.username)
						$('.user_profile').attr('src',userInfo.profile);
						$('.user_type').text('(' + (userInfo.hasOwnProperty('healthType') ? userInfo.healthType.value : '') + ')');
						$('.user_type').text('(' + 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.smokingFlg == true){
								var smokeStr = data.emr.smokingDailyNum + '支/天 ' + data.emr.smokingYear +'年';
								$('#somke').text(smokeStr);
							}else{
								$('#somke').text('无');
							} */
							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');
						}
					}
				}
			})
		},
		//获取微信票据
		getWechatTicket: function() {
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/wechat/lung/fetchWechatTicket',
				type: 'GET',
				async: false,
				data: {
					url: window.location.href
				},
				success: function(res) {
					var data = res.content;
					wx.config({
						debug: false,
						appId: data.appId, // 必填，公众号的唯一标识
						timestamp: data.timestamp, // 必填，生成签名的时间戳
						nonceStr: data.nonceStr, // 必填，生成签名的随机串
						signature: data.signature, // 必填，签名，见附录1
						jsApiList: [
							'previewImage'
						]
					})
				}
			})
		}, 
		//图片预览
		previewImage: function() {
			$('.img_warp').on('click', 'img', function() {
				var imgArray = [];
				var curImageSrc = $(this).attr('src');
				if (curImageSrc) {
					$('.img_warp img').each(function(index, el) {
						var itemSrc = $(this).attr('src');
						imgArray.push(itemSrc);
					});
					wx.previewImage({
						current: curImageSrc,
						urls: imgArray
					});
				}
			});
		}
	}
	myEmr.init();
	myEmr.previewImage();
})