import * as api from '../api/api.js';

export default {
    namespaced: true,
    state: {
        snackbar: { state: false, color: 'error', message: "" },
        userinfo: {},
        filterpay: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            status: "All",
            search: ""
        },
        list_status: ['All', 'Draft', 'Verified', 'Approved', 'Rejected', 'Completed'],
        list_payment: { records: [], total: 1 },
        currentpage: 1,

        selected_payment: {},
        payment_items: {},
        dialog_detail: false,
        operation_mode: 'view',
        operation_loading: false
    },
    mutations: {
        update_userinfo(state, val) {
            state.userinfo = val
        },
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_filterpay(state, val) {
            state.filterpay = val
        },
        update_list_status(state, val) {
            state.list_status = val
        },
        update_list_payment(state, val) {
            state.list_payment = val
        },
        update_currentpage(state, val) {
            state.currentpage = val
        },
        update_selected_payment(state, val) {
            state.selected_payment = val
        },
        update_payment_items(state, val) {
            state.payment_items = val
        },
        update_dialog_detail(state, val) {
            state.dialog_detail = val
        },
        update_operation_mode(state, val) {
            state.operation_mode = val
        },
        update_operation_loading(state, val) {
            state.operation_loading = val
        }
    },
    actions: {
        async lookupUserApproveLevel(context) {
            try {
                const resp = await api.getUserApproveLevel({token: one_token()});
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                } else {
                    context.commit('update_userinfo', resp.data);
                }
            } catch (error) {
                const snack = { state: true, color: 'error', message: error.message }
                context.commit("update_snackbar", snack)
            }
        },

        async lookupPayment(context) {
            try {
                let param = {
                    ...context.state.filterpay,
                    currentpage: context.state.currentpage,
                    token: one_token()
                }
                const resp = await api.search(param)
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                } else {
                    context.commit("update_list_payment", resp.data)
                }
            } catch (error) {
                const snack = { state: true, color: 'error', message: error.message }
                context.commit("update_snackbar", snack)
            }
        },

        async lookupPaymentItem(context) {
            try {
                let param = {
                    token: one_token(),
                    paymentID: context.state.selected_payment.SupplierPaymentID
                };

                const resp = await api.searchDetail(param);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                } else {
                    context.commit("update_payment_items", resp.data);
                }
            } catch (error) {
                const snack = { state: true, color: 'error', message: error.message }
                context.commit("update_snackbar", snack)
            }
        },

        async updateStatus(context) {
            try {
                context.state.operation_loading = true
                let param = {
                    token: one_token(),
                    userlevel: context.state.userinfo.level ?? "0",
                    paymentID: context.state.selected_payment.SupplierPaymentID
                }

                const resp = await api.updateStatusPayment(param)
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                } else {
                    const snack = { state: true, color: 'success', message: resp.data }
                    context.commit("update_snackbar", snack)

                    context.dispatch("lookupPayment")
                    context.state.operation_loading = false
                    context.state.dialog_detail = false
                }
            } catch (error) {
                const snack = { state: true, color: 'error', message: error.message }
                context.commit("update_snackbar", snack)
            }
        }
    }
}