define(['zepto', 'medtap', 'mui', 'mui.picker'], function($, medtap, mui) {　
  var discomfort = {
    // 问卷id
    followUpId: medtap.getRequest('followUpId')||'',
    // 获取本题id
    questionId: medtap.getRequest('questionId')||'',
    // 获取本题序号
    questionNo: medtap.getRequest('questionNo')||'',
    //  获取parentId
    parentId: medtap.getRequest('parentId')||'',
   
    // 选择 有或者无
    flag: true,
    // 检查选择数据
    discomfortGPObj:{
        '15': false,
        '16': false,
        '17': false,
        '18': false,
        '19': false,
        '20': false,
        '21': false,
        '22': false,
        '23': false,
        '24': false,
        '25': false,
        '26': false,
        '27': false,
        '28': false,
        '29': false,
        '30': false,
        '31': false,
        '32': false,
        '33': false
      },
    checkList: [],
    // 选中数据
    checks:'',
    
    init: function(){
      
      // 选择改变状态
      $('.content_select_has .dowebok').on('change','input[type=checkbox]',function(){ 
        console.log($(this)[0].checked,$(this)[0].name)
        discomfort.discomfortGPObj[$(this)[0].name] = $(this)[0].checked
        console.log(discomfort.discomfortGPObj)
        discomfort.checkList = []
        for(var key in  discomfort.discomfortGPObj){
          if(discomfort.discomfortGPObj[key]){
            discomfort.checkList.push(key)
          }
        }
        discomfort.checks = discomfort.checkList.join(',')
        if(discomfort.checks==''){
          discomfort.flag=false
          $('.next').text('提交')
          $('.noHas').addClass('active').siblings('.has').removeClass('active')
          $('.content_select_no').addClass('active').siblings('.content_select').removeClass('active')
        }
        })
       	// 用户点击了无
			$('.noHas').on('tap',function(){
				discomfort.flag=false
        console.log(discomfort.flag)
        discomfort.checks = ''
        $('.next').text('提交')
			})
			// 用户点击了有
			$('.has').on('tap',function(){
        $('.next').text('下一题')
				discomfort.flag=true
        console.log(discomfort.flag)
        discomfort.getQuestionAnswer()
        if(discomfort.checks==''){
          $('.checkbox_pl').prop('checked',true)
          discomfort.checks = '15'
        }
        var nodelist=document.querySelectorAll('.dowebok input')
        var list = []
        nodelist.forEach(function(ele) {
         if(ele.checked) {
           list.push(ele.name)
         }  
        })
        discomfort.checks = list.join(',')
         console.log(discomfort.checks)
			}) 
        // 点击下一题
        $('.next').on('tap',function(){
          if(discomfort.flag){
             var dataHas = {
              "questionId":discomfort.questionId,
              "parentId":discomfort.parentId,
              "answer":{
                "checks": discomfort.checks,
                "status": 1
              },
              "questionNo":Number(discomfort.questionNo),
              "followUpId": discomfort.followUpId
             }
            discomfort.getNextQuestion(dataHas)
          }else {
            var dataNo = {
             "questionId":discomfort.questionId,
             "parentId":discomfort.parentId,
             "answer":{
               "checks": '',
               "status": 0
             },
             "questionNo":Number(discomfort.questionNo),
             "followUpId": discomfort.followUpId
            }
           discomfort.submitQuestionAnswer(dataNo)
          }
       })
      // tab切换
      $(".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");
  　　});
     
			 // 点击上一题
			 $('.pre').on('click',function(){
        discomfort.getPreQuestion()
      })
    },
     // 获取问题答案
		 getQuestionAnswer: function(){
			medtap.submitAjax({
				url: "https://testdevgw.medtap.cn/operation/followUp/getAnswer",
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					"followUpId": discomfort.followUpId,
					"questionId": discomfort.questionId,
				},
				success: function(res) {
          console.log(res)
          if(!res.success) return medtap.winPop('获取数据失败')
          if(!res.content.followAnswer.answer) {
            discomfort.flag=true
            discomfort.checks='15'
            discomfort.discomfortGPObj ={
              '15': true,
              '16': false,
              '17': false,
              '18': false,
              '19': false,
              '20': false,
              '21': false,
              '22': false,
              '23': false,
              '24': false,
              '25': false,
              '26': false,
              '27': false,
              '28': false,
              '29': false,
              '30': false,
              '31': false,
              '32': false,
              '33': false
            }
            $('.checkbox_pl').prop('checked',true)
            return
          }
          if(res.content.followAnswer.answer.status=='0'){
            discomfort.flag=false
            $('.next').text('提交')
            $('.noHas').addClass('active').siblings('.has').removeClass('active')
            $('.content_select_no').addClass('active').siblings('.content_select').removeClass('active')
            return
          }
          if(res.content.followAnswer.answer.status=='1'){
            discomfort.flag=true
            discomfort.checks = res.content.followAnswer.answer.checks
              var arr = res.content.followAnswer.answer.checks
              discomfort.checkList = arr.split(',')
              discomfort.discomfortGPObj['15'] = false
              var nodelist=document.querySelectorAll('.dowebok input')
              discomfort.checkList.forEach(function(ele) {
                 switch(ele){
                   case '15':
                   nodelist[0].checked = true
                   discomfort.discomfortGPObj['15'] = true
                   break;
                   case '16':
                   nodelist[1].checked = true
                   discomfort.discomfortGPObj['16'] = true
                   break;
                   case '17':
                   nodelist[2].checked = true
                   discomfort.discomfortGPObj['17'] = true
                   break;
                   case '18':
                   nodelist[3].checked = true
                   discomfort.discomfortGPObj['18'] = true
                   break;
                   case '19':
                   nodelist[4].checked = true
                   discomfort.discomfortGPObj['19'] = true
                   break;
                   case '20':
                   nodelist[5].checked = true
                   discomfort.discomfortGPObj['20'] = true
                   break;
                   case '21':
                   nodelist[6].checked = true
                   discomfort.discomfortGPObj['21'] = true
                   break;
                   case '22':
                   nodelist[7].checked = true
                   discomfort.discomfortGPObj['22'] = true
                   break;
                   case '23':
                   nodelist[8].checked = true
                   discomfort.discomfortGPObj['23'] = true
                   break;
                   case '24':
                   nodelist[9].checked = true
                   discomfort.discomfortGPObj['24'] = true
                   break;
                   case '25':
                   nodelist[10].checked = true
                   discomfort.discomfortGPObj['25'] = true
                   break;
                   case '26':
                   nodelist[11].checked = true
                   discomfort.discomfortGPObj['26'] = true
                   break;
                   case '27':
                   nodelist[12].checked = true
                   discomfort.discomfortGPObj['27'] = true
                   break;
                   case '28':
                   nodelist[13].checked = true
                   discomfort.discomfortGPObj['28'] = true
                   break;
                   case '29':
                   nodelist[14].checked = true
                   discomfort.discomfortGPObj['29'] = true
                   break;
                   case '30':
                   nodelist[15].checked = true
                   discomfort.discomfortGPObj['30'] = true
                   break;
                   case '31':
                   nodelist[16].checked = true
                   discomfort.discomfortGPObj['31'] = true
                   break;
                   case '32':
                   nodelist[17].checked = true
                   discomfort.discomfortGPObj['32'] = true
                   break;
                   case '33':
                   nodelist[18].checked = true
                   discomfort.discomfortGPObj['33'] = true
                   break;
                 }
              });
            } 
				}
			})
    },
    // 上一题
		getPreQuestion:function(){
			medtap.submitAjax({
				url: "https://testdevgw.medtap.cn/operation/followUp/previousQuestion",
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					"questionId":discomfort.questionId,
					"questionNo":discomfort.questionNo,
					"followUpId":discomfort.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||''
          medtap.pushWindow('geneDetection.html?followUpId='+discomfort.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId);
				}
			})
		},
      // 提交答案获取下一题id
		getNextQuestion:function(data,url){
      console.log(discomfort.checks)
			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)
          // 获取下一题的id和序号通过url传递
          var parentId = res.content.nextQuestion.parentId||''
          var questionId = res.content.nextQuestion.questionId||''
          var questionNo = res.content.nextQuestion.questionNo||'' 
          switch(questionId){
            case 15:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 16:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 17:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 18:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 19:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 20:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 21:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 22:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 23:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 24:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 25:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 26:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 27:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 28:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 29:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 30:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 31:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 32:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            case 33:
            medtap.pushWindow('discomfortGeneralPage.html?followUpId='+discomfort.followUpId+'&questionNo='+questionNo+'&questionId='+questionId+'&parentId='+parentId+'&lastChecks='+discomfort.checks)  
            break;
            default:
            medtap.pushWindow('submitSuccessfully.html?followUpId='+discomfort.followUpId)  
          }
				}
			})
    },
     //  最后提交问卷
    submitQuestionAnswer: function(data){
      medtap.submitAjax({
				url: "https://testdevgw.medtap.cn/operation/followUp/completeQuestion",
				type: 'post',
				async: false,
				contentType: 'application/json',
        data: data,
        success: function(res){
          console.log(res)
          medtap.pushWindow('submitSuccessfully.html?followUpId='+discomfort.followUpId)
        }
      })
     }
  }
  discomfort.init()
  discomfort.getQuestionAnswer()
})