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

export default {
    namespaced: true,
    state: {
        list_item_inventaris: {
            records: [],
            total: 0
        },
        dialog_add_mapping_item: false,
        selected_inventaris_item: {},
        form: {
            invCoaID: 0,
            debtCoaID: 0,
            bebanCoaID: 0,
            accumCoaID: 0,
            labaCoaID: 0,
            rugiCoaID: 0
        },
        dialog_confirm_item: false
    },
    mutations: {
        update_dialog_add_mapping_item(state, val) {
            state.dialog_add_mapping_item = val
        },
        update_selected_inventaris_item(state, val) {
            state.selected_inventaris_item = val
        },
        update_list_item_inventaris(state, val) {
            state.list_item_inventaris = val;
        },
        update_form(state, val) {
            state.form = val
        },
        update_dialog_confirm_item(state, val) {
            state.dialog_confirm_item = val
        }
    },
    actions: {
        async createInvItemCoaMapping(context, params) {
            try {
                context.commit("invMap/update_loading_process", true, {root: true});
                params.token = one_token();
                const resp = await api.createInvItemCoaMapping(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                const snac = { state: true, msg: resp.data, color: "success" };
                context.commit('invMap/update_snackbar', snac, {root: true});
                
                const golID = context.state.selected_inventaris_item.itemGolID;
                context.dispatch("invMap/getListItemInventaris", golID, {root: true});
                
                context.commit("update_selected_inventaris_item", {});
                context.commit("invMap/update_loading_process", false, {root: true});
                context.commit('update_dialog_add_mapping_item', false);
            } catch (error) {
                context.commit("invMap/update_loading_process", false, {root: true});
                const snac = { state: true, msg: error.message, color: "error" };
                context.commit('invMap/update_snackbar', snac, {root: true});
            }
        },
        async editInvItemCoaMapping(context, params) {
            try {
                context.commit("invMap/update_loading_process", true, {root: true});
                params.token = one_token();
                const resp = await api.editInvItemCoaMapping(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                const snac = { state: true, msg: resp.data, color: "success" };
                context.commit('invMap/update_snackbar', snac, {root: true});

                const golID = context.state.selected_inventaris_item.itemGolID;
                context.dispatch("invMap/getListItemInventaris", golID, {root: true});

                context.commit("update_selected_inventaris_item", {});
                context.commit("invMap/update_loading_process", false, {root: true});
                context.commit('update_dialog_add_mapping_item', false);
            } catch (error) {
                context.commit("invMap/update_loading_process", false, {root: true});
                const snac = { state: true, msg: error.message, color: "error" };
                context.commit('invMap/update_snackbar', snac, {root: true});
            }
        },
        async deleteInvItemCoaMapping(context, params) {
            try {
                context.commit("invMap/update_loading_process", true, {root: true});
                params.token = one_token();
                const resp = await api.deleteInvItemCoaMapping(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                const snac = { state: true, msg: resp.data, color: "success" };
                context.commit('invMap/update_snackbar', snac, {root: true});

                const golID = context.state.selected_inventaris_item.itemGolID;
                context.dispatch("invMap/getListItemInventaris", golID, {root: true});

                context.commit("update_selected_inventaris_item", {});
                context.commit("invMap/update_loading_process", false, {root: true});
                context.commit("update_dialog_confirm_item", false);
            } catch (error) {
                context.commit("invMap/update_loading_process", false, {root: true});
                const snac = { state: true, msg: error.message, color: "error" };
                context.commit('invMap/update_snackbar', snac, {root: true});
            }
        }
    }
}