
export const confirn=(state,action)=>{//确定
    const res=Object.assign({},state);
    res.showCheckBox = false;
    let checkBoxId = action.checkBoxId;
    let datas = res.datas;
    for(let i=0; i<datas.length; i++){
      if(datas[i].id == checkBoxId){
        datas[i].showDrop = false;
      }
    }

    return res;
};

export const clear = (state,action)=>{//清空选项
  const res=Object.assign({},state);
  // res.selectVal = [];
  // 清空多选
  let values = res.checks;
  for(let i=0; i<values.length; i++){
    values[i].isSelect = false;
  }
  res.showCheckBox = false;
  

  return res;
};

export const showCheckBox = (state,action)=>{//点击症状显示下拉
  const res=Object.assign({},state);
  console.log(res,action);
  let show = action.showCheckBox;
  res.showCheckBox = !action.showCheckBox;
  res.showDrop = !res.showDrop;
  let inlineDatas = res.datas;
  let id = action.id;
  for(let i=0; i<inlineDatas.length; i++){
    if(inlineDatas[i].id == id){
      inlineDatas[i].showDrop = !inlineDatas[i].showDrop;
    }
  }

  return res;
};

export const checked = (state,action)=>{//选择下拉选项
  const res=Object.assign({},state);
  res.noSpecialFlag = 1;//互斥
  let item = action.value.name;
  let parentId = action.value.parentId;
  let change = res.change;
  let firstC = res.firstC;
  let thenC = res.thenC;
  let values = res.checks;
  // let values = res.zayin[0].questionMapping;
  let firstArr = [];
  let thenArr = [];

  if(firstC && change){//初为
    for(let i=0; i<values.length; i++){
      if(values[i].name == item){
        values[i].isSelect = !values[i].isSelect;
        values[i].first = true;
        values[i].then = false;
      }
    }
    firstArr.push(item);
    // firstArr += item+'，';
    res.firstArr = firstArr;
    const theIndex = res.thenArr.indexOf(item);
    if(theIndex !== -1){
      res.thenArr.splice(theIndex,1)
    }
  }

  else if(thenC){//后为
    for(let k=0; k<values.length; k++){
      if(values[k].name == item){
        values[k].isSelect = !values[k].isSelect;
        values[k].then = true;
        values[k].first = false;
      }
    }
    // thenArr += item+'，';
    thenArr.push(item);
    res.thenArr = thenArr;
    const firIndex = res.firstArr.indexOf(item);
    if(firIndex !== -1){
      res.firstArr.splice(firIndex,1)
    }
  }

else{//无鼠标点击顺序
  for(let i=0; i<values.length; i++){
    if(values[i].name == item){
      values[i].isSelect = !values[i].isSelect;
    }
  }
  let selected = values.filter(function(item){
    return item.isSelect;
  });

  let checkVals = [];
  // 如果没有选中项，就让无殊恢复默认
  
  if(selected.length == 0){
    res.noSpecialFlag = 0; //只有多选时，还有单选需另外处理
  }else{
    for(let k=0; k<selected.length; k++){
      // checkVals += selected[k].name + "，";
      checkVals.push(selected[k].name+ "，");
    }
  }
   
  res.selectVal = checkVals;
}
  return res;
};

export const firstChoo = (state,action)=>{//点击初为
  const res=Object.assign({},state);
  res.firstC = !res.firstC;
  res.change = action.change;
  let values = res.checks;
  if(!res.firstC){//取消初为
    let firstSelected = values.filter(function(item){
      return item.first;
    });
    if(firstSelected){//有选中项
      for(let j=0; j<firstSelected.length; j++){
        firstSelected[j].first = false;
        firstSelected[j].isSelect = false;
      }
    }
  }
  return res;
};

export const thenChoo = (state,action)=>{//点击后为
  const res=Object.assign({},state);
  res.thenC = !res.thenC;
  res.change = action.change;
  let values = res.checks;
  if(!res.thenC){//取消后为
    let thenSelected = values.filter(function(item){
      return item.then;
    });
    if(thenSelected){//有选中项
      for(let j=0; j<thenSelected.length; j++){
        thenSelected[j].then = false;
        thenSelected[j].isSelect = false;
      }
    }
  }
  return res;
};

export const noSpecial = (state,action)=>{//点击无殊
  const res=Object.assign({},state);
  res.noSpecial = !res.noSpecial;//无殊选中标识
  res.noSpecialFlag = res.noSpecial? 2 : 0;//互斥
  // res.noSpecialFlag = 2;
  console.log(res);
  return res;
};





