define(['zepto', 'medtap'], function($, medtap) {
	var treatment = {
		pointList: '',
		localIds: [],
		serverId: "",
		imgList: [],
		treatmentList: '',
		treatmentHistory: '',
		emrId: medtap.getRequest('emrId'),
		init: function() {
			treatment.bindEve();
			treatment.getWechatTicket();
			treatment.getEmrInfo();
		},
		bindEve: function() {
			$('.next').on('click', function() {
				treatment.postData();
			});

			$('.pre').unbind().bind('click', function() {
				window.history.go(-1)
			})

			$('#hasPoint').on('click', function() {
				$('.select_btn_warp').show();
				$('.content_mod').show();

				$('.select_item').on('click', function() {
					$(this).children('img').attr('src', '../images/pop_select_active@2x.png');
					$(this).siblings('.select_item').children('img').attr('src', '../images/pop_select_default@2x.png');
					$(this).attr('data-select', 'yes');
					$(this).siblings('.select_item').attr('data-select', 'no');
					if ($('.yes').attr('data-select') == 'yes') {
						$('.select_btn_list .select_btn_item').removeAttr('disabled');
						$('.select_btn_list .select_btn_item').removeClass('dis');

					} else if ($('.no').attr('data-select') == 'yes') {
						$('.select_btn_list .select_btn_item').addClass('dis');
						$('.select_btn_list .select_btn_item').removeClass('selectOn');
						$('.select_btn_list .select_btn_item').attr('disabled', 'disabled');
					}
				})
				$('.select_btn_list .select_btn_item').unbind().bind('click', function() {

					if ($('.yes').attr('data-select') == 'yes') {
						$(this).toggleClass('selectOn');

					} else if ($('.no').attr('data-select') == 'yes') {

					}
				})

			});
			$('.content_mod').on('click', function() {
				$('.select_btn_warp').hide();
				$('.content_mod').hide();
			});
			$('.select_btn_confirm').on('click', function() {
				treatment.pointList = '';
				$('.select_btn_warp').hide();
				$('.content_mod').hide();
				var str = [];
				var str2 = [];
				if ($('.yes').attr('data-select') == 'yes') {
					$('.select_btn_list .select_btn_item').each(function(index, el) {
						var selectValue = $(this).attr('sendkey');
						var showText = $(this).text()
						if ($(this).hasClass('selectOn')) {
							str.push(selectValue);
							str2.push(showText);
							str2.join(',');
							treatment.pointList = str.join(',');
						} else {

						}
					});
				} else if ($('.no').attr('data-select') == 'yes') {

				}

				if (str.length == 0) {
					$('.hasPoint').text('无').css('color', '#000');
				} else {
					$('.hasPoint').text(str2).css('color', '#000');
				}

			});

			$('.treatWay_btn').unbind().bind('click', function() {
				$('#content').hide();
				$('.hasPre').hide();
				$('.content_2').show();
			})

			$('#treat_confirm').unbind().bind('click', function() {
				var str = '';
				if ($('.content_2 .has').hasClass('select')) {
					var str1 = []
					var str2 = [];
					var str3 = [];
					var str4 = [];
					$('.content_2 .common_btn .btn_icon').each(function(index, el) {
						var selectValue = $(this).siblings('.btn_name').attr('sendkey');
						var showText = $(this).siblings('.btn_name').text();
						if ($(this).hasClass('btnSelect')) {
							str1.push(selectValue);
							str1.join(',');
							str3.push(showText);
							str3.join(',');
						} else {
							//str1 = [];
						}
					});
					$('.content_2 .common_btn .more_way_btn').each(function(index, el) {
						var selectValue = $(this).attr('sendkey');
						var showText = $(this).text();
						if ($(this).hasClass('btnOn')) {
							str2.push(selectValue);
							str2.join(',');
							str4.push(showText);
							str4.join(',');
						} else {
							//str2 = []
						}
					});
					treatment.treatmentList = str1 + ',' + str2;
					console.log(treatment.treatmentList)
					str = str3 + ',' + str4;
				} else {
					treatment.treatmentList = '';
				}
				/* $('.treating').text(str.slice(0,str.length-1)).css('color','#333') */
				if (str == '') {
					$('.treating').text('无').css('color', '#333');
				} else {
					$('.treating').text(str.slice(0, str.length - 1)).css('color', '#333')
				}
				$('.content_2').hide();
				$('#content').show();
				$('.hasPre').show();
			});

			$('.add_btn').unbind().bind('click', function() {
				$('#content').hide();
				$('.hasPre').hide();
				$('.content_3').show();
			});
			$('#treat_confirm_history').unbind().bind('click', function() {
				var str = '';
				if ($('.content_3 .has').hasClass('select')) {
					var str1 = []
					var str2 = [];
					var str3 = [];
					var str4 = [];
					$('.content_3 .common_btn .btn_icon').each(function(index, el) {
						var selectValue = $(this).siblings('.btn_name').attr('sendkey');
						var showText = $(this).siblings('.btn_name').text();
						if ($(this).hasClass('btnSelect')) {
							str1.push(selectValue);
							str1.join(',');
							str3.push(showText);
							str3.join(',');
						} else {
							//str1 = [];
						}
					});
					$('.content_3 .common_btn .more_way_btn').each(function(index, el) {
						var selectValue = $(this).attr('sendkey');
						var showText = $(this).text();
						if ($(this).hasClass('btnOn')) {
							str2.push(selectValue);
							str2.join(',');
							str4.push(showText);
							str4.join(',');
						} else {
							//str2 = []
						}
					});
					treatment.treatmentHistory = str1 + ',' + str2;
					str = str3 + ',' + str4;
				} else {
					treatment.treatmentHistory = '';
				}
				if (str == '') {
					$('.treated').text('无')
				} else {
					$('.treated').text(str.slice(0, str.length - 1));
				}
				$('.content_3').hide();
				$('#content').show();
				$('.hasPre').show();
			});

			//content_2事件定义
			$('.content_2 .select_item').unbind().bind('click', function() {
				$(this).addClass('select');
				$(this).siblings('.select_item').removeClass('select');

				if ($(this).hasClass('has')) {
					$('.content_2 .common_btn').show();
				} else if ($(this).hasClass('no')) {
					$('.content_2 .common_btn').hide();
				}
			});

			$('.content_2 .btn_icon').unbind().bind('click', function() {
				$(this).toggleClass('btnSelect');
				if ($(this).hasClass('btnSelect')) {
					$(this).attr('select', 'yes')
				} else {
					$(this).attr('select', 'no')
				}
			});

			$('.content_2 .more_way_btn').unbind().bind('click', function() {
				$(this).toggleClass('btnOn');
				if ($(this).hasClass('btnOn')) {
					$(this).attr('select', 'yes')
				} else {
					$(this).attr('select', 'no')
				}
			});

			//content_3事件定义
			$('.content_3 .select_item').unbind().bind('click', function() {
				$(this).addClass('select');
				$(this).siblings('.select_item').removeClass('select');

				if ($(this).hasClass('has')) {
					$('.content_3 .common_btn').show();
				} else if ($(this).hasClass('no')) {
					$('.content_3 .common_btn').hide();
				}
			});

			$('.content_3 .btn_icon').unbind().bind('click', function() {
				$(this).toggleClass('btnSelect');
				if ($(this).hasClass('btnSelect')) {
					$(this).attr('select', 'yes')
				} else {
					$(this).attr('select', 'no')
				}
			});

			$('.content_3 .more_way_btn').unbind().bind('click', function() {
				$(this).toggleClass('btnOn');
				if ($(this).hasClass('btnOn')) {
					$(this).attr('select', 'yes')
				} else {
					$(this).attr('select', 'no')
				}
			});
		},
		//获取微信票据
		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: [
							'chooseImage',
							'previewImage',
							'uploadImage'
						]
					})
				}
			})
		}, //选择图片
		choseImage: function() {
			$('#upload').on('click', function() {
				var html = '';
				wx.ready(function() {
					var syncUpload = function(localIds) {
						localId = localIds[0];
						localIds = localIds.length > 1 ? localIds.slice(1) : localIds[0];
						wx.uploadImage({
							localId: localId,
							isShowProgressTips: 1,
							success: function(res) {
								var serverId = res.serverId; // 返回图片的服务器端ID
								//其他对serverId做处理的代码
								treatment.upLoadImg(serverId, localId);
								if (localIds.length > 0) {
									syncUpload(localIds);
								}
							}
						});
					};
					wx.chooseImage({
						count: 9,
						sizeType: ['original', 'compressed'],
						sourceType: ['album', 'camera'],
						success: function(res) {
							treatment.localIds = res.localIds;
							//所有需要上传到图片预览
							syncUpload(treatment.localIds);
						}
					})
				})
			})
		},
		//图片预览
		previewImage: function() {
			$('#ImgUp').on('click', '.image_files_img', function() {
				var imgArray = [];
				var curImageSrc = $(this).attr('src');
				if (curImageSrc) {
					$('#ImgUp .image_files_img').each(function(index, el) {
						var itemSrc = $(this).attr('src');
						imgArray.push(itemSrc);
					});
					wx.previewImage({
						current: curImageSrc,
						urls: imgArray
					});
				}
			});
			$('#ImgUp').on('click', '.image_del', function() {
				if (confirm('确定要删除图片吗？')) {
					$(this).parent().remove();
				}
			});
		},
		//上传图片
		upLoadImg: function(serverId, localId) {
			$.ajax({
				url: 'https://testdevgw.medtap.cn/common/uploadWechatFile',
				type: 'POST',
				async: false,
				headers: {
					'apptype': 'wechat_lung',
					'user-os': 'wechat'
				},
				data: {
					bizType: "3",
					folder: 'patientInfo',
					mediaId: serverId,
					ext: 'jpg'
				},
				success: function(data) {
					if (data.success == true) {
						var res = data.content;
						var imgKey = res.ossKey;
						treatment.imgList.push(imgKey);
						var html = '<div class="image_files_item">' +
							'<img src="' + localId + '" class="image_files_img" imgkey="' + imgKey + '">' +
							'<img src="../images/post_delete_btn@2x.png" class="image_del" >' +
							'</div>';
						$('#ImgUp').append(html);
					}
				}

			})
		},
		postData: function() {
			var urls = [],
				picUrl = "";
			[].forEach.call($('#ImgUp .image_files_img'), function(item) {
				urls.push($(item).attr('imgkey'));
			});
			var picUrl = urls.join(',');
			var pointList = ''
			if ($('.hasPoint').text() == '请选择' || $('.hasPoint').text() == '') {
				pointList = '';
			} else {
				pointList = $('.hasPoint').text();
			}
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/patientEMR/createPatientEMR',
				type: 'post',
				contentType: 'application/json',
				data: {
					targetedMutations: treatment.pointList,
					acceptingCure: treatment.treatmentList, //当前正在接受的治疗
					acceptedCure: treatment.treatmentHistory, //既往接受过的治疗
					curePic: picUrl, //选择的图片
					infoStep: 2,
					emrId: treatment.emrId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						setTimeout(function() {
							medtap.toast({
								message: '保存成功',
								time: 1500
							});
							setTimeout(function() {
								window.location.replace('myEMR.html'); //跳转至医生主页
							}, 1500)
						}, 2000)
					}
				}
			})
		},
		getEmrInfo: 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);
					//var emrInfo = res.content.emr;
					if (res.success == true) {
						var emrInfo = res.content.emr;
						//$('.hasPoint').text()
						if (emrInfo.hasOwnProperty('targetedMutationsItem')) {
							var str = [];
							var str2 = [];
							for (var i = 0; i < emrInfo.targetedMutationsItem.length; i++) {
								str.push(emrInfo.targetedMutationsItem[i].value);
								$('.select_btn_list .select_btn_item').each(function(index, el) {
									var selectValue = $(this).attr('sendkey');
									if(emrInfo.targetedMutationsItem[i].child == selectValue){
										$(this).addClass('selectOn');
										str2.push(emrInfo.targetedMutationsItem[i].child);
									}
								});
							}
							str.join(',');
							treatment.pointList = str2.join(',');
							console.log(treatment.pointList)
							$('.hasPoint').text(str).css('color','#000');
						}else{
							$('.hasPoint').text('无').css('color','#000');
							treatment.pointList = '';
						}
						if(emrInfo.hasOwnProperty('acceptingCureItem')){
							var str = [];
							var str2 = [];
							var str3 = [];
							for(var i = 0; i < emrInfo.acceptingCureItem.length;i++){
								str.push(emrInfo.acceptingCureItem[i].value);
								$('.content_2 .common_btn .btn_icon').each(function(index, el) {
									var selectValue = $(this).siblings('.btn_name').attr('sendkey');
									if(emrInfo.acceptingCureItem[i].child == selectValue){
										str2.push(emrInfo.acceptingCureItem[i].child);
										$(this).addClass('btnSelect');
									}
								});
								$('.content_2 .common_btn .more_way_btn').each(function(index, el) {
									var selectValue = $(this).attr('sendkey');
									if(emrInfo.acceptingCureItem[i].child == selectValue){
										str3.push(emrInfo.acceptingCureItem[i].child);
										$(this).addClass('btnOn')
									}
								});
							}
							str.join(',');
							treatment.treatmentList = str2 + ',' + str3;
							$('.treating').text(str).css('color','#000');
						}else{
							$('.treating').text('无').css('color','#000');
							treatment.treatmentList = ''
						}
						if(emrInfo.hasOwnProperty('acceptedCureItem')){
							var str = [];
							var str2 = [];
							var str3 = [];
							for(var i = 0; i < emrInfo.acceptedCureItem.length;i++){
								str.push(emrInfo.acceptedCureItem[i].value);
								$('.content_3 .common_btn .btn_icon').each(function(index, el) {
									var selectValue = $(this).siblings('.btn_name').attr('sendkey');
									if(emrInfo.acceptedCureItem[i].child == selectValue){
										str2.push(emrInfo.acceptedCureItem[i].child);
										$(this).addClass('btnSelect');
									}
								});
								$('.content_3 .common_btn .more_way_btn').each(function(index, el) {
									var selectValue = $(this).attr('sendkey');
									if(emrInfo.acceptedCureItem[i].child == selectValue){
										str3.push(emrInfo.acceptedCureItem[i].child);
										$(this).addClass('btnOn')
									}
								});
							}
							str.join(',');
							treatment.treatmentHistory = str2 + ',' + str3;
							console.log(treatment.treatmentHistory);
							$('.treated').text(str).css('color','#000');
						}else{
							$('.treated').text('无').css('color','#000');
							treatment.treatmentHistory = '';
						}
						if(emrInfo.curePicList == ''){
								
						}else{
							var html = '';
							for(var i = 0;i < emrInfo.curePicList.length;i++){
								html += '<div class="image_files_item">' +
											'<img src="' + emrInfo.curePicList[i] + '" class="image_files_img" imgkey="'+ emrInfo.curePicList[i] +'">' +
											'<img src="../images/post_delete_btn@2x.png" class="image_del" >' +
										'</div>';
							}
							$('#ImgUp').append(html);
						}
					}
				}
			})
		}
	}
	treatment.init();
	treatment.choseImage();
	treatment.previewImage();
})
