define(['zepto', 'medtap', 'mui', 'mui.picker'], function($, medtap, mui) {　
  var targetedTherapy = {
    // 获取问卷id
    followUpId: medtap.getRequest('followUpId'),
    	// 获取本题序号
		questionNo: medtap.getRequest('questionNo'),
		//  获取本题id
    questionId: medtap.getRequest('questionId'),	
    //  获取parentId
    parentId: medtap.getRequest('parentId')||'',	
    // 奥西替尼药物类型
    drugTypesPicker: '',
    // 报销的picker
    reimbursementPicker: '',
    // 城市picker
    cityPicker: '',
    // 医保类型picker
    medicalTypePicker: '',
    // 停药时间picker
    stopTimepicker: '',
    // 停药原因
    stopCausepicker: '',
    // 是否下一步治疗
    isNextpicker: '',
    // 选择有或者无
    flag: '',
    // 使用的奥西替尼
    drugTypesVal:'',
    // 所在的地区能否报销泰瑞沙的费用
    reimbursementVal: '',
    // 每盒金额
    drugCostVal: '',
    // 社保城市
    cityVal: '',
    // 医保类型
    medicalType: '',
    // 停药时间
    stopTimeVal: '',
    // 停药原因
    stopCauseVal: '',
    // 是否下一步治疗
    isNextVal: '',
    // 描述方案
    programme:'',
    init: function(){
      	// 用户点击了无
			$('.noHas').on('tap',function(){
				targetedTherapy.flag=false
        console.log(targetedTherapy.flag)
         // 如果选是 那显示输入框
       if($('.input_item_isNext').val()=='是'){
        console.log(1)
        $('.content_body_programme').show()
       }
			})
			// 用户点击了有
			$('.has').on('tap',function(){
				targetedTherapy.flag=true
				console.log(targetedTherapy.flag)
      })
     
    // 点击下一题
      $('.next').on('tap',function(){
        // 先判断必填项
        if(targetedTherapy.flag){
          targetedTherapy.drugTypesVal = $.trim($('.input_item_drugTypes').val())
          if(targetedTherapy.drugTypesVal==''){
            medtap.winPop('请选择您正在使用的奥西替尼')
						return
          }
          targetedTherapy.reimbursementVal = $.trim($('.input_item_reimbursement').val())
          if(targetedTherapy.reimbursementVal==''){
            medtap.winPop('请选择您所在的地区能否报销泰瑞沙的费用')
						return
          }
          targetedTherapy.drugCostVal = $.trim($('.input_item_drugCost').val())
          if(targetedTherapy.drugCostVal==''){
            medtap.winPop('请输入每盒的自付金额')
						return
          }
          targetedTherapy.cityVal = $.trim($('.input_item_city').val())
          if(targetedTherapy.cityVal==''){
            medtap.winPop('请选择医保所在城市')
						return
          }
          targetedTherapy.medicalType = $.trim($('.input_item_medicalType').val())
          if(targetedTherapy.medicalType==''){
            medtap.winPop('请选择医保类型')
						return
          }
          var hasUrl = 'otherTreatment.html'
           // 选择有的时候的数据
          var dataHas = 	{
            "parentId":targetedTherapy.parentId,
						"questionId":targetedTherapy.questionId,
						"answer":{
              "drugTypesVal":targetedTherapy.drugTypesVal,
              "reimbursementVal":targetedTherapy.reimbursementVal,
              "drugCostVal":targetedTherapy.drugCostVal,
              "cityVal": targetedTherapy.cityVal,
              "medicalType": targetedTherapy.medicalType,
              "status":1
            },
						"questionNo":Number(targetedTherapy.questionNo),
						"followUpId": targetedTherapy.followUpId
					}
          targetedTherapy.getNextQuestion(dataHas,hasUrl)
          // else 为选择 ‘无’ 时判断必填项
        }else{
          targetedTherapy.stopTimeVal = $.trim($('.input_item_stopTime').val())
          if(targetedTherapy.stopTimeVal==''){
            medtap.winPop('请选择停药时间')
						return
          }
          targetedTherapy.stopCauseVal = $.trim($('.input_item_stopCause').val())
          if(targetedTherapy.stopCauseVal==''){
            medtap.winPop('请选择停药原因')
						return
          }
          targetedTherapy.isNextVal = $.trim($('.input_item_isNext').val())
          if(targetedTherapy.isNextVal==''){
            medtap.winPop('请选择是否已开启下一步治疗')
						return
          }
          targetedTherapy.programme =  $.trim($('#content_body_programme').val())
          // 选择无的时候的数据
          var noUrl = 'examination.html'
          var dataNo = 	{
            "parentId":targetedTherapy.parentId,
						"questionId":targetedTherapy.questionId,
						"answer":{
              "stopTimeVal": targetedTherapy.stopTimeVal,
              "stopCauseVal":targetedTherapy.stopCauseVal,
              "isNextVal": targetedTherapy.isNextVal,
              "programme": targetedTherapy.programme,
              "status":0
            },
						"questionNo":Number(targetedTherapy.questionNo),
						"followUpId": targetedTherapy.followUpId
					}
          targetedTherapy.getNextQuestion(dataNo,noUrl)
        }
        
      })
       // 点击上一题
			$('.pre').on('tap',function(){
        // medtap.pushWindow('furtherConsultation.html?followUpId='+targetedTherapy.followUpId);
        targetedTherapy.getPreQuestion()
      })
        // 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");
    　});
    targetedTherapy.inintDrugTypes();
    targetedTherapy.initReimbursementPicker();
    targetedTherapy.initCity();
    targetedTherapy.initCityl();
    targetedTherapy.initmedicalTypePicker();
    targetedTherapy.initstopTimepicker();
    targetedTherapy.initstopCausepicker();
    targetedTherapy.initisNextpicker();
    targetedTherapy.selectItems();

    },
    //  ---------- 有 ---------
    // 初始化drugTypesPicker
    inintDrugTypes:function(){
      targetedTherapy.drugTypesPicker = new mui.PopPicker();
      targetedTherapy.drugTypesPicker.setData([
        {value:'1',text:'泰瑞沙'},
        {value:'2',text:'东南亚仿药'},
        {value:'3',text:'原料'}
      ]);
    },
    // 初始化报销reimbursementPicker
    initReimbursementPicker: function(){
      targetedTherapy.reimbursementPicker = new mui.PopPicker();
      targetedTherapy.reimbursementPicker.setData([
        {value:'1',text:'可以'},
        {value:'2',text:'部分可以'},
        {value:'3',text:'完全自费'}
      ]);
    },
    initCityl: function() {
			medtap.loading(1);
			$.ajax({
				url: "https://testdevgw.medtap.cn/common/listAreaV0",
				data: {},
				type: 'get',
				async: true,
				success: function(data) {
          console.log(data)
					medtap.loading(0);
					if (data.success) {
						var arr = data.content.areas;
						var cityData = [];
						for (var i = 0, len = arr.length; i < len; i++) {
							var obj = {};
							obj.text = arr[i].name;
							obj.value = arr[i].provinceId;
							var temp = [];
							for (var j = 0, len2 = arr[i].city.length; j < len2; j++) {
								var item = arr[i].city[j];
								var o = {};
								o.text = item.name;
								o.value = item.cityId;
								temp.push(o);
							}
							obj.children = temp;
							cityData.push(obj);
            }
            targetedTherapy.initCity(cityData)
					}
				},
				error: function(data) {
					medtapCore.jzz(0);
					mui.toast('获取所在地失败');
				}
			});
		},
    // 初始化城市cityPicker数据
    initCity:function(cityData){
    var _getParam = function(obj, param) {
        return obj[param] || '';
    };
    //-----------------------------------------
    //					//级联示例
    targetedTherapy.cityPicker = new mui.PopPicker({
        layer: 2
    });
    targetedTherapy.cityPicker.setData(cityData);
    },
     // 初始化医保类型picker
     initmedicalTypePicker:function(){
      targetedTherapy.medicalTypePicker = new mui.PopPicker();
      targetedTherapy.medicalTypePicker.setData([
        {
          text: '城镇职工基本医疗保险',
          value: 'HT1'
         },
         {
          text: '城镇居民基本医疗保险',
          value: 'HT2'
         },
         {
          text: '新型农村合作医疗',
          value: 'HT3'
         },
         {
          text: '商业医疗保险',
          value: 'HT4'
         },
         {
          text: '全自费',
          value: 'HT5'
         },
         {
          text: '全公费',
          value: 'HT6'
         },
         {
          text: '贫困救助',
          value: 'HT7'
         },
         {
          text: '其他社会保险',
          value: 'HT8'
         }
      ]);
     },
     //  ------ 无 -----------
     //  停药时间
     initstopTimepicker: function(){
      targetedTherapy.stopTimepicker = new mui.DtPicker({type:'month'});
     },
    //  停药原因
     initstopCausepicker: function(){
      targetedTherapy.stopCausepicker = new mui.PopPicker();
      targetedTherapy.stopCausepicker.setData([
        {value:'1',text:'副反应严重'},
        {value:'2',text:'耐药/疾病进展'},
        {value:'3',text:'经济难以承受'},
        {value:'4',text:'买不到'},
        {value:'5',text:'好转无需继续治疗'},
        {value:'6',text:'其他'},
      ]);
     },
    //  是否下一步治疗
     initisNextpicker: function(){
      targetedTherapy.isNextpicker = new mui.PopPicker();
      targetedTherapy.isNextpicker.setData([
        {value:'1',text:'是'},
        {value:'2',text:'否'}
      ]);
     },
     // 点击picker后的事件注册
    selectItems:function(){
          // 点击药物类型的判断
      $('.content_body_drugTypes').click(function(){
        targetedTherapy.drugTypesPicker.show(function (Items) {
      
          $('.input_item_drugTypes').val(Items[0].text)
        })
      })
      // 点击了费用报销
      $('.input_item_reimbursement').click(function(){
        targetedTherapy.reimbursementPicker.show(function (Items) {
         
          $('.input_item_reimbursement').val(Items[0].text)
        })
      })
      // 点击医保所在城市选择
      $('.input_item_city').click(function(){
        targetedTherapy.cityPicker.show(function (Items) {
          $('.input_item_city').val(Items[0].text+Items[1].text)
        })
      })
      // 点击医保类型
      $('.content_body_medicalType').click(function(){
        targetedTherapy.medicalTypePicker.show(function (Items) {
          $('.input_item_medicalType').val(Items[0].text)
        })
      })
      //  ----------- 无 ---------
      // 点击停药时间
      $('.content_body_stopTime').click(function(){
        targetedTherapy.stopTimepicker.show(function (selectItems) { 
          var timer = selectItems.y.text + '年' + selectItems.m.text + '月'
          $('.input_item_stopTime').val(timer)
      })
      })
      // 点击停药原因
      $('.content_body_stopCause').click(function(){
        targetedTherapy.stopCausepicker.show(function (Items) {
          $('.input_item_stopCause').val(Items[0].text)
        })
      })
       // 是否下一步治疗
      $('.content_body_isNext').click(function(){
        targetedTherapy.isNextpicker.show(function (Items) {
          if(Items[0].value=='1'){
            $('.input_item_isNext').val(Items[0].text)
            $('.content_body_programme').show()
          }else{
            $('.input_item_isNext').val(Items[0].text)
            $('.content_body_programme').hide()
            $('#content_body_programme').val('')
          }
        })
      })
    },
    // 获取问题答案
    getQuestionAnswer: function(){
      medtap.submitAjax({
        url: "https://testdevgw.medtap.cn/operation/followUp/getAnswer",
        type: 'post',
        async: false,
        contentType: 'application/json',
        data: {
          "followUpId": targetedTherapy.followUpId,
          "questionId": targetedTherapy.questionId,
        },
        success: function(res) {
          console.log(res) 
          if(!res.success) return medtap.winPop('获取数据失败')
          // 如果没做过题那么answer没有
					if(!res.content.followAnswer.answer) {
            targetedTherapy.flag=true
            return
          }
          // 有
          var status = res.content.followAnswer.answer.status=undefined?  '1':res.content.followAnswer.answer.status
          if(status=='1'){
            targetedTherapy.flag=true
            $('.input_item_drugTypes').val(res.content.followAnswer.answer.drugTypesVal||'') 
            $('.input_item_reimbursement').val(res.content.followAnswer.answer.reimbursementVal||'')
            $('.input_item_drugCost').val(res.content.followAnswer.answer.drugCostVal||'')
            $('.input_item_city').val(res.content.followAnswer.answer.cityVal||'')
            $('.input_item_medicalType').val(res.content.followAnswer.answer.medicalType||'')
          }else if(status=='0'){
            console.log('1')
            // 无
              targetedTherapy.flag=false
              $('.noHas').addClass('active').siblings('.has').removeClass('active')
              $('.content_select_no').addClass('active').siblings('.content_select').removeClass('active')
              if(res.content.followAnswer.answer.isNextVal=='是'){
                $('.content_body_programme').show()
              }
             $('.input_item_stopTime').val(res.content.followAnswer.answer.stopTimeVal||'')
             $('.input_item_stopCause').val(res.content.followAnswer.answer.stopCauseVal||'')
             $('.input_item_isNext').val(res.content.followAnswer.answer.isNextVal||'')
             $('#content_body_programme').val(res.content.followAnswer.answer.programme||'')
          } 
          console.log(targetedTherapy.flag)
        }
      })
    },
      // 上一题
		getPreQuestion:function(){
			medtap.submitAjax({
				url: "https://testdevgw.medtap.cn/operation/followUp/previousQuestion",
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					"questionId":targetedTherapy.questionId,
					"questionNo":targetedTherapy.questionNo,
					"followUpId":targetedTherapy.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('furtherConsultation.html?followUpId='+targetedTherapy.followUpId+'&questionId='+questionId+'&questionNo='+questionNo
            +'&parentId='+parentId);
				
				}
			})
		},
    	 // 下一题
		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('获取数据失败')
            // 获取下一题的id和序号通过url传递
          var parentId = res.content.nextQuestion.parentId||''
					var questionId = res.content.nextQuestion.questionId||''
					var questionNo = res.content.nextQuestion.questionNo||''
					medtap.pushWindow(url+'?followUpId='+targetedTherapy.followUpId+'&questionId='+questionId+'&questionNo='+questionNo+'&parentId='+parentId);
				}
			})
		}
  }
  targetedTherapy.init()
  targetedTherapy.getQuestionAnswer()
  })