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

export default {
    namespaced: true,
    state: {
        search_status: 0,
        search_error_message: "",
        departements: [],
        selected_departement: {},
        total_page: 0,
        current_page: 1,
        xsearch: "",
        last_id: -1,
        dialog_form: false,
        act: "new",
        errors: "",
        save_status: 0,
        save_error_message: "",
        alert_error: false,
        alert_success: false,
        msg_success: ""
    },
    mutations: {
        update_search_status(state, val) {
            state.search_status = val
        },
        update_search_error_message(state, val) {
            state.search_error_message = val
        },
        update_departements(state, val) {
            state.departements = val
        },
        update_selected_departement(state, val) {
            state.selected_departement = val
        },
        update_total_page(state, val) {
            state.total_page = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_xsearch(state, val) {
            state.xsearch = val
            state.current_page = 1
        },
        update_last_id(state, val) {
            state.last_id = val
        },
        update_dialog_form(state, val) {
            state.dialog_form = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_alert_error(state, val) {
            state.alert_error = val
        },
        update_alert_success(state, val) {
            state.alert_success = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },
        update_errors(state, val) {
            state.errors = val
        },
    },
    actions: {
        async search(context) {
            context.commit("update_search_status", 1)
            try {
                var prm = {
                    token: one_token(),
                    current_page: context.state.current_page,
                    search: context.state.xsearch,
                    last_id: -1
                }
                let resp = await api.search(prm)
                if (resp.status != "OK") {
                    context.commit("update_search_status", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_search_status", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total_page
                    }

                    context.commit("update_departements", data.records)
                    context.commit("update_total_page", data.total)

                }
            } catch (e) {
                context.commit("update_search_status", 3)
                context.commit("update_search_error_message", e.message)
            }
        },
        async save(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.save(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        var msg = `Departement baru dengan kode ${data.records[0].M_DepartmentCode} sudah tersimpan`;
                        context.commit("update_msg_success", msg)
                        context.dispatch("search")
                        context.commit("update_selected_departement", data.records[0]);
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        },
        async update(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.update(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        var msg = `Departement dengan kode ${data.records[0].M_DepartmentCode} berhasil di update`;
                        context.commit("update_msg_success", msg)
                        context.dispatch("search")
                        context.commit("update_selected_departement", data.records[0]);
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        },
        async deleteDep(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.deleteDep(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        message: resp.data.message
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        context.commit("update_msg_success", data.message)
                        context.dispatch("search")
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        }
    }
}