define(['zepto', 'medtap', 'mui', 'mui.picker'], function($, medtap, mui) { 
  
  var examination = {
    // 问卷id
    followUpId: medtap.getRequest('followUpId'),
    // 获取本题序号
		questionNo: medtap.getRequest('questionNo'),
		//  获取本题id
    questionId: medtap.getRequest('questionId'),
    //  获取parentId
    parentId: medtap.getRequest('parentId')||'',
    	// 选择有或者无
    flag: true,
    // 检查选择数据
    examinationObj:{
      '6': false,
      '7': false,
      '8': false,
      '9': false,
      '10': false,
      '11': false,
      '12': false
    },
    checkList: [],
    // 选中数据
    checks:'',
    init:function(){
     // 选择改变状态
      $('.content_select_has').on('change','input[type=checkbox]',function(){ 
      console.log($(this)[0].checked,$(this)[0].name)
      examination.examinationObj[$(this)[0].name] = $(this)[0].checked
      console.log(examination.examinationObj)
      examination.checkList = []
      for(var key in  examination.examinationObj){
        if(examination.examinationObj[key]){
          examination.checkList.push(key)
        }
      }
      examination.checks = examination.checkList.join(',')
      console.log(examination.checks)
      if(examination.checks==''){
        examination.flag=false
        $('.noHas').addClass('active').siblings('.has').removeClass('active')
        $('.content_select_no').addClass('active').siblings('.content_select').removeClass('active')
      }
      })
      	// 用户点击了无
			$('.noHas').on('tap',function(){
				examination.flag=false
        console.log(examination.flag)
        examination.checks = ''
			})
			// 用户点击了有
			$('.has').on('tap',function(){
				examination.flag=true
        console.log(examination.flag)
        examination.getQuestionAnswer()
        if(examination.checks==''){
          $('.bloodTest').prop('checked',true)
          examination.checks = '6'
        }
        var nodelist=document.querySelectorAll('.content_select_has input')
        var list = []
        nodelist.forEach(function(ele) {
         if(ele.checked) {
           list.push(ele.value)
         }
         
        })
        examination.checks = list.join(',')
         console.log(examination.checks)
			})
       // 点击下一题
       $('.next').on('tap',function(){
         if(examination.flag){
            var dataHas = {
            "parentId":examination.parentId,
             "questionId":examination.questionId,
             "answer":{
               "checks":examination.checks,
               "status": 1
             },
             "questionNo":Number(examination.questionNo),
             "followUpId": examination.followUpId
            }
           examination.getNextQuestion(dataHas)
         }
         if(!examination.flag) {
           var dataNo = {
            "parentId":examination.parentId,
            "questionId":examination.questionId,
						"answer":{
              "checks":'',
              "status": 0
            },
						"questionNo":Number(examination.questionNo),
						"followUpId": examination.followUpId
           }
          examination.getNextQuestion(dataNo)
         }
			})
			 // 点击上一题
			 $('.pre').on('tap',function(){
        examination.getPreQuestion()
      })
      // tab 切换
      $(".tabbox li").on('tap',function (){
      //获取点击的元素给其添加样式,讲其兄弟元素的样式移除
      $(this).addClass("active").siblings().removeClass("active");
      //获取选中元素的下标
      var index = $(this).index();
      $(this).parent().siblings().children().eq(index).addClass("active")
      .siblings().removeClass("active");
    });
    },
     // 获取问题答案
		 getQuestionAnswer: function(){
			medtap.submitAjax({
				url: "https://testdevgw.medtap.cn/operation/followUp/getAnswer",
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					"followUpId": examination.followUpId,
					"questionId": examination.questionId,
				},
				success: function(res) {
          console.log(res)
          if(!res.success) return medtap.winPop('获取数据失败')
          // 如果没做过题那么answer没有
					if(!res.content.followAnswer.answer) {
            examination.flag=true
            examination.checks='6'
            examination.examinationObj ={
              '6': true,
              '7': false,
              '8': false,
              '9': false,
              '10': false,
              '11': false,
              '12': false
            }
            $('.bloodTest').prop('checked',true)
            console.log(examination.checks)
            return
          }
          // console.log(res.content.followAnswer.answer.checks) 
          examination.checks = res.content.followAnswer.answer.checks
          if(res.content.followAnswer.answer.status=='1'){
            examination.flag = true
            examination.checks = res.content.followAnswer.answer.checks
            var chceksStr = res.content.followAnswer.answer.checks
            examination.checkList = chceksStr.split(',')
            var nodelist=document.querySelectorAll('.content_select_has input')
            examination.checkList.forEach(function(ele) {
               switch(ele){
                 case '6':
                 nodelist[0].checked = true
                 examination.examinationObj['6']=true
                 break;
                 case '7':
                 nodelist[1].checked = true
                 examination.examinationObj['7']=true
                 break;
                 case '8':
                 nodelist[2].checked = true
                 examination.examinationObj['8']=true
                 break;
                 case '9':
                 nodelist[3].checked = true
                 examination.examinationObj['9']=true
                 break;
                 case '10':
                 nodelist[4].checked = true
                 examination.examinationObj['10']=true
                 break;
                 case '11':
                 nodelist[5].checked = true
                 examination.examinationObj['11']=true
                 break;
                 case '12':
                 nodelist[6].checked = true
                 examination.examinationObj['12']=true
                 break;
               }
            });
          } else{
            examination.flag = false
            $('.noHas').addClass('active').siblings('.has').removeClass('active')
            $('.content_select_no').addClass('active').siblings('.content_select').removeClass('active')
          }
				}
			})
    },
    // 上一题
		getPreQuestion:function(){
			medtap.submitAjax({
				url: "https://testdevgw.medtap.cn/operation/followUp/previousQuestion",
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					"questionId":examination.questionId,
					"questionNo":examination.questionNo,
					"followUpId":examination.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||''
          if(questionId=='4'){
            medtap.pushWindow('otherTreatment.html?followUpId='+examination.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId);
          }else{
            medtap.pushWindow('targetedTherapy.html?followUpId='+examination.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId);
          }
				}
			})
		},
    // 提交答案获取下一题id
		getNextQuestion:function(data){
      console.log(data)
			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('获取数据失败')
          // 获取下一题的id和序号通过url传递
          var parentId = res.content.nextQuestion.parentId||''
          var questionId = res.content.nextQuestion.questionId||''
          var questionNo = res.content.nextQuestion.questionNo||''
          switch(questionId){
            case 6:
            medtap.pushWindow('generalPage.html?followUpId='+examination.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId)  
            break;
            case 7:
            medtap.pushWindow('generalPage.html?followUpId='+examination.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId)  
            break;
            case 8:
            medtap.pushWindow('generalPage.html?followUpId='+examination.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId)  
            break;
            case 9:
            medtap.pushWindow('generalPage.html?followUpId='+examination.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId)  
            break;
            case 10:
            medtap.pushWindow('generalPage.html?followUpId='+examination.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId)  
            break;
            case 11:
            medtap.pushWindow('generalPage.html?followUpId='+examination.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId)  
            break;
            case 12:
            medtap.pushWindow('generalPage.html?followUpId='+examination.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId)  
            break;
            case 13:
            medtap.pushWindow('geneDetection.html?followUpId='+examination.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId)  
            break;
          }
				}
			})
		}
  }
  examination.init()
  examination.getQuestionAnswer()
})