import {getLabelIndex,fullfillText,getIds,resetTagtype,storageLocal,handleLocalDelTag,shiftLocalDelTag} from '@utils/tools.js';
import config from '@config/index.js';
import {Notify} from '@commonComp';

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);
  // const originalData = JSON.parse(JSON.stringify(action.data));
  const originalData = action.data||[];
  const originalSonData = action.addSymptom||[];
  // 处理tagType=4的类型
  let spreadLabels=resetTagtype(originalData);
  let sonSpreadLabels=resetTagtype(originalSonData);//子模板
  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);
  let num = res.moduleNum.num;
  num++;
  // 有回读的子模板，则优先用回读的
  let addSmoduleData = [];
  if(res.mainReadSonM.length>0){
    addSmoduleData = JSON.parse(JSON.stringify(res.mainReadSonM))
  }else{
    addSmoduleData = res.addSmoduleData.length>0?JSON.parse(JSON.stringify(res.addSmoduleData)):[];
  }
  if(addSmoduleData.length==0){//未匹配到子模板
    Notify.info("未找到相关内容");
    return
  }
  let datas = JSON.parse(JSON.stringify(res.data));
  
  for(let i=0; i<addSmoduleData.length; i++){
    addSmoduleData[i].pos = num;
  }
  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;
  const conceptId = action.item.conceptId;
  let data = JSON.parse(JSON.stringify(res.moduleData));
  // 存到本地
  // >5直接截取替换，<5需要判断是否有重复的--双重遍历
  const mainSymp = storageLocal.get('mainSymp');
  if(items.length<5){
    if(mainSymp){
      let localArr = JSON.parse(mainSymp);
      let sympArr = JSON.parse(JSON.stringify(localArr));
      let num = -1;
      for(let k=0; k<localArr.length; k++){//判断是否已存在
        for(let j=0; j<items.length; j++){
          if(localArr[k].conceptId==items[j].conceptId){
            num++
            sympArr.splice(k-num,1);
          }
        }
      }
      let newArr = sympArr.concat(items);
      if(newArr.length>5){
        storageLocal.set('mainSymp',newArr.slice(newArr.length-5,));
      }else{
        storageLocal.set('mainSymp',newArr);
      }
    }else{
      storageLocal.set('mainSymp',items);
    }
  }else{
    let seleArr = items.slice(items.length-5,);//展示后5个
    storageLocal.set('mainSymp',seleArr);
  }
    
  if(data && data.length>0){

    if(items.length>1){
      for(let i=1; i<items.length; i++){
        items[i].name = '、'+items[i].name;
        // items[i].value = '、'+items[i].value;
        items[i].value = items[i].value?'、'+items[i].value:items[i].name;
      }
    }
    let obj = [];
    for(let i=0; i<items.length; i++){
      obj.push({id:items[i].questionId||items[i].id,name:items[i].name,value:items[i].name,tagType:config.tagType,exist:1,conceptId:items[i].conceptId});
    }
    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 = res.mainIds.concat(conceptId);
    res.mainTailIds = res.mainTailIds.concat(id);
    res.editClear = false;//主诉框编辑状态
  }else{//慢病模式下重新点选
    let tempTxt = "";
    if(items.length>0){
      for(let k=0; k<items.length; k++){
        tempTxt += items[k].name + '，';
      } 
    }
    res.saveText[0] = tempTxt.substring(0,tempTxt.length-1);
  }
  res.update=Math.random();
  return res;
}


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

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;
    // 存到本地
    // >5直接截取替换，<5需要判断是否有重复的--双重遍历
    const items = exists.concat(withs);
    const mainSymp = storageLocal.get('mainSymp');
    if(items.length<5){
      if(mainSymp){
        let localArr = JSON.parse(mainSymp);
        let sympArr = JSON.parse(JSON.stringify(localArr));
        let num = -1;
        for(let k=0; k<localArr.length; k++){//判断是否已存在
          for(let j=0; j<items.length; j++){
            items[j].exist = 1;
            if(localArr[k].conceptId==items[j].conceptId){
              num++
              sympArr.splice(k-num,1);
            }
          }
        }
        let newArr = sympArr.concat(items);
        if(newArr.length>5){
          storageLocal.set('mainSymp',newArr.slice(newArr.length-5,));
        }else{
          storageLocal.set('mainSymp',newArr);
        }

      }else{
        storageLocal.set('mainSymp',items);
      }
    }else{
      // let seleArr = items.slice(0,5);
      let seleArr = items.slice(items.length-5,);//展示后5个
      for(let n=0; n<seleArr.length; n++){
        seleArr[n].exist = 1;//将伴随都改成主症状
      }
      storageLocal.set('mainSymp',seleArr);
    }

  let banText = JSON.stringify(ban) == "{}" ? '':{id:ban.id,name:ban.name,value:ban.name,tagType:config.tagType};
  let length = exists.length;
  let existConpId = [];
  let withConpId = [];
  exists.map((v,i)=>{
    existConpId.push(v.conceptId);
  })
  withs.map((v,i)=>{
    withConpId.push(v.conceptId);
  })
  let labelIndx = parseInt(ikey);//当前点击标签的index
  // 取第一个添加症状的主症状和伴随
  if(!arr[labelIndx].pos){
    let existsId = getIds(exists);
    let withsId = getIds(withs);
    res.mainIds = res.mainIds?res.mainIds.concat(existConpId,withConpId):[];
    res.mainTailIds = res.mainTailIds?res.mainTailIds.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,withsAddP[i].conceptId));
          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,newWiths[j].conceptId));
        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,existsAddP[i].conceptId));
          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,withsAddP[j].conceptId));
          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,exists[i].conceptId));
          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,newWiths[j].conceptId));
        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,exists[i].conceptId));
          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,withsAddP[j].conceptId));
          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.data[labelInx].selecteds = action.data;
  res.saveText[labelInx] = showText;
  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;
  res.isEnd = action.isEnd;
  return res;
}
// 主诉-添加症状-选中搜索结果
export const chooseSearch = (state,action)=>{
  const res = Object.assign({},state);
  const {item,cliIndex,ban} = action.info;
  const index = parseInt(cliIndex);
  let data = res.data;
  let text = {id:item.questionId,name:item.name,value:item.name,tagType:config.tagType,conceptId:item.conceptId};
  // 判断index前是saveText中是否有伴
  let nText = {};
  if(!data[index].pos){//第一病程
    const preText = res.saveText.slice(0,index);
    const ind = preText.indexOf("伴");
    if(ind != -1 || ban){
      nText = Object.assign({},text,{exist:2});
    }else{
      nText = Object.assign({},text,{exist:1});
    }
  }else{//第二及以上病程
    nText = Object.assign({},text);
  }

  // 缓存到localStorage中
  const mainSymp = storageLocal.get('mainSymp');
  if(mainSymp){
    let localArr = JSON.parse(mainSymp);
    let sympArr = JSON.parse(JSON.stringify(localArr));
    for(let k=0; k<localArr.length; k++){//判断是否已存在
      if(localArr[k].conceptId==item.conceptId){
        sympArr.splice(k,1);
      }
    }
    sympArr.push(nText);
    if(sympArr.length>5){
      storageLocal.set('mainSymp',sympArr.slice(sympArr.length-5,));
    }else{
      storageLocal.set('mainSymp',sympArr);
    }
  }
  // 插入data中
  res.data.splice(index,0,Object.assign({},nText,{name:'、'+item.name,value:'、'+item.name}));
  res.saveText.splice(index,0,'、'+item.name);
  res.mainIds.push(item.conceptId);
  if(item.questionId){
    res.mainTailIds.push(item.questionId);
  }
  res.update=Math.random();
  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;//获取尾巴id
  const ids = action.info.conceptId;//去重id
  let focusIndex = res.focusIndex;
  let span = action.span;
  let text = {id:id,name:searchData,value:searchData,tagType:config.tagType,conceptId:ids};
  const emptySpan = JSON.parse(config.textLabel);
if(moduleData && moduleData.length>0){
    // 存到本地
    const mainSymp = storageLocal.get('mainSymp');
    let items = JSON.parse(JSON.stringify(action.info));
    if(mainSymp){
      let localArr = JSON.parse(mainSymp);
      let sympArr = JSON.parse(JSON.stringify(localArr));
      for(let k=0; k<localArr.length; k++){//判断是否已存在
        if(localArr[k].conceptId==items.conceptId){
          sympArr.splice(k,1);
        }
      }
      sympArr.push(items);
      if(sympArr.length>5){
        storageLocal.set('mainSymp',sympArr.slice(sympArr.length-5,));
      }else{
        storageLocal.set('mainSymp',sympArr);
      }
    }else{
      let arr = [];
      arr.push(items);
      storageLocal.set('mainSymp',arr);
    }

  if(data.length==0){
    let inserIndx = null;
    moduleData.map((it,i)=>{
      if(it.flag == 1){//在此处插入主诉数据
        inserIndx = i;
      }
    })
     // flag=1前是文本标签就替换，否则插入
    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,conceptId:ids});
     }else{
       moduleData.splice(inserIndx,0,{id:id,name:searchData,value:searchData,tagType:config.tagType,exist:1,conceptId:ids});
     }  
    res.saveText = [];//将手动输入的值清掉
    res.data = moduleData;
    res.saveText = fullfillText(res.data).saveText;
    res.mainIds.push(ids);
    if(id){
      res.mainTailIds.push(id);
    }
    res.editClear = false;//主诉框编辑状态
    
  }else{
    /*******************第一病程搜索结果也移植到现病史修改（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;
    const isEnd = res.isEnd;
    let innerText = span.current.innerText || span.current.innerHTML;
    if(!isEnd){//前
      const reg = new RegExp("^"+searchStr)
      const value = innerText.replace(reg,"");
      res.data.splice(focusIndex,0,nText);
      res.saveText.splice(focusIndex,0,searchData);
      res.data[focusIndex+1].value = value;
      res.saveText[focusIndex+1] = value;
      span.current.innerText?(span.current.innerText = value):(span.current.innerHTML=value); 
    }else{
      const reg = new RegExp(searchStr+"$")
      const value = innerText.replace(reg,"");
      if(data[focusIndex].value.trim()==searchStr){//为空标签则替换-否则会多一个空标签
        res.data.splice(focusIndex,1,nText);
        res.saveText.splice(focusIndex,1,searchData);
      }else{
        res.data.splice(focusIndex+1,0,nText);
        res.saveText.splice(focusIndex+1,0,searchData);
        res.data[focusIndex].value = value;
        res.saveText[focusIndex] = value;
      }
      span.current.innerText?(span.current.innerText = value):(span.current.innerHTML=value);
    } 
    res.mainIds.push(ids);
    if(id){
      res.mainTailIds.push(id);
    }
  }
  }else{
    res.saveText[0] = searchData;
  }
  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;
    handleLocalDelTag('1',index,res.data[index]);
    res.data.splice(index,num);
    res.saveText = fullfillText(res.data).saveText;
  }
  res.update = Math.random();//console.log(222,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.mainIds = action.mainIds;
  res.mainTailIds = action.mainTailIds || [];
  res.chronicDesease = action.chronicDesease;//慢病疾病字段
  res.mainReadSonM = action.mainReadSonM || [];//子模板
  res.moduleNum.num = 0; //恢复子模板点击次数
  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.update = Math.random();
  return res;
}

// backspace删除
export function backspaceText(state,action){
  let res = Object.assign({},state);
  const {delIndex,flag} = action;
  const data = res.data;
  if(flag == 'backsp'){
    if(data[delIndex-1].flag&&data[delIndex-1].flag==3){
      // 前一个是文本标签或者子模板 不做处理
    }else if(data[delIndex-1]&&data[delIndex-1].tagType==8){
      data.splice(delIndex,1);
      res.saveText.splice(delIndex,1);
    }
    else{
      handleLocalDelTag(1,delIndex-1,data[delIndex-1]);
      data.splice(delIndex-1,action.text.length>0&&(config.punctuationReg.test(action.text))?1:2);//data不为空不能清除
      res.saveText.splice(delIndex-1,action.text.length>0?1:2);
    }
  }else if(flag == 'del'){
    if(data[delIndex+1] && data[delIndex+1].flag&&data[delIndex+1].flag==3){
      // 后一个是文本标签或者子模板 不做处理

    }else if(data[delIndex+1] && data[delIndex+1].tagType==8){
      data.splice(delIndex,1);
      res.saveText.splice(delIndex,1);
    }
    else if(!data[delIndex+1] && data[delIndex-1] && data[delIndex-1].tagType !=8){//最后一个文本标签
      console.log("删除最后一个啦")
    }
    else{
      handleLocalDelTag(1,delIndex,data[delIndex+1]);
      data.splice(delIndex,2);
      res.saveText.splice(delIndex,2);
    }
  }
  res.update = Math.random();
  return res;
}

//删除后移除id
export function removeId(state,action){
  let res = Object.assign({},state);
  const {index,text,flag} = action;
  const data = res.data;
  // 获取尾巴id-questionId
  const id = data[index].id;
  let tailIds = res.mainTailIds;
  if(tailIds.includes(id)){
    tailIds.splice(tailIds.indexOf(id),1);
  }
  // 去重id
  const copid = data[index].conceptId;
  let ids = res.mainIds;
  if(ids.includes(copid)){
    ids.splice(ids.indexOf(copid),1);
  }
  if(flag == 'del'){
    if(!data[index].value && data[index+1] &&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;
    }
  }else{
    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;
  data[ikey].selecteds = action.data;
  res.saveText[ikey] = seleData;
  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].conceptId);
      if(data[i].id){//questionId有可能为null
        res.mainTailIds.push(data[i].id);
      } 
    }
    res.symptomFeature.featureData = data;
    res.symptomFeature.refresh = !res.symptomFeature.refresh;
  }
  res.update = Math.random();
  return res;
}


// 引用回显数据 
export function reMainsuitData(state, action) {
   let res = Object.assign({}, state);
   res.saveText = JSON.parse(action.params[0].content);
   return res
}
