import {
  RECOVER_TAG_MAIN,
  COMM_SYMPTOMS,
  CLEAR_COMSYMPTOMS,
  SHOW_TAIL,
  INSERT_MAIN,
  SET_SEARCH,
  CLEAR_SEARCH,
  GET_BIGDATAPUSH,
  SET_MAINSUIT,
  MIX_CONFIRM,
  NUMBER_SELECT,
  RADIO_SELECT,
  COMM_CONFIRM,
  CHANGE_LABELVAL,
  SAVE_FREE,
  CLEAR_MAIN_SUIT,
  SET_DATA,
  INSERT_SEARCH,
  MAIN_FOCUS_INDEX,
  SETTEXTMODEVALUE,
  SETMAINTEXT,
  MAINADDLABELITEM,
  SETMAININPUT,
  DEL_MAIN,
  REMOVE_MAIN_ID,
  MAINSUIT_MUL,
  DEL_MAIN_LABLE,
  SET_FEATURE,
  SET_MS_RADIO_INPUT_VAL,
  SAVE_CHRONIC,
  MAIN_REMOVE_SPAN,
  SET_ADD_SEARCH,
  CLEAR_ADD_SEARCH,
  DELETE_MAIN_SELECTED_TAGS,
  REMAINSUITDATA,
  CLEARMAINSUITDATA,
} from '../types/mainSuit';
import {
  getCommSymptoms,
  handleTailClick,
  insertMain,
  setSearch,
  getBigSymptom,
  setMainMoudle,
  confirm,
  commConfirm,
  changeLabelVal,
  saveFreeVal,
  clearMainSuit,
  insertSearch,
  setTextModeValue,
  backspaceText,
  removeId,
  multipleComfirn,
  delSingleLable,
  getSymptomFeature,
  reMainsuitData,
} from '../actions/mainSuit';
import {recoveTag,setRadioInputValue,setNumberValue,setRadioValue,setCheckText,addLabelItem,setInputLabel,deleteSelectedLabels} from '@utils/utils';

const initState = {
  showDrop:false,
  CommonSymptoms:[],//常见症状
  searchDatas:[],//搜索结果
  bigDataSymptom:[],//大数据推送症状
  moduleData:[],//主诉模板
  addSmoduleData:[],//添加病情变化模板
  data:[],//主诉使用的模板数据
  saveText:[],//主诉框内的值
  moduleNum:{num:0},//点了几次添加病情变化
  focusIndex:'',
  mainIds:[], //选中的主症状和伴随的id(搜索去重)
  editClear:true,
  symptomFeature:{//分词
    featureData:[],
    refresh:false //用于数据更新
  },
  chronicDesease:null, //慢病
  mainTailIds:[],  //获取症状尾巴用
  mainReadSonM:[], //回读的子模板
  addSearchData:[] //添加症状里的搜索
}

export default function(state=initState,action){
  const res = Object.assign({},state);
  switch (action.type) {
    case SET_MAINSUIT: //主诉模板
      return setMainMoudle(state, action);
    case SET_DATA: //主诉使用模板
      // res.data = res.moduleData.concat();
      res.data = JSON.parse(JSON.stringify(res.moduleData));
      res.update = Math.random();
      return res;
    // return setMainData(state,action);
    case COMM_SYMPTOMS:
      return getCommSymptoms(state, action);
    case CLEAR_COMSYMPTOMS: //清空常见症状
      // return clearCommSymptoms(state,action);
      res.CommonSymptoms = [];
      return res;
    case GET_BIGDATAPUSH: //大数据推送症状
      return getBigSymptom(state, action);
    case SHOW_TAIL: //显示标签尾巴
      return handleTailClick(state, action);
    case INSERT_MAIN: //插入选中的常见症状
      return insertMain(state, action);
    case COMM_CONFIRM: //普通多选框确定事件
      return commConfirm(state, action);
    case MIX_CONFIRM: //有伴 多选框确定事件
      return confirm(state, action);
    case SET_SEARCH:
      return setSearch(state, action);
    case INSERT_SEARCH:
      return insertSearch(state, action);
    case CLEAR_SEARCH: //清空搜索结果
      res.searchDatas = [];
      return res;
    case NUMBER_SELECT: //数字键盘选中的值处理
      return setNumberValue(state, action);
    case RADIO_SELECT: //单选选中的值处理
      return setRadioValue(state, action);
    case CHANGE_LABELVAL: //改变标签值
      return changeLabelVal(state, action);
    case SAVE_FREE: //自由输入
      return saveFreeVal(state, action);
    case MAIN_FOCUS_INDEX:
      res.focusIndex = action.index;
      res.span = action.$span;
      return res;
    case CLEAR_MAIN_SUIT: //清除数据
      return clearMainSuit(state, action);
    case SETTEXTMODEVALUE:
      return setTextModeValue(state, action);
    case SETMAINTEXT:
      return setCheckText(state, action);
    case MAINADDLABELITEM:
      return addLabelItem(state, action);
    case SETMAININPUT:
      return setInputLabel(state, action);
    case DEL_MAIN:
      return backspaceText(state, action);
    case REMOVE_MAIN_ID:
      return removeId(state, action);
    case MAINSUIT_MUL: //单列多选
      return multipleComfirn(state, action);
    case DEL_MAIN_LABLE: //单独删除标签
      return delSingleLable(state, action);
    case SET_FEATURE: //主诉分词
      return getSymptomFeature(state, action);
    case SET_MS_RADIO_INPUT_VAL:
      return setRadioInputValue(state, action);
    case SAVE_CHRONIC: //存慢病信息，说明走慢病流程，慢病没有主诉模板，故要清掉
      res.chronicDesease = action.data;
      res.moduleData = [];
      res.addSmoduleData = [];
      return res;
    case RECOVER_TAG_MAIN:
      return recoveTag(state, action);
    case MAIN_REMOVE_SPAN:
      // 只剩最后一个空标签时才移除
      if (res.data.length == 1) {
        res.data = [];
        res.saveText = [];
        res.mainIds = [];
        res.editClear = true;
      }
      return res;
    case SET_ADD_SEARCH: //添加症状-搜索
      res.addSearchData = action.data;
      return res;
    case CLEAR_ADD_SEARCH: //清空症状搜索结果
      //非必要是不要重复赋值[],造成searchBox componentWillReceiveProps循环调用导致内存泄漏
      if (res.addSearchData && res.addSearchData.length > 0) {
        res.addSearchData = [];
      }
      return res;
    case DELETE_MAIN_SELECTED_TAGS:
      return deleteSelectedLabels(state, action);
    // 回显主述
    case REMAINSUITDATA:
      return reMainsuitData(state, action);
    case CLEARMAINSUITDATA:
      res.saveText = [];
      return res;
    default:
      return state;
  }
}