import * as api from "../api/api.js";

export default {
    namespaced: true,
    state: {
        invCoa: [],
        debtCoa: [],
        bebanCoa: [],
        accumCoa: [],
        labaCoa: [],
        rugiCoa: []
    },
    mutations: {
        update_invCoa(state, val) {
            state.invCoa = val
        },
        update_debtCoa(state, val) {
            state.debtCoa = val
        },
        update_bebanCoa(state, val) {
            state.bebanCoa = val
        },
        update_accumCoa(state, val) {
            state.accumCoa = val
        },
        update_labaCoa(state, val) {
            state.labaCoa = val
        },
        update_rugiCoa(state, val) {
            state.rugiCoa = val
        }
    },
    actions: {
        async getListCoa(context, param) {
            try {
                let params = {
                    token: one_token(),
                    keyword: param.keyword
                }
                const resp = await api.getListCoa(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                let update = "update_" + param.key;
                context.commit(update, resp.data);
            } catch (e) {
                const snac = { state: true, msg: e.message, color: "error" };
                context.commit('invMap/update_snackbar', snac, { root: true });
            }
        },
    }
}
