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


function insertPro(data,processModule){//插入添加病情变化模板
  let index;
  for(let i=0; i<data.length; i++){
    if(data[i].flag && data[i].flag==3 ){
      index = i;
    }
  }
  for(let j=0; j<processModule.length; j++){
    let pos = index + j;
    data.splice(pos,0,processModule[j]);
  }
  return data;
}


//获取主诉模板
export const setMainMoudle = (state,action) => {
  const res = Object.assign({},state);
  // const emptySpan = JSON.parse(config.textLabel);
  // let data = action.data.push(emptySpan);
  // 处理tagType=4的类型
  // const originalData = JSON.parse(JSON.stringify(action.data));
  const originalData = action.data||[];
  const originalSonData = action.addSymptom||[];
  let spreadLabels=[];
  let sonSpreadLabels=[];//子模板
  if(originalData){
    for(let i=0; i<originalData.length;i++){
      if(originalData[i].tagType==4){
        spreadLabels.push(...originalData[i].questionMapping);
      }else{
        spreadLabels.push(originalData[i]);
      }
    }
  }
  if(originalSonData){
    for(let j=0; j<originalSonData.length;j++){
      if(originalSonData[j].tagType==4){
        sonSpreadLabels.push(...originalSonData[j].questionMapping);
      }else{
        sonSpreadLabels.push(originalSonData[j]);
      }
    }
  } 
  // res.moduleData = action.data;
  // res.addSmoduleData = action.addSymptom;
  res.moduleData = spreadLabels;
  // res.saveText = res.save||[];
  res.saveText = res.save||res.saveText;//慢病
  res.addSmoduleData = sonSpreadLabels;
  res.update=Math.random();
  return res;
}

//获取常见症状
export const getCommSymptoms = (state,action) => {
  const res = Object.assign({},state);
  res.CommonSymptoms = action.data;
  return res;
}

//清除常见症状
export const clearCommSymptoms = (state,action) => {
  const res = Object.assign({},state);
  res.CommonSymptoms = [];
  return res;
}

//获取大数据推送症状
export const getBigSymptom = (state,action) => {
  const res = Object.assign({},state);
  res.showDrop = !res.showDrop;
  res.bigDataSymptom = action.data;//推送数据
  let inlineDatas = res.data;
  const index = action.info.ikey;
  let labelInx = getLabelIndex(index);
  // 添加症状 暂时剔除没有id、questionMapping字段的数据
  let pushDataList = action.data;
  /*let pushDataList = action.data.filter(function(item){
    return item.id
  });*/
  let dataList = inlineDatas[labelInx].questionMapping;
  for(let k=0; k<dataList.length; k++){
    if(dataList[k].controlType == 99){
      dataList[k].questionDetailList = pushDataList;
    }
  }
  return res;
}

//点击插入新模板--添加病程变化
export const handleTailClick = (state,action)=>{
  const res = Object.assign({},state);
  const sonId = action.id;//子模板id
  let num = res.moduleNum.num;
  num++;
  // const addSmoduleData = JSON.parse(JSON.stringify(res.addSmoduleData));
  // const addDatas = JSON.parse(JSON.stringify(fullfillText(action.info).newArr));
  let addSmoduleData;
  // const allModules = JSON.parse(localStorage.getItem('modules'));
  let allModules = action.allModules;
  for(let k=0; k<allModules.length; k++){
    if(allModules[k].id==sonId){
      addSmoduleData = JSON.parse(JSON.stringify(allModules[k].moduleDetailDTOList));
    }
  }
  let datas = JSON.parse(JSON.stringify(res.data));
  let leng = datas.length;
  
  for(let i=0; i<addSmoduleData.length; i++){
    addSmoduleData[i].pos = num;
    // let pos = leng-1+i;
   /* let pos = leng-2+i;
    datas.splice(pos,0,addDatas[i]);*/
  }
  let newData = insertPro(datas,addSmoduleData);
  res.data = fullfillText(newData).newArr;
  res.saveText = fullfillText(res.data).saveText;
  res.moduleNum.num = num;
  res.showDrop = !res.showDrop;
  return res;
}


