import {
  GET_ASSIST_SEARCH_LIST,
  GET_ASSIST_LABEL,
  DEL_ASSIST_LABEL,
  CHANGE_ASSIST_VAL,
  CHANGE_DATE,
  CLEAR_ASSIST_DATA,
  ADD_ASSIST_LABEL
} from '../types/assistCheck';
import store from '@store';
const initSearchList = {
  list: [], //点击的结果
  assistLabel: [], //搜索的结果
  dataString: '', //结果拼接
  assistVal: '',

  hospitalPac: [], //医院检索到的
  hospitalPacObj: {}, //组对应的明细（单选多选全选）
  selectGroupList: [], //选的组的明细可能有多个组
  allCheck: false, //全选反选
  checkedList: [], //选中的小项
  checkedListImport: [], //辅检导入
  allCheckLis:[],//所有导入数据
  msgObj:{
    name:'',
    patientNum:''
  }
}
import { getCurrentDate,getAllString} from '@utils/tools';

export default (state = initSearchList, action) => {
  if (action.type == ADD_ASSIST_LABEL) {
    const newState = Object.assign({}, state);
    let tempArr = newState.assistLabel;
    for (let i = 0; i < action.lis.length; i++) {
      tempArr.push(action.lis[i]);
    }
    for (let i = 0; i < tempArr.length; i++) {
      if (i == action.idx) {
        tempArr[i].time = action.date
        newState.assistLabel = [...tempArr]
      }
    }
    newState.assistLabel = [...tempArr]
    newState.dataString = getAllString(newState.checkedListImport,newState.assistLabel)
    return newState;
  }
  if (action.type == GET_ASSIST_SEARCH_LIST) { //右侧推送添加到左侧
    const newState = Object.assign({}, state);
    newState.list = action.list
    newState.assistVal = action.val
    newState.dataString = getAllString(newState.checkedListImport,newState.assistLabel)
    return newState;
  }
  if (action.type == GET_ASSIST_LABEL) { //默认
    const newState = Object.assign({}, state);
    const tempArrs = newState.assistLabel;
    let tempArr = [];
    let tmpCommonLis = store.getState().homePage.assistList;
    if (action.sign == 'common') {
      let tmpAssistList = JSON.parse(JSON.stringify(tmpCommonLis))
      tempArr = tmpAssistList;
    } else {
      tempArr = newState.list
    }
    for (let i = 0; i < tempArr.length; i++) {
      if (tempArr[i].conceptId == action.id && i == action.idx) {
        tempArr[i].time = getCurrentDate(1);
        tempArrs.push(tempArr[i]);
        newState.assistLabel = [...tempArrs];
      }
    }
    newState.dataString = getAllString(newState.checkedListImport,newState.assistLabel)
    return newState;
  }
  if (action.type == DEL_ASSIST_LABEL) { //删除
    const newState = Object.assign({}, state);
    let tempArr = newState.assistLabel,tempArrs = [];
    let tmpImportLis = newState.checkedListImport;
    if(action.flg == 0){//删除辅检导入
      tmpImportLis.splice(action.idx,1)
    }else{
      for (let k = 0; k < tempArr.length; k++) {
        if (k != action.idx) {
          tempArrs.push(tempArr[k])
        }
      }
      newState.assistLabel = [...tempArrs]
    }
    newState.dataString = getAllString(newState.checkedListImport,newState.assistLabel)
    return newState;
  }
  if (action.type == CHANGE_ASSIST_VAL) { //改变输入值
    const newState = Object.assign({}, state);
    const tempArr = newState.assistLabel;
    for (let i = 0; i < tempArr.length; i++) {
      if (i == action.idx) {
        tempArr[i].value = action.val
        newState.assistLabel = [...tempArr]
      }
    }
    newState.dataString = getAllString(newState.checkedListImport,newState.assistLabel)
    return newState;
  }
  if (action.type == CHANGE_DATE) { //新增
    const newState = Object.assign({}, state);
    const tempArr = newState.assistLabel;
    for (let i = 0; i < tempArr.length; i++) {
      if (i == action.idx) {
        tempArr[i].time = action.date
        newState.assistLabel = [...tempArr]
      }
    }
    newState.dataString = getAllString(newState.checkedListImport,newState.assistLabel)
    return newState;
  }
  if (action.type == CLEAR_ASSIST_DATA) {
    const newState = Object.assign({}, state);
    newState.assistLabel = [...action.data];
    newState.dataString = action.saveText;
    newState.checkedListImport = action.checkedListImport;
    return newState;
  }
  return state;
}
