import config from '@config/index';
import {getLabelIndex,fullfillText} from '@common/js/func.js';

//多选标签选中确定处理
export const confirm = (state,action) =>{
  let res = Object.assign({},state);
  let arr = res.data;
  const {nones,exists,withs,ikey,exclusion,excluName,copyType} = action.data;
  //选中互斥项
  if(exclusion){
    arr[ikey].value = excluName;
    res.saveText[ikey] = excluName;
    res.selecteds[ikey] = action.data;
    res.update=Math.random();
    return res;
  }
  //只有无的项目选中
  if(arr[ikey].value)  arr[ikey].value= '';
  if([...exists,...withs].length==0&&nones){
    const newPreText = arr[ikey-1].name+nones;//console.log(arr[ikey-1],newPreText)
    arr[ikey-1].value = newPreText;
    res.saveText[ikey-1] = newPreText;
    res.update=Math.random();
    return res;
  }
  //有、伴项目有选中
  //const existDatas = fullfillText(exists).newArr;
  //let withDatas = fullfillText(withs).newArr;
  /*if(withDatas&&withDatas.length>0){
    withDatas[0].value=withs.length>0?"伴":'';
  }*/
  //const text = Object.assign({},JSON.parse(config.textLabel),{value:"伴"}):undefined;
  if(+copyType===1){       //原标签保留
    arr.splice(ikey-1,0,...exists,...withs);
  }else{                 //替换原标签
    arr.splice(ikey,1);
    arr.splice(ikey-1,1,...exists,...withs);
  }

  res.update=Math.random();         //用于触发组件更新（data变化了因在对象中无法被组件检测到）
  return res;
};

//单选下拉选中
export function setRadioValue(state,action){
  let res = Object.assign({},state);
  const {ikey,id,text} = action;
  let labelInx = getLabelIndex(ikey);
  const subInx = ikey.substr(ikey.length-1);
  let item = res.data[labelInx];
  if(typeof text != 'string'){    //需要展开项--有无治疗类型
    const len = +item.copyType === 0?1:0;
    res.data.splice(labelInx,len,text);
    return res;
  }
  if(+item.tagType===1){
    item.value = text;
    res.saveText[labelInx] = item.labelPrefix+text+item.labelSuffix;
    item.questionDetailList.map((its)=>{
      if(its.id === id){
        its.selected = true;
      }else{
        its.selected = false;
      }
    });
  }else{
    item.questionMapping[subInx].value = text;
    let hasValue = false;
    const sub = item.questionMapping.map((it)=>{
      //添加选中状态
      it.questionDetailList.map((its)=>{
        if(its.id === id){
          its.selected = true;
        }else{
          its.selected = false;
        }
      });

      if(it.value){     //至少有一个子值才黑显
        hasValue = true;
      }
      return (it.labelPrefix||'')+(it.value||'')+(it.labelSuffix||'');
    });
    res.saveText[labelInx] = hasValue?sub.join(''):'';
  }
  res.update = Math.random();
  return res;
}

//数字键盘选中事件
export function setNumberValue(state,action){
  let res = Object.assign({},state);
  const param = action.params;
  const ikey = param.ikey;
  let labelInx = getLabelIndex(ikey);
  const subInx = ikey.substr(ikey.length-1);
  let item = res.data[labelInx];
  if(+item.tagType===1){
    item.value = param.text;
    res.saveText[labelInx] = param.text?item.labelPrefix+param.text+item.labelSuffix:'';
  }else{
    item.questionMapping[subInx].value = param.text;
    let hasValue = false;
    const sub = item.questionMapping.map((it)=>{
      if(it.value){     //至少有一个子值才黑显
        hasValue = true;
      }
      return (it.labelPrefix||'')+(it.value||'')+(it.labelSuffix||'');
    });
    res.saveText[labelInx] = hasValue?sub.join(''):'';
  }
  res.update = Math.random();
  return res;
}

//自由文本
export function setCheckText(state,action) {
  let res = Object.assign({},state);
  const {i,text} = action;
  if(res.data[i]){
    res.data[i].value=text;
    //res.data[i].name='';      //默认显示的文字
  }
  res.saveText[i] = text;
  res.update = Math.random();
  return res;
}

//文本输入标签
export function setOtherInput(state,action){
  let res = Object.assign({},state);
  const {i,text,prefix,suffix} = action;
  if(res.data[i]){
    res.data[i].value=text;
  }
  res.saveText[i] = prefix+text+suffix;
  res.update = Math.random();
  return res;
}

//多选文字，如杂音
export function setCheckBoxValue(state,action) {
  let res = Object.assign({},state);
  const {labelInx,excluName,existsName,nones,withsName} = action.data;
  const showText = (excluName||'')+(existsName||'')+(nones||'')+(withsName||'');
  res.data[labelInx].value = showText;
  res.saveText[labelInx] = showText;
  res.selecteds[labelInx] = action.data;
  res.update = Math.random();
  return res;
}
//搜索结果
export function setSearchData(state,action){
  let res = Object.assign({},state);
  res.searchData = action.data;
  res.searchStr = action.inpStr;
  return res;
}

//插入标签数据
export function insertLabelData(state,action){
  let res = Object.assign({},state);
  const text = Object.assign({},JSON.parse(config.textLabel));
  const searchStr = res.searchStr;
  const {index,data,isReplace,span}=action;
  const showText = res.saveText[index];
  const spreadLabels = data;
  const toEnd = showText.indexOf(searchStr)>0;    //替换前或后的搜索词（中间不可搜）
  let reg = toEnd?new RegExp(searchStr+"$"):new RegExp("^"+searchStr);
  const newText=showText.replace(reg,'')||' ';
  if(!isReplace){
    span.current.innerText = newText;
    const pText = Object.assign({},text,{value:newText});
    res.data.splice(index,1,pText,spreadLabels,text);
    res.saveText.splice(index,1,newText,'','');
  }else{
    span.current.innerText = ' ';
    res.data.splice(index+1,0,spreadLabels,text);
    res.saveText.splice(index+1,0,'','');
  }
  res.searchData = [];    //选中清空搜索内容（即关闭搜索弹窗）
  res.update = Math.random();
  return res;
}

export function changeTextLabel(state,action) {
  const res = Object.assign({},state);
  const index = action.data.ikey;
  const newVal = action.data.changeVal;
  let item = res.data[index];
  if(item){
    item.value = newVal;
  }
  res.saveText[index] = newVal;
  res.update = Math.random();
  return res;
}

export function clearOtherHistory(state,action){       //清空数据
    let res = Object.assign({},state);
    res.data = action.data;
    res.saveText = action.saveText;
    res.selecteds = action.selecteds;
    res.editClear = action.editClear;
    return res;
  }
//文本模式下值保存
export const setTextModeValue = (state,action)=>{
  const res = Object.assign({},state);
  res.saveText[0] = action.text;
  return res;
};

//复制标签（如血压）事件
export function addLabelItem(state,action){
    let res = Object.assign({},state);
    const {data,i} = action;
    const textLabel = JSON.parse(config.textLabel);
    //使用Object.assign({},data)拷贝操作时复制项和原项会同步修改
    if(!data) return res;
    res.data.splice(i+1,0,JSON.parse(data),textLabel);
    res.update = Math.random();
    return res;
}
export const otherEditClear = (state,action)=>{
    const res = Object.assign({},state);
    res.editClear = action.bool;
    return res;
};