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

export default {
    namespaced: true,
    state: {
        search_status: 0,
        search_error_message: "",
        users: [],
        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_users(state, val) {
            state.users = 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
        }
    },
    actions: {
        async getUser(context, prm) {
            context.commit("update_search_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.getUser(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_users", 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)
            }
        },
    }
}