const {post,config} = require('./promise.js');
const $ = require("jquery");

$(".conditionHintTips").on('click',".radioChecks",function(e){
  e.stopPropagation()
  const isShow = $(this).parent().attr("data-isShow")
  if(isShow == '0') {
    return
  }
  $(this).attr("data-select","select").children("i").css({
    "background":"url('../images/sex2.png') center center no-repeat"
  })
  $(this).siblings().attr("data-select","").children("i").css("background","url('../images/sex1.png') center center no-repeat")
})
$(".conditionHintTips").on('click',".closeModal",function(e){
  e.stopPropagation()
  $(this).parents(".popupModal").css("display","none")
})
$(".conditionHintTips").on('click',".popupModal",function(e){
  e.stopPropagation()
})
$(".conditionHintTips").on('click',".mayResultSelect",function(){
  $(".popupModal").css('display',"none")
  $(this).children(".popupModal").css('display',"block")
  var that = $(this);
  var numFlg = $(this).attr("data-num")
  var tmpMoadlData = window.pushMessage[numFlg]
  for(var i = 0;i<tmpMoadlData.details.length;i++){
    var tmpPushMsgDetail = tmpMoadlData.details[i]
    if(tmpPushMsgDetail.type == 2){
      for(var m = 0;m < tmpPushMsgDetail.content.details.length;m++){
        var tmpPushMsgDetailContentVal = tmpPushMsgDetail.content.details[m]
        if(that.find(".makesureResult").html() != '请选择'){
          if(tmpPushMsgDetailContentVal.controlType == 0){
            var domList = that.find(".caculateLis").children("li")
            for(var n =0;n < tmpPushMsgDetailContentVal.details.length;n++){
              if(tmpPushMsgDetailContentVal.details[n].state == 1){
                domList.eq(n).attr("data-select","select").children("i").css({
                  "background":"url('../images/sex2.png') center center no-repeat"
                })
              }
              if(tmpPushMsgDetailContentVal.details[n].state == 0){
                domList.eq(n).attr("data-select","").children("i").css({
                  "background":"url('../images/sex1.png') center center no-repeat"
                })
              }
            }
          }
          if(tmpPushMsgDetailContentVal.controlType == 2){
            var domLis = that.find("input[type=text]").eq(m)
            $(domLis).val(tmpPushMsgDetailContentVal.value)
          }
        }
      }
    }
    if(tmpPushMsgDetail.type == 3 && that.find(".sureScore").attr("data-type") == 3){
      for(var a = 0;a < tmpPushMsgDetail.content.details.length;a++){
        var tmpPushMsgDetailContentValS = tmpPushMsgDetail.content.details[a]
        var domLi = that.find(".radioChecks")
        for(var b = 0;b <domLi.length;b++){
          if(that.find(".makesureResult").html() == tmpPushMsgDetailContentValS.detailName){
            domLi.eq(a).attr("data-select","select").children("i").css({
              "background":"url('../images/sex2.png') center center no-repeat"
            })
          }else{
            domLi.eq(a).attr("data-select","").children("i").css({
              "background":"url('../images/sex1.png') center center no-repeat"
            })
          }
        }
      }
    }
  }
})
$(".conditionHintTips").on('click',".sureScore",function(e){
  e.stopPropagation()
  if($(this).attr("data-type") == 3){
    var tempVal = $(this).parents(".mayResultSelect").find(".radioChecks[data-select='select'] span").html()
    tempVal && $(this).parents(".mayResultSelect").find(".makesureResult").html(tempVal)
  }
  $(this).parents(".popupModal").css('display',"none")
})
$(".conditionHintTips").on('click','.calculate',function(e){
  e.stopPropagation()
  var that = $(this)
  var numFlg = that.attr("data-num")
  var tmpMoadlData = window.pushMessage[numFlg]
  var paramsJson = {}
  var resultParams = {}
  for(var i = 0;i<tmpMoadlData.details.length;i++){
    var tmpPushMsgDetail = tmpMoadlData.details[i]
    if(tmpPushMsgDetail.type == 2){
      var tmpDetailArr = []
      paramsJson = tmpPushMsgDetail;
      for(var m = 0;m < tmpPushMsgDetail.content.details.length;m++){
        var tmpPushMsgDetailContentVal = tmpPushMsgDetail.content.details[m]
        if(tmpPushMsgDetailContentVal.controlType == 0){
          var domList = that.parent(".modalMainTwo").find(".caculateLis").children("li")
          for(var n =0;n < domList.length;n++){
            if($(domList[n]).attr("data-select")){
              tmpPushMsgDetailContentVal.details[n].state = 1
            }else{
              tmpPushMsgDetailContentVal.details[n].state = 0
            }
          }
        }
        if(tmpPushMsgDetailContentVal.controlType == 2&&tmpPushMsgDetailContentVal.isShow !='0'){
          tmpPushMsgDetailContentVal.value = $('.'+paramsJson.content.name+tmpPushMsgDetailContentVal.name).val()
        }
        tmpDetailArr.push(tmpPushMsgDetailContentVal)
      }
      resultParams.type = paramsJson.type
      resultParams.data = paramsJson
    }
  }
  for(var s = 0;s < paramsJson.content.details.length;s++){
    var hasSelect = false
    if(paramsJson.content.details[s].controlType == 0) {
      for(let i = 0; i < paramsJson.content.details[s].details.length; i++) {
        if(paramsJson.content.details[s].details[i].state == '1') {
          hasSelect = true
        }
      }
    }
    if(paramsJson.content.details[s].controlType == 2 && paramsJson.content.details[s].value == ''||paramsJson.content.details[s].controlType == 0 && !hasSelect){
      that.parent().find(".calculateError").css("display","block");
      that.parent().find(".calculateResult").html('');
      return;
    }
  }
  post(config.calculate,resultParams).then((res)=>{
    var data = res.data
    if(data.code == 0){
      var result = data.data;
      var tmpName = that.parent().find(".calculateName");
      var deepDetailResultStr = '';
      for(var v = 0;v < result.result.length;v++){
        var name = result.result[v].name
        var text = result.result[v].text
        deepDetailResultStr+='<p class="result"><span class="calculateName">'+name+'：</span><span class="calculateResult">'+text+'</span></p>'
      }
      $('.resultLis').html(deepDetailResultStr)
      if($(".sureScore").attr("data-type") == 2){
        var tmpVal = that.parents(".mayResultSelect").find(".calculateResult:last").html()
        tmpVal && that.parents(".mayResultSelect").find(".makesureResult").html(tmpVal)
      }
      that.parent().find(".calculateError").css("display","none")
    }else{
      that.parent().find(".calculateError").html(data.msg).css("display","block")
      that.parent().find(".calculateResult").html('')
    }
  })
})

