import types from "../types";

let currentPerIndex = 0
let beginDate = ''

const state = { currentPerIndex, beginDate }

const getters = {
    getCurPerIndex(state) {
        const temp = localStorage.getItem('currentPerIndex');
        if (temp) {
            state.currentPerIndex = temp
        }
        return state.currentPerIndex;
    },
    getBeginDate(state) {
        const temp = localStorage.getItem('beginDate');
        if (temp) {
            state.beginDate = temp
        }
        return state.beginDate;
    },

}

const mutations = {
    [types.CHANGE_CURRENT_PERIOD](state, currentPerIndex) {
        state.currentPerIndex = currentPerIndex;
        try {
            localStorage.setItem("currentPerIndex", currentPerIndex);
        } catch (error) { }
    },
    [types.CHANGE_CURRENT_BEGINDATE](state, beginDate) {
        state.beginDate = beginDate;
        try {
            localStorage.setItem("beginDate", beginDate);
        } catch (error) { }
    }
}

const actions = {
    savePeriodIndex({ commit }, currentPerIndex) {
        commit(types.CHANGE_CURRENT_PERIOD, currentPerIndex);
    },
    savePerBeginDate({ commit }, beginDate) {
        commit(types.CHANGE_CURRENT_BEGINDATE, beginDate);
    }
}

export default {
    state,
    getters,
    mutations,
    actions
}