import { createSlice } from '@reduxjs/toolkit';

export const slice = createSlice({
    name: 'tabPanes',
    initialState: {
        activeTab: '',
        panes:[],
        toUnRead:false
    },
    reducers: {
        close: (state,action) => {        //关闭tab
            const {delIndex,active} = action.payload;
            if(active===state.activeTab){       //如果关闭的是当前激活的tab则激活前一个或后一个tab
                state.activeTab = state.panes[delIndex-1]?state.panes[delIndex-1].key:state.panes[delIndex+1].key;
            }
            state.panes.splice(delIndex,1);
        },
        closeOther:(state,action) => {        //关闭其他tab
            const pane = state.panes[action.payload.del];
            state.panes = [pane];
            //state.activeTab = action.payload.active;
        },
        add: (state,action) => {        //打开新tab
            state.panes.push(action.payload);
            state.activeTab = action.payload.key;
            state.toUnRead = action.payload.isUnRead;   //是否筛选未读
        },
        active:(state,action) => {       //切换tab
            state.activeTab = action.payload.idName;    //当前激活的tab
            state.toUnRead = action.payload.isUnRead;   //是否筛选未读
        },
        panesNow: (state, action) => {       //切换tab
            state.panes = [];
        },
    },
});

export const { close, closeOther, active, add, panesNow } = slice.actions;

export default slice.reducer;
