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

export default {
    namespaced: true,
    state: {
        filter: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            search: ''
        },
        listing_voucher: {
            records: [],
            total: 0
        },
        main_table_page: 1,
        selected_voucher: {},
        list_detail_voucher: {},
        list_account: [],
        detail_table_page: 1,
        selected_detail_voucher: {},
        dialog_adjustment: false,
        snackbar: {
            isOpen: false,
            color: 'info',
            message: ''
        },
    },
    mutations: {
        update_filter(state, val) {
            state.filter = val
        },
        update_selected_voucher(state, val) {
            state.selected_voucher = val
        },
        update_listing_voucher(state, val) {
            state.listing_voucher = val
        },
        update_main_table_page(state, val) {
            state.main_table_page = val
        },
        update_list_detail_voucher(state, val) {
            state.list_detail_voucher = val
        },
        update_detail_table_page(state, val) {
            state.detail_table_page = val
        },
        update_selected_detail_voucher(state, val) {
            state.selected_detail_voucher = val
        },
        update_dialog_adjustment(state, val) {
            state.dialog_adjustment = val
        },
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_list_account(state, val) {
            state.list_account = val
        }
    },
    actions: {
        async search(context) {
            var param = {
                token: one_token(),
                page: context.state.main_table_page,
                ...context.state.filter
            }

            let resp = await api.search(param)
            if (resp.status != 'OK') {
                let snk = {isOpen: true, color: 'error', message: '[Error] ' + resp.message}
                context.commit("update_snackbar", snk)
            } else {
                context.commit("update_listing_voucher", resp.data)
            }
        },
        async searchDetail(context) {
            var param = {
                token: one_token(),
                ID: context.state.selected_voucher.PaymentVoucherID
            }
            let resp = await api.searchDetail(param)
            if (resp.status != 'OK') {
                let snk = {isOpen: true, color: 'error', message: '[Error] ' + resp.message}
                context.commit("update_snackbar", snk)
            } else {
                context.commit("update_list_detail_voucher", resp.data)
            }
        },
        async updateAdjusment(context) {
            var param = {
                token: one_token(),
                prd_id: context.state.selected_detail_voucher.PurchaseRequestDirectID,
                price_adj: context.state.selected_detail_voucher.PurchaseRequestDirectAdjustment,
                account_adj: context.state.selected_detail_voucher.PurchaseRequestDirectAdjustmentAccount
            }

            let resp = await api.updateAdjusment(param)
            if (resp.status != 'OK') {
                let snk = {isOpen: true, color: 'error', message: '[Error] ' + resp.message}
                context.commit("update_snackbar", snk)
            } else {
                let snk = {isOpen: true, color: 'success', message: '[Succeed]'};
                context.commit("update_snackbar", snk)
                context.dispatch("searchDetail");
                context.commit("update_selected_detail_voucher", {});
                context.commit("update_dialog_adjustment", false);
            }
        },
        async approveRealisasi(context) {
            var param = {
                token: one_token(),
                ID: context.state.selected_voucher.PaymentVoucherID
            }

            let resp = await api.approveRealisasi(param);
            if (resp.status != 'OK') {
                let snk = {isOpen: true, color: 'error', message: '[Error] ' + resp.message}
                context.commit("update_snackbar", snk)
            } else {
                let snk = {isOpen: true, color: 'success', message: '[Succeed]'};
                context.commit("update_snackbar", snk)
                context.dispatch("search");
                context.commit("update_selected_voucher", {})
                context.commit("update_list_detail_voucher", {})
            }
        },
        async getListAccount(context) {
            var resp = await api.getListAccount({token: one_token()})
            if (resp.status != 'OK') {
                let snk = {isOpen: true, color: 'error', message: '[Error] ' + resp.message}
                context.commit("update_snackbar", snk)
            } else {
                context.commit("update_list_account", resp.data)
            }
        }
    }
}