define(['zepto', 'medtap', 'mui', 'mui.picker'], function($, medtap, mui) {　
  var geneDetection = {
		// 问卷id
		followUpId: medtap.getRequest('followUpId'),
		// 获取本题序号
		questionNo: medtap.getRequest('questionNo')||'',
		//  获取本题id
		questionId: medtap.getRequest('questionId')||'',
		//  获取parentId
    parentId: medtap.getRequest('parentId')||'',
    // EGFR检测数据
    EGFRpicker: '',
    // 突变部位
		Positionpicker: '',
		// 选择有或者无
		flag: true,
		// 图片集合
		imgList: [],
		serverId: "",
		// 
		localIds: '',
		// 图片https://地址
		imgKey: '',
		// EGFR值
		EGFRval: '',
		// 突变位置的值
		positionVal: '',
		// 医生对检测结果的说明
		testDescription:'',
    init: function(){
			// 点击删除图片
			$('#ImgUp').on('click', '.image_del', function() {
				var src = $(this).siblings('img').attr('src')
				var index	= geneDetection.imgList.findIndex(function(item){
					return item = src
				})
				if (confirm('确定要删除图片吗？')) {
					geneDetection.imgList.splice(index,1)
					$(this).parent().remove();	
				}
			});
			// 用户点击了无
				$('.noHas').on('tap',function(){
				geneDetection.flag=false
				console.log(geneDetection.flag)
			})
			// 用户点击了有
			$('.has').on('tap',function(){
				geneDetection.flag=true
				
				console.log(geneDetection.flag)
			})
       // 点击下一题
      $('.next').on('click',function(){
				if(geneDetection.flag){
					geneDetection.EGFRval = $('.input_item_EGFR').val()
					geneDetection.positionVal = $('.input_item_position').val()
					geneDetection.testDescription =$.trim($('#content_body_fill').val()) 
					// 如果没选EGFR检测结果
					if(geneDetection.EGFRval==''){
						medtap.winPop('请选择EGFR检测结果')
						return
					}
					switch(geneDetection.EGFRval){
						case '突变':
						// 如果没选突变位置
						if(geneDetection.positionVal==''){
						medtap.winPop('请选择突变部位')
						return
						}
						break;
					}
					
					var dataHas = {
					 "questionId":geneDetection.questionId,
					 "parentId":geneDetection.parentId,
					 "answer":{
						 'EGFRval':geneDetection.EGFRval,
						 'positionVal': geneDetection.positionVal,
						 'testDescription':geneDetection.testDescription,
						 'imgList': geneDetection.imgList,
						 "status": 1
					 },
					 "questionNo":Number(geneDetection.questionNo),
					 "followUpId": geneDetection.followUpId
					}
				  geneDetection.getNextQuestion(dataHas)		
				}else{
					var dataNo = {
						"questionId":geneDetection.questionId,
						"parentId":geneDetection.parentId,
						"answer":{
						 'EGFRval':'',
						 'positionVal': '',
						 'testDescription': '',
						 'imgList': '',
							"status": 0
						},
						"questionNo":Number(geneDetection.questionNo),
						"followUpId": geneDetection.followUpId
					 }
					 geneDetection.getNextQuestion(dataNo)	
				}
			})
			 // 点击上一题
			$('.pre').on('click',function(){
				geneDetection.getPreQuestion()
        
      })
      $(".tabbox li").click(function ()
  　　{
  　　　　//获取点击的元素给其添加样式，讲其兄弟元素的样式移除
  　　　　$(this).addClass("active").siblings().removeClass("active");
  　　　　//获取选中元素的下标
  　　　　var index = $(this).index();
  　　　　$(this).parent().siblings().children().eq(index).addClass("active")
  　　　　.siblings().removeClass("active");
  　　});
     geneDetection.getWechatTicket();
     geneDetection.inintEGFRpicker();
     geneDetection.inintPositionpicker();
     geneDetection.selectItems();
    },

    // 初始化EGFR检测picker数据
    inintEGFRpicker:function(){
    geneDetection.EGFRpicker = new mui.PopPicker();
    geneDetection.EGFRpicker.setData([
      {value:'1',text:'突变'},
      {value:'2',text:'未突变'},
      {value:'3',text:'不知道'}
    ]);
    },
    // 初始化突变部位picker数据
    inintPositionpicker:function(){
    geneDetection.Positionpicker = new mui.PopPicker();
    geneDetection.Positionpicker.setData([
      {value:'1',text:'EXON20_T790M'},
      {value:'2',text:'EXON18_G719X'},
      {value:'3',text:'EXON19_19_DEL'},
      {value:'4',text:'EXON20_S768I'},
      {value:'5',text:'EXON20_20-Ins'},
      {value:'6',text:'EXON21_L858R'},
      {value:'7',text:'EXON21_L861Q'},
      {value:'8',text:'不知道'}
    ])
    },
    // 点击picker后的事件注册
    selectItems:function(){
        // 点击EGFR检测
    $('.content_body_EGFR').click(function(){
      geneDetection.EGFRpicker.show(function (Items) {
				 console.log(Items[0].text);
        console.log(Items[0].value);
        $('.input_item_EGFR').val(Items[0].text)
        if(Items[0].value=='1'){
          $('.content_body_position').show()
        }else{
          $('.content_body_position').hide()
          $('.input_item_position').val('')
        }
      })
    })
    // 点击突变部位
    $('.content_body_position').click(function(){
      geneDetection.Positionpicker.show(function (Items) {
        console.log(Items[0].text);
        console.log(Items[0].value);
        $('.input_item_position').val(Items[0].text)
      })
    })
    },
    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_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做处理的代码
								geneDetection.upLoadImg(serverId, localId);
								if (localIds.length > 0) {
									syncUpload(localIds);
								}
							}
						});
					};
					wx.chooseImage({
						count: 9,
						sizeType: ['original', 'compressed'],
						sourceType: ['album', 'camera'],
						success: function(res) {
							geneDetection.localIds = res.localIds;
							//所有需要上传到图片预览
							syncUpload(geneDetection.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();
					console.log($(this))
				}
			});
		},
		//上传图片
		upLoadImg: function(serverId, localId) {
			$.ajax({
				url: 'https://testdevgw.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;
						geneDetection.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(){
      medtap.submitAjax({
        url: "https://testdevgw.medtap.cn/operation/followUp/getAnswer",
        type: 'post',
        async: false,
        contentType: 'application/json',
        data: {
          "followUpId": geneDetection.followUpId,
          "questionId": geneDetection.questionId,
        },
        success: function(res) {
          console.log(res) 
					if(!res.success) return medtap.winPop('获取数据失败')
					if(!res.content.followAnswer.answer) {
            geneDetection.flag=true
            return
          }
					if(res.content.followAnswer.answer.status=='0'){
						geneDetection.flag=false
						$('.noHas').addClass('active').siblings('.has').removeClass('active')
            $('.content_select_no').addClass('active').siblings('.content_select').removeClass('active')
						$('.input_item_EGFR').val('')
						$('.input_item_position').val('')
						$('#content_body_fill').val('')
						return
					}
          if(res.content.followAnswer.answer.status=='1'){
						geneDetection.flag=true
						if(res.content.followAnswer.answer.EGFRval=="突变"){
							$('.content_body_position').show()
					}
          $('.input_item_EGFR').val(res.content.followAnswer.answer.EGFRval || '')
					$('.input_item_position').val(res.content.followAnswer.answer.positionVal || '')
					$('#content_body_fill').val(res.content.followAnswer.answer.testDescription || '')
					// 图片。。
					var imgList = res.content.followAnswer.answer.imgList
					geneDetection.imgList = res.content.followAnswer.answer.imgList
					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(){
			medtap.submitAjax({
				url: "https://testdevgw.medtap.cn/operation/followUp/previousQuestion",
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					"questionId":geneDetection.questionId,
					"questionNo":geneDetection.questionNo,
					"followUpId":geneDetection.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='+geneDetection.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId)
						break;
						case 6:
						medtap.pushWindow('generalPage.html?followUpId='+geneDetection.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId)
						break;
						case 7:
						medtap.pushWindow('generalPage.html?followUpId='+geneDetection.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId)
						break;
						case 8:
						medtap.pushWindow('generalPage.html?followUpId='+geneDetection.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId)
						break;
						case 9:
						medtap.pushWindow('generalPage.html?followUpId='+geneDetection.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId)
						break;
						case 10:
						medtap.pushWindow('generalPage.html?followUpId='+geneDetection.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId)
						break;
						case 11:
						medtap.pushWindow('generalPage.html?followUpId='+geneDetection.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId)
						break;
						case 12:
						medtap.pushWindow('generalPage.html?followUpId='+geneDetection.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId+'&backChecks='+questionId)
						break;
					}
					
				}
			})
		},
		// 提交答案获取下一题id
		getNextQuestion:function(data,url){
			medtap.submitAjax({
				url: "https://testdevgw.medtap.cn/operation/followUp/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||''
					medtap.pushWindow('discomfort.html'+'?followUpId=' + geneDetection.followUpId+'&questionNo='+nextquestionNo+'&questionId='+nextquestionId+'&parentId='+parentId)
				}
			})
		}
  }
  geneDetection.init();
  geneDetection.getQuestionAnswer();
  geneDetection.choseImage();
  geneDetection.previewImage();
  })