// 1 => LOADING
// 2 => DONE
// 3 => ERROR
import * as api from "../api/rktype.js"

export default {
    namespaced: true,
    state: {
        typelist: [],
        total_listtype: 0,
        status_get: 0,
        selected_type: {},
        snackbar: false,
        message_snack: "",
        errors: [],
        save_status: 0,
        act: "new",
        search: "",
        categorylist: [],
        total_categorylist: 0,
        selected_category: {},
        xcategory: ""
    },
    mutations: {
        update_selected_type(state, data) {
            state.selected_type = data
        },
        update_total_listtype(state, data) {
            state.total_listtype = data
        },
        update_typelist(state, data) {
            state.typelist = data
        },
        update_status_get(state, data) {
            state.status_get = data
        },
        open_snackbar(state, bool) {
            state.snackbar = bool
        },
        update_message_snack(state, data) {
            state.message_snack = data
        },
        update_errors(state, val) {
            state.errors = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_search(state, val) {
            state.search = val
        },
        update_categorylist(state, val) {
            state.categorylist = val
        },
        update_total_categorylist(state, val) {
            state.total_categorylist = val
        },
        update_selected_category(state, val) {
            state.selected_category = val
        },
        update_xcategory(state, val) {
            state.xcategory = val
        }
    },
    actions: {
        async getList(context) {
            context.commit("update_status_get", 1);
            try {
                var prm = {token: one_token(), search: context.state.search};
                let resp = await api.getListing(prm);
                if (resp.status != "OK") {
                    context.commit("update_status_get", 3);
                    context.commit("update_message_snack", resp.message);
                    context.commit("open_snackbar", true);
                } else {
                    context.commit("update_status_get", 2);
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_categorylist", data.records);
                    context.commit("update_total_categorylist", data.total);
                }
            } catch (error) {
                context.commit("update_status_get", 3);
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        },

        async gettype(context, prm) {
            context.commit("update_status_get", 1);
            try {
                let resp = await api.gettype(one_token(), prm)
                if (resp.status != "OK") {
                    context.commit("update_status_get", 3);
                    context.commit("update_message_snack", resp.message);
                    context.commit("open_snackbar", true);
                } else {
                    context.commit("update_status_get", 2);
                    context.commit("update_message_snack", resp.message);
                    let data = {
                        records: resp.data.records
                    }

                    context.commit("update_typelist", data.records)
                }
            } catch (e) {
                context.commit("update_status_get", 3);
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        },
        
        async delete(context, prm) {
            context.commit("update_save_status", 1);
            try {
                prm.token = one_token();
                let resp = await api.deleteCategory(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3);
                    context.commit("open_snackbar", true);
                    context.commit("update_message_snack", resp.message);
                } else {
                    context.commit("update_save_status", 3);
                    context.commit("update_message_snack", resp.message);

                    context.commit("open_snackbar", true);
                    var msg = "Berhasil menghapus data kategori";
                    context.commit("update_message_snack", msg);
                    context.commit("update_selected_type", {});
                    context.commit("update_xcategory", "");
                    context.dispatch("getList");
                }
            } catch (error) {
                context.commit("update_save_status", 3);
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        },

        async save(context, prm) {
            context.commit("update_save_status", 1);
            try {
                prm.token = one_token();
                let resp = await api.saveCategory(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3);
                    context.commit("open_snackbar", true);
                    context.commit("update_message_snack", resp.message);
                } else {
                    context.commit("update_save_status", 2);
                    context.commit("update_message_snack", resp.message);


                    context.commit("open_snackbar", true);
                    var msg = "Berhasil menyimpan data kategori";
                    context.commit("update_message_snack", msg);
                    context.commit("update_selected_type", {});
                    context.commit("update_xcategory", "");
                    context.commit("update_errors", []);
                    context.dispatch("getList");
                }
            } catch (error) {
                context.commit("update_save_status", 3);
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        },

        async update(context, prm) {
            context.commit("update_save_status", 1);
            try {                
                prm.token = one_token()
                let resp = await api.updateCategory(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3);
                    context.commit("open_snackbar", true);
                    context.commit("update_message_snack", resp.message);
                } else {
                    context.commit("update_save_status", 2);
                    context.commit("update_message_snack", resp.message);

                    context.commit("open_snackbar", true);
                    var msg = "Berhasil menyimpan data kategori";
                    context.commit("update_message_snack", msg);
                    context.commit("update_selected_type", {});
                    context.commit("update_xcategory", "");
                    context.commit("update_errors", []);
                    context.dispatch("getList");
                }
            } catch (error) {
                context.commit("update_save_status", 3);
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        }
    }
};