define(['zepto', 'medtap'], function($, medtap) {
	var treatment = {
		pointList: '',
		localIds: [],
		serverId: "",
		imgList: [],
		treatmentList:'',
		treatmentHistory:'',
		emrId:medtap.getRequest('emrId'),
		toLink:medtap.getRequest('toLink'),
		wechatId:medtap.getRequest('wechatId'),
		init: function() {
			treatment.bindEve();
			treatment.getWechatTicket();
		},
		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;
					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.slice(0,treatment.treatmentList.length-1), //当前正在接受的治疗
					acceptedCure:treatment.treatmentHistory.slice(0,treatment.treatmentHistory.length-1), //既往接受过的治疗
					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('doctorHomePage.html?doctorId=' + medtap.getRequest('doctorId') + '&operationType=' + medtap.getRequest('operationType')); //跳转至医生主页
								switch (treatment.toLink){
									case 'MYDOCTOR':
										window.location.replace('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_myHome/pages/myDoctor.html?wechatId=' + treatment.wechatId)
										break;
									case 'MYEMR':
										window.location.replace('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_myHome/pages/myEMR.html?wechatId=' + treatment.wechatId);
										break;
									case 'MYREMIND':
										window.location.replace('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_myHome/pages/myRemind.html?wechatId=' + treatment.wechatId);
										break;
									case 'MYCONSULT':
										window.location.replace('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_myHome/pages/myConsult.html?wechatId=' + treatment.wechatId);
									default:
										break;
								}
							}, 1500)
						}, 2000)
					}
				}
			})
		}
	}
	treatment.init();
	treatment.choseImage();
	treatment.previewImage();
})
