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

export default {
    namespaced: true,
    state: {
        act:'new',
        lookup_priviledge: 0,
        lookup_error_message: '',
        priviledges: [],
        save_status: 0,
        save_error_message: '',
        dialog_success: false,
        msg_success: "",
        listregional: [{"id":"1", "name":"Regional", "code":"R"},{"id":"2", "name":"Cabang","code":"N"}],
        selected_listregional:{"id":"1", "name":"Regional","code":"R"}
    },
    mutations: {
        update_listregional(state, val) {
            state.listregional = val
        },
        update_selected_listregional(state, val) {
            state.selected_listregional = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_lookup_error_message(state, status) {
            state.lookup_error_message = status
        },
        update_lookup_priviledge(state, status) {
            state.lookup_priviledge = status
        },
        update_priviledges(state, data) {
            state.priviledges = data
        },
        update_selected_priviledge(state, val) {
            state.selected_priviledge = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_dialog_success(state, val) {
            state.dialog_success = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        }
    },
    actions: {
        async lookup(context, prm) {
            context.commit("update_lookup_priviledge", 1)
            try {
                prm.token = one_token()
                let resp = await api.lookup(prm)
                if (resp.status != "OK") {
                    context.commit("update_lookup_priviledge", 3)
                    context.commit("update_lookup_error_message", resp.message)
                } else {
                    context.commit("update_lookup_priviledge", 2)
                    context.commit("update_lookup_error_message", "")
                    let data = {
                        records: resp.data.records
                    }
                    context.commit("update_priviledges", data.records)
                }
            } catch (e) {
                context.commit("update_lookup_priviledge", 3)
                context.commit("update_lookup_error_message", e.message)
            }
        },
        async save(context, prm) {
            context.commit("update_save_status", 1)
            try {
                let resp = await api.save(one_token(),prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", "")
                    
                    context.commit("update_dialog_success", true)
                    var msg = "Data sudah terupdate dong ..."
                    context.commit("update_msg_success", msg)
                    context.commit("usergroup/update_no_save", false, { root: true })
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
            }
        }
    }
}