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

interface CommonState {
    resources: [] | null;
    meal_tags: [] | null;
    food_scales: [] | null;
    configs: any | null;
    duration: {
        min: number;
        max: number;
        step: number;
    } | null;
    showTabbar: boolean | true;
    showFoodTabBadge: boolean | false;
    pageIndex: number | 0;
    recordSelID: any | null; //删除id
}

const initialState: CommonState = {
    resources: [],
    meal_tags: [],
    food_scales: [],
    configs: null,
    duration: null,
    showTabbar: true,
    showFoodTabBadge: false,
    pageIndex: 0,
    recordSelID:null
}

const commonSlice = createSlice({
    name: 'permission',
    initialState,
    reducers: {
        setSelID(state,action){
            state.recordSelID = action.payload;
        },
        //静态资源
        setResources(state, action) {
            state.resources = action.payload;
        },
        setMealTags(state, action) {
            state.meal_tags = action.payload
        },
        setFoodScales(state, action) {
            state.food_scales = action.payload
        },
        //是否disable并隐藏tabbar
        setTabbarStatus(state, action) {
            state.showTabbar = action.payload;
        },
        //time配置文件
        setConfigs(state, action) {
            state.configs = action.payload;

            if (state.configs) {

                state.duration = {
                    min: state.configs.min / 60,
                    max: state.configs.max / 60,
                    step: state.configs.step
                }

            }
        },
        changeTabbar(state, action) {
            state.pageIndex = action.payload;
        },
        setFoodTabBadge(state, action) {
            state.showFoodTabBadge = action.payload;
        }
    }
});

export default commonSlice.reducer;
export const {setSelID, setResources, setMealTags, setFoodScales, setConfigs, setTabbarStatus, changeTabbar, setFoodTabBadge } = commonSlice.actions;