// 1 => LOADING
// 2 => DONE
// 3 => ERROR
import { update } from "../../acc-one-md-supplier/api/supplier.js"
import * as api from "../api/approve.js"

export default {
    namespaced: true,
    state: {
        lookup_search: 0,
        lookup_error_message: "",
        approvelist: [],
        selected_menu: {},

        listdata: [],
        selected_data: {},
        loading: false,
        total_page: 0,
        current_page: 1,
        xsearch: "",
        user: one_user()
    },
    mutations: {
        update_lookup_search(state, val) {
            state.lookup_search = val
        },
        update_lookup_error_message(state, val) {
            state.lookup_error_message = val
        },
        update_approvelist(state, val) {
            state.approvelist = val
        },
        update_selected_menu(state, val) {
            state.selected_menu = val
        },
        update_listdata(state, val) {
            state.listdata = val
        },
        update_selected_data(state, val) {
            state.selected_data = val
        },
        update_loading(state, val) {
            state.loading = 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
        },
        update_user(state, val) {
            state.user = val
        }
    },
    actions: {
        async listApprove(context, prm) {
            context.commit("update_lookup_search", 1)
            try {
                let prm = {}
                prm.token = one_token()
                let resp = await api.listApprove(prm)
                if (resp.status != "OK") {
                    context.commit("update_lookup_search", 3)
                    context.commit("update_lookup_error_message", resp.message)
                } else {
                    context.commit("update_lookup_search", 2)
                    context.commit("update_lookup_error_message", "")
                    let data = {
                        records: resp.data.records
                    }
                    context.commit("update_approvelist", data.records)
                }
            } catch (e) {
                context.commit("update_lookup_search", 3)
                context.commit("update_lookup_error_message", e.message)
            }
        },

        async search(context) {
            context.commit("update_loading", true);
            try {
                let prm = {
                    token: one_token()
                }
                let resp = await api.search(prm)
                if (resp.status != "OK") {
                    context.commit("update_lookup_search", 3)
                    context.commit("update_lookup_error_message", resp.message)
                } else {
                    context.commit("update_lookup_error_message", "")
                    let data = {
                        records: resp.data.records
                    }
                    context.commit("update_listdata", data.records)

                    context.commit("update_loading", false);
                }
            } catch (e) {
                context.commit("update_loading", false);
                context.commit("update_lookup_error_message", e.message)
            }
        }
    }
}
