import {
  DEL_ITEMS,
  INIT_ITEMS,
  TAB_CHANGE,
  CKECK_ITEMS,
  BATCH_DEL_ITEMS,
  ALL_CHECKED,
  CHANGE_TITLE,
  CHANGE_VISIBLE,
  SHOW_MESSAGE,
  KEEP_PUSH_DATA,
  ALL_CHECKED_SHOW,
} from '../types/tabTemplate';


const initDataList = {
  activeId: '0', //点击的tab
  visible: false, //弹窗
  showMsg: { //提示
    show: false,
    content: '',
    type: ''
  },
  items: [],
  allChecked: false, //全选反选
  checkItems: [], //选中要删除的元素
  activeItem: {}, //引用的模板
  activeItemHis: {}, //引用的历史病例
  allCheckShow: false, //全选反选是否显示
}
export default (state = initDataList, action) => {
  if (action.type === DEL_ITEMS) {
    const newState = Object.assign({}, state);
    let tempArr = newState.items;
    tempArr.splice(tempArr.findIndex(item => item.id == action.id), 1)
    newState.items = [...tempArr];
    return newState;
  }
  if (action.type === BATCH_DEL_ITEMS) {
    const newState = Object.assign({}, state);
    let tempArr = newState.items;
    for (let i = 0; i < action.ids.length; i++) {
      let currentId = action.ids[i];
      tempArr.splice(tempArr.findIndex(item => item.id == currentId), 1)
    }
    newState.items = [...tempArr];
    newState.checkItems = [];
    return newState;
  }
  if (action.type === INIT_ITEMS) {
    const newState = Object.assign({}, state);
    newState.items = action.state.data;
    return newState;
  }
  if (action.type === CHANGE_VISIBLE) {
    const newState = Object.assign({}, state);
    newState.visible = action.bool;
    return newState;
  }
  if (action.type === SHOW_MESSAGE) {
    const newState = Object.assign({}, state);
    newState.showMsg = action.bool;
    return newState;
  }
  if (action.type === TAB_CHANGE) {
    const newState = Object.assign({}, state);
    newState.activeId = action.idx;
    return newState;
  }
  if (action.type === CHANGE_TITLE) {
    const newState = Object.assign({}, state);
    let tempArr = newState.items;
    console.log(action)
    for (let i = 0; i < tempArr.length; i++) {
      if (tempArr[i].id == action.obj.id) {
        tempArr[i].name = action.obj.title
      }
    }
    newState.items = [...tempArr];
    return newState;
  }
  if (action.type === CKECK_ITEMS) {
    const newState = Object.assign({}, state);
    if (newState.checkItems.indexOf(action.id) == -1) {
      let tempArr = newState.checkItems;
      tempArr.push(action.id);
      newState.checkItems = [...tempArr]
    } else {
      let tempArr = newState.checkItems;
      tempArr.splice(tempArr.findIndex(item => item === action.id), 1)
      newState.checkItems = [...tempArr]
    }
    return newState;
  }
  if (action.type === KEEP_PUSH_DATA) {
    const newState = Object.assign({}, state);
    action.flg == 'his' ? (newState.activeItemHis = action.data) : (newState.activeItem = action.data);
    return newState;
  }
  if (action.type === ALL_CHECKED) {
    const newState = Object.assign({}, state);
    let tempArr = [];
    if (action.bool) {
      newState.items.forEach((val) => {
        tempArr.push(val.id)
      })
    } else {
      tempArr = []
    }
    newState.allChecked = action.bool;
    newState.checkItems = [...tempArr];
    return newState;
  }

  if (action.type === ALL_CHECKED_SHOW) {
    const newState = Object.assign({}, state);
    newState.allCheckShow = action.bool;
    return newState;
  }
  return state;
}