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

export default {
    namespaced: true,
    state: {
        act:'new',
        lookup_usergroup: 0,
        lookup_error_message: '',
        usergroups: [],
        total_usergroups: 0,
        total_filter_usergroups: 0,
        selected_usergroup: { name: "[ Belum memilih Grup User ]" },
        save_status: 0,
        save_error_message: '',
        dialog_form_usergroup: false,
        dialog_edit_form_usergroup:false,
        alert_success: false,
        msg_success: "",
        show_all:'N',
        no_save:false,
        open_alert_confirmation:false
    },
    mutations: {
        update_act(state, val) {
            state.act = val
        },
        update_show_all(state, val) {
            state.show_all = val
        },
        update_lookup_error_message(state, status) {
            state.lookup_error_message = status
        },
        update_lookup_usergroup(state, status) {
            state.lookup_usergroup = status
        },
        update_usergroups(state, data) {
            state.usergroups = data.records
            state.total_usergroups = data.total
            state.total_filter_usergroups = data.total_filter
        },
        update_selected_usergroup(state, val) {
            state.selected_usergroup = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_dialog_form_usergroup(state, val) {
            state.dialog_form_usergroup = val
        },
        update_dialog_edit_form_usergroup(state, val) {
            state.dialog_edit_form_usergroup = val
        },
        update_alert_success(state, val) {
            state.alert_success = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },
        update_no_save(state, val) {
            state.no_save = val
        },
        update_open_alert_confirmation(state, val) {
            state.open_alert_confirmation = val
        }
    },
    actions: {
        async lookup(context, prm) {
            context.commit("update_lookup_usergroup", 1)
            try {
                let resp = await api.lookup(one_token(), prm.search, prm.all)
                if (resp.status != "OK") {
                    context.commit("update_lookup_usergroup", 3)
                    context.commit("update_lookup_error_message", resp.message)
                } else {
                    context.commit("update_lookup_usergroup", 2)
                    context.commit("update_lookup_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total,
                        total_filter: resp.data.total_filter
                    }
                    context.commit("update_usergroups", data)
                    context.commit("update_selected_usergroup", data.records[0])
                    context.dispatch("priviledge/lookup",{id:data.records[0].id,code:context.rootState.priviledge.selected_listregional.code}, { root: true })
                }
            } catch (e) {
                context.commit("update_lookup_usergroup", 3)
                context.commit("update_lookup_error_message", e.message)
            }
        }
    }
}