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

export default {
    namespaced: true,
    state: {
        // dialog
        dialog_journal: false,
        dialog_journal_balik: false,
        dialog_add_jurnaltrx: false,
        dialog_view_jurnal: false,
        dialog_pick_periode: false,
        
        // jurnal balik
        filter_balik: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            periodeID: 0,
            typeJournalID: 0,
            search: ""
        },
        list_journal_balik: {
            records: [],
            total: 0
        },
        currpage_balik: 1,
        selected_journal_balik: {},
        journal_detail_balik: {
            transaction: [],
            summary: {
                total_debit: 0,
                total_kredit: 0,
                balance: 0,
            }
        },
        journal_balik_new: [],
        summary_balik_new: {
            total_debit: 0,
            total_kredit: 0,
            balance: 0,
        },

        // list data general
        list_periode: [],
        selected_periode: {},
        list_typejurnal: [],
        list_account: [],

        // jurnal closed
        filter_closed: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            periodeID: 0,
            typeJournalID: 0,
            search: ""
        },
        list_journal_closed: {
            records: [],
            total: 0
        },
        currpage_closed: 1,
        selected_journal_closed: {
            totals: {
                total_debit: 0,
                total_kredit: 0,
                balance: 0,
            }
        },

        // utilities
        snackbar: {
            color: "primary",
            active: false,
            message: ""
        },
        user_level: {}
    },
    mutations: {
        // dialog
        update_dialog_journal(state, val) {
            state.dialog_journal = val
        },
        update_dialog_journal_balik(state, val) {
            state.dialog_journal_balik = val
        },
        update_dialog_add_jurnaltrx(state, val) {
            state.dialog_add_jurnaltrx = val
        },
        update_dialog_view_jurnal(state, val) {
            state.dialog_view_jurnal = val
        },
        update_dialog_pick_periode(state, val) {
            state.dialog_pick_periode = val
        },

        // jurnal balik
        update_filter_balik(state, val) {
            state.filter_balik = val
        },
        update_list_journal_balik(state, val) {
            state.list_journal_balik = val
        },
        update_currpage_balik(state, val) {
            state.currpage_balik = val
        },
        update_selected_journal_balik(state, val) {
            state.selected_journal_balik = val
        },
        update_journal_detail_balik(state, val) {
            state.journal_detail_balik = val
        },
        update_journal_balik_new(state, val) {
            state.journal_balik_new = val
        },
        update_summary_balik_new(state, val) {
            state.summary_balik_new = val
        },

        // list data general
        update_list_periode(state, val) {
            state.list_periode = val
        },
        update_selected_periode(state, val) {
            state.selected_periode = val
        },
        update_list_typejurnal(state, val) {
            state.list_typejurnal = val
        },
        update_list_account(state, val) {
            state.list_account = val
        },

        // journal closed
        update_list_journal_closed(state, val) {
            state.list_journal_closed = val
        },
        update_filter_closed(state, val) {
            state.filter_closed = val
        },
        update_currpage_closed(state, val) {
            state.currpage_closed = val
        },
        update_selected_journal_closed(state, val) {
            state.selected_journal_closed = val
        },

        // utilities
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_user_level(state, val) {
            state.user_level = val
        }
    },
    actions: {
        async getDaftarPeriode(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                var resp = await api.getDaftarPeriode({ token: one_token() })
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    context.commit('update_list_periode', resp.data);

                    let fbalik = context.state.filter_balik;
                    let fclosed = context.state.filter_closed;
                    fbalik.periodeID = resp.data[0].periodeID;
                    fclosed.periodeID = resp.data[0].periodeID;

                    context.commit('update_filter_balik', fbalik);
                    context.commit('update_filter_closed', fclosed);
                    context.commit('update_selected_periode', resp.data[0]);
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true }
                context.commit('update_snackbar', snac)
            }
        },

        async getDaftarTipeJurnal(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                var resp = await api.getDaftarTipeJurnal({ token: one_token() });
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    context.commit("update_list_typejurnal", resp.data);
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snac);
            }
        },

        async getDaftarJournalClosed(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                let params = {
                    token: one_token(),
                    currpage: context.state.currpage_closed,
                    ...context.state.filter_closed
                }

                var resp = await api.getDaftarJournalClosed(params)
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    context.commit("update_list_journal_closed", resp.data)
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snac);
            }
        },

        async getDetailJournalClosed(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                let params = {
                    token: one_token(),
                    jurnalID: context.state.selected_journal_closed.jurnalID
                }

                var resp = await api.getDetailJournalClosed(params)
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    let datatx = resp.data
                    // calculate total debit, kredit, balance
                    const totals = datatx.reduce((acc, item) => {
                        const debit = Number(item.jurnalTxDebit) || 0
                        const kredit = Number(item.jurnalTxCredit) || 0

                        acc.total_debit += debit
                        acc.total_kredit += kredit
                        return acc;
                    }, {total_debit: 0, total_kredit: 0})
                    totals.balance = totals.total_debit - totals.total_kredit

                    let selected = context.state.selected_journal_closed
                    let jrlbalik = context.state.journal_detail_balik
                    
                    selected.detail = resp.data
                    selected.totals = totals

                    jrlbalik.transaction = datatx
                    jrlbalik.summary = totals

                    context.commit("update_selected_journal_closed", selected)
                    context.commit("update_journal_detail_balik", jrlbalik)
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snac);
            }
        },

        async getDaftarAccount(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                let param = {
                    token: one_token(),
                    keyword: ""
                }
                var resp = await api.getDaftarAccount(param);
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    context.commit("update_list_account", resp.data)
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snac);
            }
        },

        async getDaftarJournalBalik(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                let param = {
                    token: one_token(),
                    currpage: context.state.currpage_balik,
                    ...context.state.filter_balik
                }
                var resp = await api.getDaftarJournalBalik(param)
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    context.commit("update_list_journal_balik", resp.data)
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snac);
            }
        },

        async getDetailJournalBalik(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                let param = {
                    token: one_token(),
                    jurnalID: context.state.selected_journal_balik.jurnalID
                }
                var resp = await api.getDetailJournalBalik(param)
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    let datatx = resp.data

                    const summary = datatx.reduce((acc, item) => {
                        const debit = Number(item.jurnalTxDebit)
                        const kredit = Number(item.jurnalTxCredit)

                        acc.total_debit += debit
                        acc.total_kredit += kredit
                        return acc
                    }, { total_debit: 0, total_kredit: 0 })
                    summary.balance = summary.total_debit - summary.total_kredit

                    let detail_balik = {
                        transaction: datatx,
                        summary: summary
                    }
                    context.commit("update_journal_detail_balik", detail_balik)
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snac);
            }
        },

        async getUserApproveLevel(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);
                
                var resp = await api.getUserApproveLevel({token: one_token()});
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    context.commit("update_user_level", resp.data)
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snac);
            }
        },

        async saveKoreksiJurnal(context) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                let detailtrx = [];
                let jurnalbalik = context.state.journal_detail_balik.transaction
                jurnalbalik.forEach(item => {
                    detailtrx.push({
                        coaID: item.jurnalTxCoaID,
                        coaDescription: item.coaDescription,
                        debit: item.jurnalTxDebit,
                        kredit: item.jurnalTxCredit,
                        info: item.jurnalAddOnValue,
                        infocode: item.jurnalAddOnCode
                    })
                });
                
                let jurnalnew = context.state.journal_balik_new
                jurnalnew.forEach(item => {
                    detailtrx.push({
                        coaID: item.jurnalTxCoaID,
                        coaDescription: item.coaDescription,
                        debit: item.jurnalTxDebit,
                        kredit: item.jurnalTxCredit,
                        info: item.jurnalAddOnValue,
                        infocode: 'PENYESUAIAN'
                    })
                });

                let param = {
                    token: one_token(),
                    oldJurnalID: context.state.selected_journal_closed.jurnalID,
                    jurnalTypeCode: context.state.selected_journal_closed.JurnalTypeCode,
                    jurnalNo: context.state.selected_journal_closed.jurnalNo,
                    jurnalDate: context.state.selected_journal_closed.jurnalDate,
                    detailtrx: detailtrx
                }
                var resp = await api.saveKoreksiJurnal(param);
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    let snac = { color: 'success', message: resp.data, active: true };
                    context.commit('update_snackbar', snac);

                    context.state.selected_journal_closed = {
                        totals: {
                            total_debit: 0,
                            total_kredit: 0,
                            balance: 0,
                        }
                    };

                    context.state.journal_detail_balik = {
                        transaction: [],
                        summary: {
                            total_debit: 0,
                            total_kredit: 0,
                            balance: 0,
                        }
                    };

                    context.state.summary_balik_new = {
                        total_debit: 0,
                        total_kredit: 0,
                        balance: 0,
                    }
                    
                    context.dispatch("getDaftarJournalBalik");
                    context.state.dialog_journal_balik = false;
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snac);
            }
        },

        async updateStatusJurnalKoreksi(context, status) {
            try {
                let snac = { color: 'primary', message: '', active: false };
                context.commit('update_snackbar', snac);

                let param = {
                    token: one_token(),
                    jurnalID: context.state.selected_journal_balik.jurnalID,
                    status: status
                }

                var resp = await api.updateStatusKoreksiJurnal(param)
                if (resp.status != 'OK') {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit('update_snackbar', snac);
                } else {
                    let snac = { color: 'success', message: resp.data, active: true };
                    context.commit('update_snackbar', snac);

                    context.commit("update_selected_journal_balik", {});
                    context.dispatch("getDaftarJournalBalik");
                    context.commit('update_dialog_view_jurnal', false);
                }
            } catch (error) {
                let snacc = { color: 'error', message: error.message, active: true };
                context.commit('update_snackbar', snacc);
            }
        }
    }
}