define(['zepto', 'medtap', 'mui', 'mui.picker'], function($, medtap, mui) { 
  var generalPage = {
    wechatId:medtap.getRequest('wechatId'),
    // 问卷id
		followUpId: medtap.getRequest('followUpId'),
		// 修改人员id
    adminId: medtap.getRequest('adminId')||'',
    // 获取checks数据判断下面跳转的页面
		checks: medtap.getRequest('checks'),
		// 获取checks数据判断下面跳转的页面
    backChecks: medtap.getRequest('backChecks')||'',
    // 获取本题id
    questionId: medtap.getRequest('questionId')||'',
    // 获取本题序号
		questionNo: medtap.getRequest('questionNo')||'',
		//  获取parentId
    parentId: medtap.getRequest('parentId')||'',
    // checks数据的数组形式
		newChecks:[],
		// 上一题的checks
		backCheckList:[],
    // 6题数据图片
		imgList: [],
		serverId: "",
		// 
		localIds: '',
		// 图片https://地址
		imgKey: '',
    // 6题数据文字
    dataText:'',
    init:function(){
	
      switch(generalPage.questionId){
        case '6':
        $('.content_body_title').text('6、请拍照上传血液检查结果')
        break;
        case '7':
        $('.content_body_title').text('6、请拍照上传胸透检查结果')
        break;
        case '8':
        $('.content_body_title').text('6、请拍照上传CT检查结果')
        break;
        case '9':
        $('.content_body_title').text('6、请拍照上传MRI检查结果')
        break;
        case '10':
        $('.content_body_title').text('6、请拍照上传PET-CT检查结果')
        break;
        case '11':
        $('.content_body_title').text('6、请拍照上传骨扫描检查结果')
        break;
        case '12':
        $('.content_body_title').text('6、请拍照上传病理检查结果')
        break;
      }
			// 点击删除图片
			$('#ImgUp').on('click', '.image_del', function() {
				var src = $(this).siblings('img').attr('src')
				var index	= generalPage.imgList.findIndex(function(item){
					return item = src
			 })
				if (confirm('确定要删除图片吗?')) {
					generalPage.imgList.splice(index,1)
					$(this).parent().remove();	
				}
			});
       // 点击下一题
       $('.next').on('click',function(){
        //  获取用户填写的数据
        generalPage.dataText = $('#content_body_fill').val()
          var data = {
            "questionId":generalPage.questionId,
            'parentId':generalPage.parentId,
             "answer":{
               'checkImgList': generalPage.imgList,
               'dataText': generalPage.dataText,
               "status": 1
             },
             "questionNo":Number(generalPage.questionNo),
             "followUpId": generalPage.followUpId
          }
          generalPage.getNextQuestion(data)
       
			})
			 // 点击上一题
			$('.pre').on('click',function(){
				console.log('上一题')
        generalPage.getPreQuestion()
      })
      generalPage.getWechatTicket();
    },
    getWechatTicket: function() {
			medtap.submitAjax({
				url: 'https://gateway.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_img').on('click', function() {
        console.log('点击了选择图片')
				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做处理的代码
								generalPage.upLoadImg(serverId, localId);
								if (localIds.length > 0) {
									syncUpload(localIds);
								}
							}
						});
					};
					wx.chooseImage({
						count: 9,
						sizeType: ['original', 'compressed'],
						sourceType: ['album', 'camera'],
						success: function(res) {
							generalPage.localIds = res.localIds;
							//所有需要上传到图片预览
							syncUpload(generalPage.localIds);
						}
					})
				})
			})
		},
		//图片预览
		previewImage: function() {
			$('#ImgUp').on('click', '.image_files_img', function() {
				var imgArray = [];
				var curImageSrc = $(this).attr('src');
				console.log($(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
					});
				}
			});
		
		},
		//上传图片
		upLoadImg: function(serverId, localId) {
			$.ajax({
				url: 'https://gateway.medtap.cn/common/uploadWechatFile',
				type: 'POST',
				async: false,
				data: {
					bizType: "3",
					folder: 'bbs',
					mediaId: serverId,
					ext: 'jpg'
				},
				headers:{
					'apptype':'wechat_lung',
					'user-os':'wechat'
				},
				success: function(data) {
					if (data.success == true) {
						var res = data.content;
						var imgKey = res.url;
						generalPage.imgList.push(imgKey);
						console.log('上传后图片的imgKey',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);
					}
				}

			})
		},
		// 获取问题答案
		getQuestionAnswer: function(){
			var crm = generalPage.adminId==''? '':'/crm'
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/operation/followUp'+crm+'/getAnswer',
					type: 'post',
					async: false,
					contentType: 'application/json',
					data: {
						"followUpId": generalPage.followUpId,
						"questionId": generalPage.questionId,
					},
					success: function(res) {
						if(!res.success) return medtap.winPop('获取数据失败')
						console.log(res) 
						if(!res.content.followAnswer.answer) {
              return
            }
						$('#content_body_fill').val(res.content.followAnswer.answer.dataText || '')
							// 图片
							var imgList = res.content.followAnswer.answer.checkImgList
							generalPage.imgList = res.content.followAnswer.answer.checkImgList
							var html =''
							if(imgList.length>0){
								for(var i = 0;i<imgList.length;i++){
								html+='<div class="image_files_item">' +
									'<img src="' + imgList[i] + '" class="image_files_img">' +
									'<img src="../images/post_delete_btn@2x.png" class="image_del" >' +
									'</div>';
								}
								$('#ImgUp').append(html)
							}
					}
				})
			},
		// 上一题
		getPreQuestion:function(){
			var crm = generalPage.adminId==''? '':'/crm'
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/operation/followUp'+crm+'/previousQuestion',
					type: 'post',
					async: false,
					contentType: 'application/json',
					data: {
						"questionId":generalPage.questionId,
						"questionNo":generalPage.questionNo,
						"followUpId":generalPage.followUpId,
					},
					success: function(res) {
						console.log(res)
						if(!res.success) return medtap.winPop('获取数据失败')
						var parentId = res.content.previousQuestion.parentId||''
						var questionId = res.content.previousQuestion.questionId||''
						var questionNo = res.content.previousQuestion.questionNo||''
						switch(questionId){
							case 5:
							medtap.pushWindow('examination.html?followUpId='+generalPage.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&adminId='+generalPage.adminId)
							break;
							case 6:
							medtap.pushWindow('generalPage.html?followUpId='+generalPage.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId+'&adminId='+generalPage.adminId)
							break;
							case 7:
							medtap.pushWindow('generalPage.html?followUpId='+generalPage.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId+'&adminId='+generalPage.adminId)
							break;
							case 8:
							medtap.pushWindow('generalPage.html?followUpId='+generalPage.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId+'&adminId='+generalPage.adminId)
							break;
							case 9:
							medtap.pushWindow('generalPage.html?followUpId='+generalPage.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId+'&adminId='+generalPage.adminId)
							break;
							case 10:
							medtap.pushWindow('generalPage.html?followUpId='+generalPage.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId+'&adminId='+generalPage.adminId)
							break;
							case 11:
							medtap.pushWindow('generalPage.html?followUpId='+generalPage.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId+'&adminId='+generalPage.adminId)
							break;
							case 12:
							medtap.pushWindow('generalPage.html?followUpId='+generalPage.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId+'&adminId='+generalPage.adminId)
							break;
						}
					}
				})
			},
    // 提交答案获取下一题id
		getNextQuestion:function(data,url){
			var crm = generalPage.adminId==''? '':'/crm'
			medtap.submitAjax({
				url: 'https://gateway.medtap.cn/operation/followUp'+crm+'/nextQuestion',
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: data,
				success: function(res) {
					console.log(res)
					if(!res.success) return medtap.winPop('获取数据失败')
					var parentId = res.content.nextQuestion.parentId||''
          var nextquestionNo = res.content.nextQuestion.questionNo||''
          var nextquestionId = res.content.nextQuestion.questionId||''
					switch(nextquestionId){
						case 7:
						medtap.pushWindow('generalPage.html?followUpId='+ generalPage.followUpId + '&questionNo='+nextquestionNo + '&questionId='+ nextquestionId+ '&parentId='+parentId+'&adminId='+generalPage.adminId)
						break;
						case 8:
						medtap.pushWindow('generalPage.html?followUpId='+ generalPage.followUpId + '&questionNo='+nextquestionNo + '&questionId='+ nextquestionId+ '&parentId='+parentId+'&adminId='+generalPage.adminId)
						break;
						case 9:
						medtap.pushWindow('generalPage.html?followUpId='+ generalPage.followUpId + '&questionNo='+nextquestionNo + '&questionId='+ nextquestionId+ '&parentId='+parentId+'&adminId='+generalPage.adminId)
						break;
						case 10:
						medtap.pushWindow('generalPage.html?followUpId='+ generalPage.followUpId + '&questionNo='+nextquestionNo + '&questionId='+ nextquestionId+ '&parentId='+parentId+'&adminId='+generalPage.adminId)
						break;
						case 11:
						medtap.pushWindow('generalPage.html?followUpId='+ generalPage.followUpId + '&questionNo='+nextquestionNo + '&questionId='+ nextquestionId+ '&parentId='+parentId+'&adminId='+generalPage.adminId)
						break;
						case 12:
						medtap.pushWindow('generalPage.html?followUpId='+ generalPage.followUpId + '&questionNo='+nextquestionNo + '&questionId='+ nextquestionId+ '&parentId='+parentId+'&adminId='+generalPage.adminId)
						break;
						case 13:
						medtap.pushWindow('geneDetection.html?followUpId='+ generalPage.followUpId + '&questionNo='+nextquestionNo + '&questionId='+ nextquestionId+ '&parentId='+parentId+'&adminId='+generalPage.adminId)
						break;
					}
				}
			})
		}
  }
	generalPage.init();
	generalPage.getQuestionAnswer();
  generalPage.choseImage();
  generalPage.previewImage();
  })