//将选择的常见症状插入
export const insertMain = (state,action) => {
  const res = Object.assign({},state);
  const items = action.item.select;
  const id = action.item.ids;
  let data = JSON.parse(JSON.stringify(res.moduleData));
  if(items.length>1){
    for(let i=1; i<items.length; i++){
      items[i].name = '、'+items[i].name;
      items[i].value = '、'+items[i].value;
    } 
  }
  let obj = [];
  for(let i=0; i<items.length; i++){
    obj.push({id:items[i].questionId,name:items[i].name,value:items[i].name,tagType:config.tagType,exist:1});
  }
  let inserIndx = null;
  data.map((it,i)=>{
      if(it.flag == 1){//在此处插入主诉数据
        inserIndx = i;
      }
    })
  // data.splice(inserIndx-1,1,...obj);
  if(data[inserIndx-1]&&data[inserIndx-1].tagType==8&& !data[inserIndx-1].name){
    data.splice(inserIndx-1,1,...obj);
  }else{
    data.splice(inserIndx-1,0,...obj);
  }
  
  // data[inserIndx-1] = text;//替换空标签
  res.data = data;
  res.saveText = [];//将手动输入的值清掉
  // res.saveText[inserIndx] = text.name;
  res.saveText = fullfillText(res.data).saveText;
  // res.mainIds.push(id);
  res.mainIds = res.mainIds.concat(id);
  res.editClear = false;//主诉框编辑状态
  res.update=Math.random();
  return res;
}


function getTextLabel(id,name,flag){
  // exist 主症状1，伴随2
  return {id:id,name:name,value:name,tagType:config.tagType,exist:flag};
}

function addPoint(data,num){//给伴随添加顿号
  for(let i=num; i<data.length; i++){
    data[i].name = '、'+ data[i].name;
  }
  return data;
}

//多选标签选中确定处理
export const confirm = (state,action) =>{
  const res = Object.assign({},state);
  let arr = res.data;
  let saveText = JSON.parse(JSON.stringify(res.saveText));
  const {exists,withs,ikey,withsName,existsName,ban} = action.data;
  let banText = JSON.stringify(ban) == "{}" ? '':{id:ban.id,name:ban.name,value:ban.name,tagType:config.tagType};
  let length = exists.length;
  let labelIndx = parseInt(ikey);//当前点击标签的index
  // 取第一个添加症状的主症状和伴随
  if(!arr[labelIndx].pos){
    let existsId = getIds(exists);
    let withsId = getIds(withs);
    res.mainIds = res.mainIds?res.mainIds.concat(existsId,withsId):[];
  }

  // 第二病程-伴位置处理
  let forwardId;
  let text;
  for(let n=labelIndx-1; n>=0; n--){
    // if(arr[n].id==8){
    if(arr[n].flag && arr[n].flag==1){//添加症状打标flag=1
      forwardId = n;
      break;
    }
  }
  if(forwardId){
    text = saveText.slice(forwardId,labelIndx);
    let index = text.indexOf('伴');
    let sliceArr = arr.slice(forwardId,labelIndx);
    let newSliceArr = JSON.parse(JSON.stringify(sliceArr));

    if(index==-1){//没有伴,直接插入ikey前
      if(exists.length > 0){
        let withsAddP = addPoint(exists,1);
        for(let i=0; i<withsAddP.length; i++){
          let pos = labelIndx + i;
          arr.splice(pos,0,getTextLabel(withsAddP[i].id,withsAddP[i].name,1));
          res.saveText[pos] = withsAddP[i].name;
        }
      }
      if(withs.length > 0){
        let withsAddP = addPoint(withs,1);
        let newWiths = JSON.parse(JSON.stringify(withsAddP));
        if(banText){
         newWiths.unshift(banText);
       }
       for(let j=0; j<newWiths.length; j++){
        let withsPos = labelIndx +length + j;
        arr.splice(withsPos,0,getTextLabel(newWiths[j].id,newWiths[j].name,2));
        res.saveText[withsPos] = newWiths[j].name;
      }
    }
    }else{//有伴
      if(exists.length > 0){
        // 判断前面是否已有主症状--是否加顿号、
        let existsAddP,startInd;
        for(let m=0; m<newSliceArr.length; m++){
          if(newSliceArr[m].exist && newSliceArr[m].exist==1){
            startInd = 0; 
            break;
          }else{
            startInd = 1;
          }
        }
        existsAddP = addPoint(exists,startInd);
        for(let i=0; i<existsAddP.length; i++){
          let pos = forwardId + index + i;
          arr.splice(pos,0,getTextLabel(existsAddP[i].id,existsAddP[i].name,1));
          res.saveText[pos] = existsAddP[i].name;
        }
      }
      
      if(withs.length>0){//有伴插到ikey前既可
        let withsAddP = addPoint(withs,0);     
        for(let j=0; j<withsAddP.length; j++){
          let withsPos = labelIndx +length + j;
          arr.splice(withsPos,0,getTextLabel(withsAddP[j].id,withsAddP[j].name,2));
          res.saveText[withsPos] = withsAddP[j].name;
        }
      }
    }
  }else{
    let text = saveText.slice(0,labelIndx);
    let index = text.indexOf('伴');
    if(index==-1){//没有伴,直接插入ikey前
      if(exists.length > 0){
        for(let i=0; i<exists.length; i++){
          let pos = labelIndx + i;
          arr.splice(pos,0,getTextLabel(exists[i].id,'、'+exists[i].name,1));
          res.saveText[pos] = exists[i].name;
        }
      }
      if(withs.length > 0){
        let withsAddP = addPoint(withs,1);
        let newWiths = JSON.parse(JSON.stringify(withsAddP));
        if(banText){
         newWiths.unshift(banText);
       }
       for(let j=0; j<newWiths.length; j++){
        let withsPos = labelIndx +length + j;
        arr.splice(withsPos,0,getTextLabel(newWiths[j].id,newWiths[j].name,2));
        res.saveText[withsPos] = newWiths[j].name;
      }
    }
    }else{//有伴
      if(exists.length > 0){
        for(let i=0; i<exists.length; i++){
          let pos = index + i;
          arr.splice(pos,0,getTextLabel(exists[i].id,'、'+exists[i].name,1));
          res.saveText[pos] = exists[i].name;
        }
      }
      
      if(withs.length>0){//有伴插到ikey前既可
        let withsAddP = addPoint(withs,0);     
        for(let j=0; j<withsAddP.length; j++){
          let withsPos = labelIndx +length + j;
          arr.splice(withsPos,0,getTextLabel(withsAddP[j].id,withsAddP[j].name,2));
          res.saveText[withsPos] = withsAddP[j].name;
        }
      }
    }
  }

    
    res.data = arr;
    res.saveText = fullfillText(arr).saveText;
    res.update=Math.random();
    return res;
  };


