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

export default {
    namespaced: true,
    state: {
        snackbar: {
            state: false,
            msg: "",
            color: "error"
        },
        loading_process: false,
        list_inventarisMapping: {
            records: [],
            total: 0
        },
        selected_inventaris_gol: {},
        
        /* dialog inventaris gol for default */ 
        dialog_add_mapping_default: false,
        form_gol: {
            golID: 0,
            invCoaID: 0,
            debtCoaID: 0,
            bebanCoaID: 0,
            accumCoaID: 0,
            labaCoaID: 0,
            rugiCoaID: 0
        },
        list_gol_inventaris: {
            records: [],
            total: 0
        },

        dialog_confirm: false
    },
    mutations: {
        update_snackbar(state, val) {
            state.snackbar = val;
        },
        update_loading_process(state, val) {
            state.loading_process = val;
        },
        update_list_inventarisMapping(state, val) {
            state.list_inventarisMapping = val
        },
        update_selected_inventaris_gol(state, val) {
            state.selected_inventaris_gol = val
        },

        /* dialog invetaris gol for default */ 
        update_dialog_add_mapping_default(state, val) {
            state.dialog_add_mapping_default = val
        },
        update_form_gol(state, val) {
            state.form_gol = val
        },
        update_list_gol_inventaris(state, val) {
            state.list_gol_inventaris = val;
        },
        update_dialog_confirm(state, val) {
            state.dialog_confirm = val
        }
    },
    actions: {
        async getListingInventarisGol(context) {
            try {
                let params = { token: one_token() }
                const resp = await api.getListingInventarisGol(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_list_gol_inventaris', resp.data);
            } catch (e) {
                const snac = { state: true, msg: e.message, color: "error" };
                context.commit('update_snackbar', snac);
            }
        },
        async getListItemInventaris(context, id) {
            try {
                let params = {
                    token: one_token(),
                    golID: id
                }
                const resp = await api.getListItemInventaris(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('invItemMap/update_list_item_inventaris', resp.data, {root: true});
            } catch (e) {
                const snac = { state: true, msg: e.message, color: "error" };
                context.commit('update_snackbar', snac);
            }
        },
        async getListInventorygolMapping(context, keyword) {
            try {
                let params = {
                    token: one_token(),
                    keyword: keyword,
                    currpage: 1
                }
                const resp = await api.getListInventorygolMapping(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit("update_list_inventarisMapping", resp.data);
            } catch (e) {
                const snac = { state: true, msg: e.message, color: "error" };
                context.commit('update_snackbar', snac);
            }
        },
        async createInventarisGolMapping(context, params) {
            try {
                context.state.loading_process = true;
                params.token = one_token();
                const resp = await api.createInvCoaMapping(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

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

                context.state.loading_process = false;
                context.state.dialog_add_mapping_default = false;
            } catch (e) {
                context.state.loading_process = false;
                const snac = { state: true, msg: e.message, color: "error" };
                context.commit('update_snackbar', snac);
            }
        },
        async editInventarisGolMapping(context, params) {
            try {
                context.state.loading_process = true;
                params.token = one_token();
                const resp = await api.editInvCoaMapping(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

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

                context.state.loading_process = false;
                context.state.dialog_add_mapping_default = false;
            } catch (e) {
                context.state.loading_process = false;
                const snac = { state: true, msg: e.message, color: "error" };
                context.commit('update_snackbar', snac);
            }
        },
        async deleteInventarisGolMapping(context, id) {
            try {
                context.state.loading_process = true;
                params = {
                    token: one_token(),
                    M_InventarisCoaMappingID: id
                };
                const resp = await api.deleteInvCoaMapping(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

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

                context.state.loading_process = false;
                context.state.dialog_add_mapping_default = false;
            } catch (e) {
                context.state.loading_process = false;
                const snac = { state: true, msg: e.message, color: "error" };
                context.commit('update_snackbar', snac);
            }
        }
    }
}
