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,
  GET_DPT_LIS,
  INIT_ADMIN_ITEMS,
  ALL_CHECKED_ADMIN,
  ALL_CHECKED_SHOW_ADMIN,
  CKECK_ITEMS_ADMIN,
  DEL_ITEMS_ADMIN,
  BATCH_DEL_ITEMS_ADMIN,
  CHANGE_TITLE_ADMIN,
  WHICH_TEMP,
  FLODER_SLIDE,
  FLODER_ACTION,
  FOLDER_LIST_ADMIN,
  FOLDER_MODAL,
  FOLDER_ORDER_SHOW,
  NORMS_TEMPLATE_SEARCH,
  CLEAR_NORMS_TEMPLATE_SEARCH
} from '../types/tabTemplate';


const initDataList = {
  activeId: '0', //点击的tab
  visible: false, //弹窗
  showMsg: { //提示
    show: false,
    content: '',
    type: ''
  },
  items: [],
  adminItems:[],
  allChecked: false, //全选反选
  allCheckedAdmin:false,//全选反选管理员模板
  checkItems: [], //选中要删除的元素
  checkItemsAdmin: [], //选中要删除的元素管理员
  activeItem: {}, //引用的模板
  activeItemHis: {}, //引用的历史病例
  allCheckShow: false, //全选反选是否显示
  allCheckShowAdmin: false, //全选反选是否显示管理员管理
  current:1,//当前页
  hasMore:true,//是否显示更多
  departLis:[],//搜索的科室列表
  selectTemp:1,//默认1个人模板2标准模板
  floderListAdmin:[],//文件夹列表
  folderModalShow:false,//新建文件夹弹窗
  folderListContentArr :[],//管理员文件夹内模板合并
  folderOrderShow: false, //文件夹管理弹窗
  templateNormsSearchList:[] //标准模板搜索
}
export default (state = initDataList, action) => {
  if(action.type === CLEAR_NORMS_TEMPLATE_SEARCH) {
    const newState = Object.assign({}, state);
    newState.templateNormsSearchList = [];
    return newState;
  }
  if(action.type === NORMS_TEMPLATE_SEARCH) {
    const newState = Object.assign({}, state);
    newState.templateNormsSearchList = JSON.parse(JSON.stringify(action.templateNormsSearchList));
    return newState;
  }

  if (action.type === FOLDER_ORDER_SHOW) {//文件夹管理员列表
    const newState = Object.assign({}, state);
    newState.folderOrderShow = action.show;
    return newState;
  }
  if (action.type === FOLDER_MODAL) {//文件夹管理员列表
    const newState = Object.assign({}, state);
    newState.folderModalShow = action.show;
    return newState;
  }
  if (action.type === FOLDER_LIST_ADMIN) {//文件夹管理员列表
    const newState = Object.assign({}, state);
    newState.floderListAdmin = action.data;
    return newState;
  }
  if (action.type === FLODER_ACTION) {//文件夹操作
    const newState = Object.assign({}, state);
    let adminItemsTmp = newState.adminItems;
    for(let i = 0;i<adminItemsTmp.length;i++){
      if(adminItemsTmp[i].id == action.id){
        if(adminItemsTmp[i].action == 1){
          adminItemsTmp[i].action = 2
        }else{
          adminItemsTmp[i].action = 1
        }
      }else{
        adminItemsTmp[i].action = 2
      }
    }
    newState.adminItems = [...adminItemsTmp];
    return newState;
  }
  if (action.type === FLODER_SLIDE) {//文件夹展开
    const newState = Object.assign({}, state);
    let adminItemsTmp = newState.adminItems;
    for(let i = 0;i<adminItemsTmp.length;i++){
      if(adminItemsTmp[i].id == action.id){
        if(adminItemsTmp[i].slide == 1){
          adminItemsTmp[i].slide = 2
        }else{
          adminItemsTmp[i].slide = 1
        }
      }else{
        adminItemsTmp[i].slide = 2
      }
    }
    newState.adminItems = [...adminItemsTmp];
    return newState;
  }
  if (action.type === WHICH_TEMP) {//模板tab切换
    const newState = Object.assign({}, state);
    newState.selectTemp = action.tab;
    return newState;
  }
  if (action.type === GET_DPT_LIS) {
    const newState = Object.assign({}, state);
    newState.departLis = action.list;
    return newState;
  }
  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];
    if(tempArr.length == 0){
      newState.allCheckShow = false
    }
    return newState;
  }
  if (action.type === DEL_ITEMS_ADMIN) {//删除管理员模板
    const newState = Object.assign({}, state);
    let tempArr = newState.adminItems,tmpTmpLis = [];
    for(let i = 0;i < tempArr.length;i++){
      let tmpAction = tempArr[i]
      if(tmpAction.id == action.folderId){
        tmpTmpLis = tmpAction.templateInfo
        tmpTmpLis.splice(tmpTmpLis.findIndex(item => item.id == action.id), 1)
        tempArr[i].templateInfo = tmpTmpLis
      }
    }
    newState.adminItems = [...tempArr];
    if(tempArr.length == 0){
      newState.allCheckShowAdmin = false
    }
    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];
    if(tempArr.length == 0){
      newState.allCheckShow = false
    }
    newState.checkItems = [];
    return newState;
  }
  if (action.type === BATCH_DEL_ITEMS_ADMIN) {//批量删除管理员模板
    const newState = Object.assign({}, state);
    let tempArr = newState.folderListContentArr;
    for (let i = 0; i < action.ids.length; i++) {
      let currentId = action.ids[i];
      tempArr.splice(tempArr.findIndex(item => item.id == currentId), 1)
    }
    newState.folderListContentArr = [...tempArr];
    if(folderListContentArr.length == 0){
      newState.allCheckShowAdmin = false
    }
    newState.checkItemsAdmin = [];
    return newState;
  }
  if (action.type === INIT_ITEMS) {
    const newState = Object.assign({}, state);
    // let tmpItems = JSON.parse(JSON.stringify(newState.items))
    // let tmpCurrent = JSON.parse(JSON.stringify(newState.current))
    // console.log(action.state.flg,action.state.pages,tmpCurrent,45544)
    // if(action.state.current == 1&&!action.state.flg){//进入页面会调取分页相关先去掉
    //   newState.items = action.state.records
    // }else{
    //   newState.items = tmpItems.concat(action.state.records);
    // }
    newState.items = action.state.personalTemplate.records
    // newState.adminItems = action.state.adminTemplate.records
    newState.current = action.state.current;
    newState.hasMore = action.state.current<action.state.pages;
    return newState;
  }
  if (action.type === INIT_ADMIN_ITEMS) {//标准模板数据
    const newState = Object.assign({}, state);
    if(action.state[0]&&action.state[0].templateInfo&&action.state[0].templateInfo.length>0){
      action.state[0].slide=1
    }
    newState.adminItems = action.state
    let tmpLis = [];
    for(let k = 0;k < action.state.length;k++){
      let tmpData = action.state[k].templateInfo||[]
      tmpLis = tmpLis.concat(tmpData)
    }
    newState.folderListContentArr = [...tmpLis]
    newState.current = action.state.current;
    if(tmpLis.length == 0){
      newState.allCheckShowAdmin = false
    }
    newState.hasMore = action.state.current<action.state.pages;
    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;
    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 === CHANGE_TITLE_ADMIN) {
    const newState = Object.assign({}, state);
    let tempArr = newState.adminItems;
    for (let i = 0; i < tempArr.length; i++) {
      if (tempArr[i].id == action.obj.id) {
        tempArr[i].name = action.obj.title
      }
    }
    newState.adminItems = [...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 === CKECK_ITEMS_ADMIN) {
    const newState = Object.assign({}, state);
    if (newState.checkItemsAdmin.indexOf(action.id) == -1) {
      let tempArr = newState.checkItemsAdmin;
      tempArr.push(action.id);
      newState.checkItemsAdmin = [...tempArr]
    } else {
      let tempArr = newState.checkItemsAdmin;
      tempArr.splice(tempArr.findIndex(item => item === action.id), 1)
      newState.checkItemsAdmin = [...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_ADMIN) {//管理员全选反选
    const newState = Object.assign({}, state);
    let tempArr = [];
    if (action.bool) {
      newState.folderListContentArr.forEach((val) => {
        tempArr.push(val.id)
      })
    } else {
      tempArr = []
    }
    newState.allCheckedAdmin = action.bool;
    newState.checkItemsAdmin = [...tempArr];
    return newState;
  }

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