// 普通多选确定事件
export const commConfirm=(state,action)=>{
  const res = Object.assign({},state);
  const {labelInx,excluName,existsName,nones,withsName,ban} = action.data;
  // 若每个选项都有符号，去掉最后一个，因与标签间的符号有冲突
  let showText = (excluName||'')+(existsName||'')+(ban.name||'')+(withsName||'')+(nones&&nones.replace('、','')||'');
  let pattern = new RegExp(/\,+$|\，+$|\.+$|\。+$|\、+$/);//+ 一次或多次
  if(pattern.test(showText)){
    showText = showText.substr(0,showText.length-1);
  }
  res.data[labelInx].value = showText;
  res.saveText[labelInx] = showText;
  res.selecteds[labelInx] = action.data;
  res.update=Math.random();
  return res;
}


export const setSearch = (state,action)=>{
  const res = Object.assign({},state);
  res.showDrop = !res.showDrop;//防止不更新
  let searchData = action.data;
  res.searchDatas = searchData;
  res.searchStr = action.inpStr;
  return res;
}

//将选中的搜索结果插入
export const insertSearch = (state,action)=>{
  // 如果没有模板就插入模板，有模板则只插入所传文字
  const res = Object.assign({},state);
  let data = res.data;
  let moduleData = JSON.parse(JSON.stringify(res.moduleData));
  let searchData = action.info.name;
  const id = action.info.questionId;
  let focusIndex = res.focusIndex;
  let span = action.span;
  let text = {id:id,name:searchData,value:searchData,tagType:config.tagType};
  const emptySpan = JSON.parse(config.textLabel);
  if(data.length==0){   
    let inserIndx = null;
    moduleData.map((it,i)=>{
      if(it.flag == 1){//在此处插入主诉数据
        inserIndx = i;
      }
    })
    // flag=1前是文本标签就替换，否则插入
    // const iftext = moduleData[inserIndx-1].tagType;
    const iftext = inserIndx&&moduleData[inserIndx-1]&&moduleData[inserIndx-1].tagType;
    if(iftext==config.tagType){
       moduleData.splice(inserIndx-1,1,{id:id,name:searchData,value:searchData,tagType:config.tagType,exist:1});
     }else{
       moduleData.splice(inserIndx,0,{id:id,name:searchData,value:searchData,tagType:config.tagType,exist:1});
     }
    // moduleData.splice(inserIndx,0,{id:id,name:searchData,value:searchData,tagType:config.tagType,exist:1},emptySpan);   
    res.saveText = [];//将手动输入的值清掉
    res.data = moduleData;
    // res.saveText[inserIndx] = searchData;
    res.saveText = fullfillText(res.data).saveText;
    // res.saveText[inserIndx] = searchData;
    res.mainIds.push(id);
    res.editClear = false;//主诉框编辑状态
  }else{
    // 替换
    /*span.current.innerText = '';
    res.data.splice(focusIndex,1,text);
    res.saveText[focusIndex] = searchData;
    res.mainIds.push(id);*/
    // 追加--前or后？searchStr
    /*const searchStr = res.searchStr;
    let innerText = span.current.innerText;
    let index = innerText.indexOf(searchStr);
    let value;
    if(index <= 1){//前
      value = innerText.replace(searchStr,searchData);
    }else{
      let temp = innerText.substring(0,innerText.lastIndexOf(searchStr));
      value = temp + searchData;
    }
    // let value = innerText + searchData;
    span.current.innerText = value;
    res.data[focusIndex].value = value;
    res.saveText[focusIndex] = value;*/

    /*******************第一病程搜索结果也移植到现病史修改（2-18）********************/
    // 位置判断-第一病程-主症状还是伴随 前一个标签存在 并且没有pos；
    // 判断在伴的前面还是后面：前exist=1（主），后exist=2（伴）
    let nText = {};
    if(focusIndex==0){
      nText = Object.assign({},text,{exist:1});
    }else{
      if(!data[focusIndex-1].pos){//第一病程
        const preText = res.saveText.slice(0,focusIndex);
        const ind = preText.indexOf("伴");
        if(ind != -1){
          nText = Object.assign({},text,{exist:2});
        }else{
          nText = Object.assign({},text,{exist:1});
        }
      }else{//第二及以上病程
        nText = Object.assign({},text);
      }
    }
    

    // 从新插入一个span标签
    const searchStr = res.searchStr;
    let innerText = span.current.innerText || span.current.innerHTML;
    const value = innerText.replace(searchStr,"");
    let index = innerText.indexOf(searchStr);
    if(index < 1){//前
      if(data[focusIndex].value==searchStr){//为空标签则替换
        res.data.splice(focusIndex,1,nText);
        res.saveText.splice(focusIndex,1,searchData);
      }else{
        res.data.splice(focusIndex,0,nText);
        res.saveText.splice(focusIndex,0,searchData);
        res.data[focusIndex+1].value = value;
        res.saveText[focusIndex+1] = value;
      }  
    }else{
      res.data.splice(focusIndex+1,0,nText);
      res.saveText.splice(focusIndex+1,0,searchData);
      res.data[focusIndex].value = value;
    }
    span.current.innerText?(span.current.innerText = value):(span.current.innerHTML=value);
    res.mainIds.push(id);
  }
  // console.log(456,res,action);
  res.update=Math.random();
  return res;
}



