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

export default {
    namespaced: true,
    state: {
        snacbar: {
            message: '',
            color: 'success',
            active: false
        },
        filter: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            typejournal_id: 0,
            search: '',
            periode_id: 0,
        },
        list_jurnaltype: [],
        list_periode: [],
        selected_periode: {},
        listjournal: {
            records: [],
            total: 0
        },
        currpage: 1,
        selected_jurnal: {},
        selected_jurnal_detail: {},
        summary_jurnal_detail: {
            total_debit: 0,
            total_kredit: 0,
            balance: 0
        },
        dialog_verifikasi: false,
        dialog_pick_periode: false,
        dialog_edit_request: false
    },
    mutations: {
        update_filter(state, val) {
            state.filter = val
        },
        update_list_jurnaltype(state, val) {
            state.list_jurnaltype = val
        },
        update_list_periode(state, val) {
            state.list_periode = val
        },
        update_selected_periode(state, val) {
            state.selected_periode = val
        },
        update_listjournal(state, val) {
            state.listjournal = val
        },
        update_currpage(state, val) {
            state.currpage = val
        },
        update_selected_jurnal(state, val) {
            state.selected_jurnal = val
        },
        update_selected_jurnal_detail(state, val) {
            state.selected_jurnal_detail = val
        },
        update_summary_jurnal_detail(state, val) {
            state.summary_jurnal_detail = val
        },
        update_dialog_verifikasi(state, val) {
            state.dialog_verifikasi = val
        },
        update_snacbar(state, val) {
            state.snacbar = val
        },
        update_dialog_pick_periode(state, val) {
            state.dialog_pick_periode = val
        },
        update_dialog_edit_request(state, val) {
            state.dialog_edit_request = val
        }
    },
    actions: {
        async getListPeriode(context) {
            try {
                let snac = { color: 'error', message: '', active: false };
                context.commit("update_snacbar", snac);

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

                    // update to latest periode id in masterdata
                    let filter = context.state.filter
                    filter.periode_id = resp.data[0].periodeID
                    context.commit("update_filter", filter);
                    context.commit("update_selected_periode", resp.data[0]);
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit("update_snacbar", snac);
            }
        },
        async getListTypeJurnal(context) {
            try {
                let snac = { color: 'error', message: '', active: false };
                context.commit("update_snacbar", snac);

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

                let param = {
                    periodeID: context.state.filter.periode_id,
                    typeJournalID: context.state.filter.typejournal_id,
                    search: context.state.filter.search,
                    startdate: context.state.filter.startdate,
                    enddate: context.state.filter.enddate,
                    page: context.state.currpage,
                    token: one_token()
                }
                var resp = await api.getListJournalEdit(param);
                if (resp.status != "OK") {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit("update_snacbar", snac);
                } else {
                    context.commit("update_listjournal", resp.data);
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit("update_snacbar", snac);
            }
        },
        async getDetailJournalEdit(context, jurnalid) {
            try {
                let snac = { color: 'error', message: '', active: false };
                context.commit("update_snacbar", snac);

                let param = {
                    jurnalID: jurnalid,
                    token: one_token()
                }
                var resp = await api.getDetailJournalEdit(param);
                if (resp.status != "OK") {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit("update_snacbar", snac);
                } else {
                    const data = resp.data;
                    context.commit("update_selected_jurnal_detail", data);

                    // summary total kredit & debit jurnal
                    // changeAfter & changeBefore harusnya sama
                    const summary = data.changeAfter.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});
                    summary.balance = summary.total_debit - summary.total_kredit;

                    context.commit("update_summary_jurnal_detail", summary);
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit("update_snacbar", snac);
            }
        },
        async updateRequestEditStatus(context, status) {
            try {
                let snac = { color: 'error', message: '', active: false };
                context.commit("update_snacbar", snac);

                let param = {
                    JurnalRequestEditID: context.state.selected_jurnal.JurnalRequestEditID,
                    jurnalID: context.state.selected_jurnal.jurnalID,
                    status: status.status,
                    token: one_token()
                }
                var resp = await api.updateRequestEditStatus(param);
                if (resp.status != "OK") {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit("update_snacbar", snac);
                } else {
                    context.dispatch("getListJournalEdit");
                    let snac = { color: 'success', message: resp.data, active: true };
                    context.commit("update_snacbar", snac);

                    context.commit("update_dialog_edit_request", false)
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit("update_snacbar", snac);
            }
        },
        async verifEditJurnal(context, status) {
            try {
                let snac = { color: 'error', message: '', active: false };
                context.commit("update_snacbar", snac);

                let param = {
                    jurnalID: context.state.selected_jurnal.jurnalID,
                    newtrx: context.state.selected_jurnal_detail.changeAfter,
                    JurnalRequestEditID: context.state.selected_jurnal.JurnalRequestEditID,
                    status: status,
                    token: one_token()
                }
                var resp = await api.verifEditJurnal(param);
                if (resp.status != "OK") {
                    let snac = { color: 'error', message: resp.message, active: true };
                    context.commit("update_snacbar", snac);
                } else {
                    context.dispatch("getListJournalEdit");
                    let snac = { color: 'success', message: resp.data, active: true };
                    context.commit("update_snacbar", snac);

                    context.commit("update_summary_jurnal_detail", {
                        total_debit: 0,
                        total_kredit: 0,
                        balance: 0
                    });
                    context.commit("update_selected_jurnal_detail", {});
                    context.commit("update_selected_jurnal", {})
                    context.commit("update_dialog_verifikasi", false);
                }
            } catch (error) {
                let snac = { color: 'error', message: error.message, active: true };
                context.commit("update_snacbar", snac);
            }
        }
    }
}
