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

export default {
    namespaced: true,
    state: {
        periode: [],
        total_list: 0,
        status_get: 0,
        selected: {},
        snackbar: false,
        message_snack: ""
    },
    mutations: {
        update_selected(state, data) {
            state.selected = data
        },
        update_total_list(state, data) {
            state.total_list = data
        },
        update_list(state, data) {
            state.periode = 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
        }
    },
    actions: {
        async getList(context) {
            context.commit("update_status_get", 1);
            try {
                var params = {token: one_token()};
                let resp = await api.getListing(params);
                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
                    }
                    // console.log(data.total);
                    // console.log(data.records);
                    context.commit("update_list", data.records);
                    context.commit("update_total_list", data.total);
                }
            } catch (error) {
                context.commit("update_status_get", 3);
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        },
        
        async deletePeriode(context) {
            try {
                var params = {
                    token: one_token(),
                    id: context.state.selected.periodeID
                }
                console.log(params);
                let resp = await api.p_delete(params);
                if (resp.status != "OK") {
                    // console.log(resp.message);
                    context.commit("open_snackbar", true);
                    context.commit("update_message_snack", resp.message);
                } else {
                    context.commit("open_snackbar", true);
                    var msg = "Berhasil menghapus data periode";
                    context.commit("update_message_snack", msg);
                    context.commit("update_selected", {});
                    context.dispatch("getList");
                }
            } catch (error) {
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        },

        async addPeriode(context) {
            try {
                var params = {
                    token: one_token(),
                    ...context.state.selected
                };
                console.log(params);
                let resp = await api.p_add(params);
                if (resp.status != "OK") {
                    context.commit("open_snackbar", true);
                    context.commit("update_message_snack", resp.message);
                } else {
                    context.commit("open_snackbar", true);
                    var msg = "Berhasil menyimpan data periode";
                    context.commit("update_message_snack", msg);
                    context.commit("update_selected", {});
                    context.dispatch("getList");
                }
            } catch (error) {
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        },

        async editPeriode(context) {
            try {                
                var params = {
                    token: one_token(),
                    ...context.state.selected
                };
                console.log(params);
                let resp = await api.p_edit(params);
                if (resp.status != "OK") {
                    context.commit("open_snackbar", true);
                    context.commit("update_message_snack", resp.message);
                } else {
                    context.commit("open_snackbar", true);
                    var msg = "Berhasil menyimpan data periode";
                    context.commit("update_message_snack", msg);
                    context.commit("update_selected", {});
                    context.dispatch("getList");
                }
            } catch (error) {
                context.commit("update_message_snack", error.message);
                context.commit("open_snackbar", true);
            }
        },

        async checkNewOrEdit(context) {
            if (!checkIsEmpty(context.state.selected)) {
                var msg = "Form masih ada yang kosong";
                context.commit("update_message_snack", msg);
                context.commit("open_snackbar", true);
                return;
            }

            if (!context.state.selected.periodeID) {
                context.dispatch("addPeriode");
            } else {
                context.dispatch("editPeriode");
            }
        }
    }
};

function checkIsEmpty(obj) {
    if (Object.keys(obj).length === 0) {
        return false;
    }

    return obj.periodeYear && obj.periodeMonth && obj.periodeName && obj.periodeStartDate && obj.periodeEndDate;
}