//数字键盘选中事件
export function setNumberValue(state,action){
  let res = Object.assign({},state);
  const param = action.params;
  const index = param.ikey;
  let labelInx = getLabelIndex(index);
  const subInx = index.substr(index.length-1);
  let item = res.data[labelInx];
  // if(param.text.trim()){
    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] = sub.join(',');
      res.saveText[labelInx] = hasValue?sub.join(''):'';
      item.value = sub.join('');
    }
  /*}else{//删除完标签内容则删除该标签
    res.data.splice(labelInx,1);
    res.saveText = fullfillText(res.data).saveText;
  }*/
  res.update = Math.random();
  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] = sub.join(',');
    res.saveText[labelInx] = hasValue?sub.join(''):'';
    item.value = sub.join('');
  }
  res.update = Math.random();
  return res;
}

//单选带输入值保存
export const setRadioInputValue = (state,action)=>{
  const res = Object.assign({},state);
  const {ikey,values,id} = action.data;
  let index = getLabelIndex(ikey);
  let innerInx = ikey.substr(ikey.length-1);
  let item = res.data[index];
  if(item.tagType!=1){      //在组合项中
    item = res.data[index].questionMapping[innerInx];
  }
  let str='',temp='',obj=item.questionDetailList;
  if(!values){    //清空
    let sld=obj.find((item)=>{
      return item.selected==true;
    });
    sld?sld.selected=false:'';
    item.vals = null;
    item.value = '';
    if(res.data[index].tagType!=1){
      let hasValue = false;
      const sub = res.data[index].questionMapping.map((it)=>{
        if(it.value){     //至少有一个子值才黑显
          hasValue = true;
        }
        return (it.labelPrefix||'')+(it.value||'')+(it.labelSuffix||'');
      });
      res.saveText[index] = hasValue?sub.join(''):'';
    }else{
      res.saveText[index] = '';
    }
    res.update = Math.random();
    return res;
  }
  for(let i in values){
    temp = values[i];
    if(typeof temp=='object'){
      str+=temp.value;
    }else{
      str+=temp;
    }
  }
  //选中状态
  if(id){
    obj.map((its)=>{
      if(its.id === id){
        its.selected = true;
      }else{
        its.selected = false;
      }
    });
  }
  item.vals = values;
  item.value = str;
  if(res.data[index].tagType!=1){
    let hasValue = false;
    const sub = res.data[index].questionMapping.map((it)=>{
      return (it.labelPrefix||'')+(it.value||'')+(it.labelSuffix||'');
    });
    res.saveText[index] = sub.join('');
  }else {
    res.saveText[ikey] = str;
  }
  res.update = Math.random();
  return res;
}
//双击标签输入改变值
export const changeLabelVal = (state,action)=>{
  const res = Object.assign({},state);
  const {changeVal,totalVal,ikey,prefix,suffix} = action.data;
  const index = ikey;
  const newVal = changeVal;
  let item = res.data[index];
  const next = res.data[+index+1];
  const nextVal = next.value||next.name;
  //标签后是不是标点符号标签,是的话删除本标签时一起删除
  let nextIsDot = +next.tagType===8&&!nextVal.match(config.punctuationReg);
  let labText = totalVal?totalVal:newVal;
  if(labText.trim()){
    if(item){
      item.value = newVal;
      item.labelPrefix = prefix||'';
      item.labelSuffix = suffix||'';
      res.saveText[index] = labText;
    }
  }else{//删除完标签内容则删除该标签
    const num = nextIsDot?2:1;
    res.data.splice(index,num);
    res.selecteds.splice(index,num);//杂音类样式选中状态对应
    res.saveText = fullfillText(res.data).saveText;
  }
  res.update = Math.random();//console.log(222,res);
  return res;
}

// 数字键盘较特殊，有直接输入
export const changeNumLabelVal = (state,action)=>{
  const res = Object.assign({},state);
  const {changeVal,totalVal,ikey,prefix,suffix} = action.data;
  const index = ikey;
  const newVal = changeVal;
  let labText = totalVal?totalVal:newVal;   //如单选没有前后缀
  let item = res.data[index];
  const next = res.data[+index+1];
  const nextVal = next.value||next.name;
  //标签后是不是标点符号标签,是的话删除本标签时一起删除
  let nextIsDot = +next.tagType===8&&!nextVal.match(config.punctuationReg);
  if(labText.trim()){
    if(item){
      item.value = newVal;
      item.labelPrefix = prefix||'';
      item.labelSuffix = suffix||'';
      res.saveText[index] = labText;
    }
  }else{//删除完标签内容则删除该标签
    const num = nextIsDot?2:1;
    res.data.splice(index,num);
    res.saveText = fullfillText(res.data).saveText;
  }
  res.update = Math.random();//console.log(44,action,res);
  return res;
}

// 保存自由输入文本--无模板
export const saveFreeVal = (state,action)=>{
  const res = Object.assign({},state);
  let data = action.data;
  res.saveText[0] = data;
  /*if(data.trim()){
   res.saveText[0] = data;
  }else{
    res.saveText = [];
  }*/
  res.update = Math.random();
  return res;
}


export const clearMainSuit = (state,action)=>{//回读和清除
  const res = Object.assign({},state);
  res.data = action.data;
  res.saveText = action.saveText;
  // res.saveText = fullfillText(action.data).saveText;//解决删除data里的值保存，回读后页面上有内容saveText里没有的情况
  res.selecteds = action.selecteds?action.selecteds:[];
  res.mainIds = action.mainIds;
  res.chronicDesease = action.chronicDesease;//慢病疾病字段
  if(action.editClear){
    res.editClear = action.editClear;
  }
  if(res.data.length > 0){//回读
    res.editClear = false;
  }
  if(action.symptomFeature !== undefined){//清除分词数据
    res.symptomFeature.featureData = action.symptomFeature;
  }
  // console.log(res,action);
  return res;
}


//文本模式下值保存
export const setTextModeValue = (state,action)=>{
  const res = Object.assign({},state);
  res.saveText[0] = action.text;
  return res;
};


//自由文本
export function setCheckText(state,action) {
  let res = Object.assign({},state);
  const {i,text} = action;
  res.data[i].value=text;
  res.saveText[i] = text;
  /*if(res.data[i]){
    res.data[i].value=text;
  }
  res.saveText[i] = text;*/
  res.update = Math.random();
  return res;
}
//复制标签（如血压）事件
export function addLabelItem(state,action){
  let res = Object.assign({},state);
  const {data,i} = action;
  const textLabel = Object.assign({},JSON.parse(config._textLabel),{showInCheck:JSON.parse(data).showInCheck});
  //使用Object.assign({},data)拷贝操作时复制项和原项会同步修改
  if(!data) return res;
  res.data.splice(+i+2,0,JSON.parse(data),textLabel);
  res.saveText.splice(+i+2,0,'','');
  res.selecteds.splice(+i+2,0,null,null);
  res.update = Math.random();
  return res;
}

//文本输入标签
export function setInputLabel(state,action){
  let res = Object.assign({},state);//console.log(state,action)
  const {i,text,prefix,suffix,subIndex} = action;
  const item = res.data[i];
  if(+item.tagType===3){      //multSpred标签
    item.questionMapping[subIndex].value = text;
    let texts = item.questionMapping.map((it)=>{
      return it.value?(it.labelPrefix||'')+(it.value||'')+(it.labelSuffix||''):'';
    });
    res.saveText[i] = texts.join('');
    res.update = Math.random();
    return res;
  }else{
    if(item){
      item.value=text;
    }
  }
  res.saveText[i] = prefix+text+suffix;//console.log(res)
  res.update = Math.random();
  return res;
}

// backspace删除
export function backspaceText(state,action){
  let res = Object.assign({},state);
  const {delIndex} = action;
  const data = res.data;
  if(data[delIndex-1].flag&&data[delIndex-1].flag==3){
  // if(data[delIndex-1].tagType==8 ||data[delIndex-1].flag&&data[delIndex-1].flag==3){
    // 前一个是文本标签或者子模板 不做处理
  }else if(data[delIndex-1].tagType==8){
    data.splice(delIndex,1);
    res.saveText.splice(delIndex,1);
  }
  else{
    data.splice(delIndex-1,2);
    res.saveText.splice(delIndex-1,2);
  }
  // res.saveText = fullfillText(data).saveText;
  res.update = Math.random();
  return res;
}

//删除后移除id
export function removeId(state,action){
  let res = Object.assign({},state);
  const {index,text} = action;
  const data = res.data;
  const id = data[index].id;
  let ids = res.mainIds;
  if(ids.includes(id)){
    ids.splice(ids.indexOf(id),1);
  }
  if(!data[index].value && data[index-1].tagType==8){
    data.splice(index,1);
    res.saveText.splice(index,1);
  }else{
    data[index].id = "";
    data[index].name = text;
    data[index].value = text;
  }
  res.update = Math.random();
  return res;
}

// 单列多选
export function multipleComfirn(state,action){
  let res = Object.assign({},state);
  const {ikey,seleData} = action.data;
  let data = res.data;
  data[ikey].value = seleData;
  res.saveText[ikey] = seleData;
  res.selecteds[ikey] = action.data;
  res.update = Math.random();
  return res;
}

export function delSingleLable(state,action){
  let res = Object.assign({},state);
  const {index} = action;
  let data = res.data;
  // 前一个不是文本标签 并且不是子模板，则删除
  if(data[index-1].flag&&data[index-1].flag==3){

  }
  else if(data[index-1].tagType != 8){
    data.splice(index-1,1);
    res.saveText.splice(index-1,1);
  }
  res.update = Math.random();
  return res;
}


export function getSymptomFeature(state,action){
  let res = Object.assign({},state);
  const {data} = action;
  if(data && data.length>0){
    for(let i=0; i<data.length; i++){
      data[i].tagType = '8';
      res.mainIds.push(data[i].id);
    }
    res.symptomFeature.featureData = data;
    res.symptomFeature.refresh = !res.symptomFeature.refresh;
  }
  res.update = Math.random();
  return res;
}