// 1 => LOADING
// 2 => DONE
// 3 => ERROR
import * as api from "../api/journal.js"

export default {
    namespaced: true,
    state: {
        search_status: 0,
        current_page: 1,
        x_search: "",
        search_error_message: "",
        last_id: -1,
        coas: [],
        total_coas: 0,
        selected_coa: {},
        dialog_form_coa: false,
        act: 'new',
        save_status: 0,
        save_error_message: '',
        errors: [],
        alert_success: false,
        msg_success: "",
        alert_error: false,
        dialog_error: false,
        open_print: false,

        // ----
        xdate: moment(new Date()).format('YYYY-MM-DD'),
        end_date: moment(new Date()).format('YYYY-MM-DD'),
        journals: [
            // {
            //     "journalID": "1",
            //     "journalNo": "JM002/08",
            //     "date": "31-08-2024",
            //     "name": "budhi/budhi",
            //     "account": "508501001",
            //     "title": "MS BESIN, SOLAR, PARKIR, TOL, TRANSPORT",
            //     "description": "ACCRUED PARKIR AGST 24",
            //     "debit": "275000",
            //     "credit": "0",
            //     "posted": "Y"
            // },
            // {
            //     "journalID": "2",
            //     "journalNo": "JM002/08",
            //     "date": "31-08-2024",
            //     "name": "budhi/budhi",
            //     "account": "2004009",
            //     "title": "ACCRUED EXP OTHERS",
            //     "description": "ACCRUED PARKIR AGST 24",
            //     "debit": "0",
            //     "credit": "275000",
            //     "posted": "Y"
            // }
        ],
        selected_journal: {},
        summary: {"debit": 0, "credit": 0, "balance": 0},
        dialog_form_detail: false,
        periodeList: [],
        selected_periode: {},
        loading: false,
        dialog_form_periode: false,
        dialog_form_confirm: false,
        autocomplete_status: 0,
        search_status: 0,
        search_error_message: "",
        total_journal: 0,
        user: one_user(),
        jurnaldetails: [],
        dialog_error_jurnal: false,
        msgerrjurnallist: [],
        user: one_user(),
        ishide: 'Y',
        branchlists: []
    },
    mutations: {
        update_ishide(state, val) {
            state.ishide = val
        },
        update_search_status(state, val) {
            state.search_status = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_x_search(state, val) {
            state.x_search = val
            state.current_page = 1
        },
        update_search_error_message(state, val) {
            state.search_error_message = val
        },
        update_last_id(state, val) {
            state.last_id = val
        },
        update_coas(state, val) {
            state.coas = val
        },
        update_total_coas(state, val) {
            state.total_coas = val
        },
        update_selected_coa(state, val) {
            state.selected_coa = val
        },
        update_dialog_form_coa(state, val) {
            state.dialog_form_coa = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_errors(state, val) {
            state.errors = val
        },
        update_alert_success(state, val) {
            state.alert_success = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },
        update_alert_error(state, val) {
            state.alert_error = val
        },
        update_dialog_error(state, val) {
            state.dialog_error = val
        },
        update_open_print(state, value) {
            state.open_print = value
        },

        // ---
        update_xdate(state, val) {
            state.xdate = val
        },
        update_end_date(state, val) {
            state.end_date = val
        },
        update_journals(state, val) {
            state.journals = val
        },
        update_branchlists(state, val) {
            state.branchlists = val
        },
        update_selected_journal(state, val) {
            state.selected_journal = val
        },
        update_summary(state, val) {
            state.summary = val
        },
        update_dialog_form_detail(state, val) {
            state.dialog_form_detail = val
        },
        update_periodeList(state, val) {
            state.periodeList = val
        },
        update_selected_periode(state, val) {
            state.selected_periode = val
        },
        update_loading(state, val) {
            state.loading = val
        },
        update_dialog_form_periode(state, val) {
            state.dialog_form_periode = val
        },
        update_dialog_form_confirm(state, val) {
            state.dialog_form_confirm = val
        },
        update_autocomplete_status(state, val) {
            state.autocomplete_status = val
        },
        update_search_status(state, val) {
            state.search_status = val
        },
        update_search_error_message(state, val) {
            state.search_error_message = val
        },
        update_total_journal(state, val) {
            state.total_journal = val
        },
        update_user(state, val) {
            state.user = val
        },
        update_jurnaldetails(state, val) {
            state.jurnaldetails = val
        },
        update_dialog_error_jurnal(state, val) {
            state.dialog_error_jurnal = val
        },
        update_msgerrjurnallist(state, val) {
            state.msgerrjurnallist = val
        },
        update_user(state, val) {
            state.user = val
        }
    },
    actions: {
        async getPeriode(context) {
            context.commit("update_autocomplete_status", 1)
            try {
                let prm = {}
                prm.token = one_token()
                let resp = await api.getPeriode(prm)
                if (resp.status != "OK") {
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_autocomplete_status", 2)
                    let data = resp.data

                    context.commit("update_periodeList", data)
                    // Ambil bulan dan tahun sekarang
                    let now = new Date();
                    let currentMonth = now.getMonth() + 1; // getMonth() dimulai dari 0 (Jan = 0)
                    let currentYear = now.getFullYear().toString(); // Ambil tahun dalam bentuk string

                    // Cari data dengan periodeMonth dan periodeYear yang cocok
                    let selectedPeriode = data.find(p => p.periodeMonth == currentMonth.toString() && p.periodeYear == currentYear);

                    // Jika tidak ditemukan, gunakan data pertama sebagai default
                    if (!selectedPeriode) {
                        selectedPeriode = data[0];
                    }

                    context.commit("update_selected_periode", selectedPeriode)
                }
            } catch (e) {
                context.commit("update_autocomplete_status", 3)
            }
        },
        async search(context, prm) {
            context.commit("update_search_status", 1)
            try {
                // var prm = {
                //     token: one_token(),
                //     periodeid: context.state.selected_periode.id
                // }
                // var prm = {}
                // prm.one_token()
                let resp = await api.search(one_token(), prm.xdate, prm.periodeid, prm.branchid, prm.regionalid)
                if (resp.status != "OK") {
                    context.commit("update_search_status", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_search_status", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    }

                    context.commit("update_journals", data.records)
                    context.commit("update_ishide", data.records[0].ishide)
                    context.commit("update_total_journal", data.total)

                }
            } catch (e) {
                context.commit("update_search_status", 3)
                context.commit("update_search_error_message", e.message)
            }
        },
        async getdetail(context, prm) {
            context.commit("update_search_status", 1)
            try {
                // var prm = {
                //     token: one_token(),
                //     periodeid: context.state.selected_periode.id
                // }
                // var prm = {}
                prm.token = one_token()
                let resp = await api.getdetail(prm)
                if (resp.status != "OK") {
                    context.commit("update_search_status", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_search_status", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    }

                    context.commit("update_branchlists", data.records)

                }
            } catch (e) {
                context.commit("update_search_status", 3)
                context.commit("update_search_error_message", e.message)
            }
        },

        async insertSales(context, prm) {
            context.commit("update_save_status", 1)
            try {
                let branchCode = prm.branchCode;
                let date = prm.date;
                let jurnalno = prm.jurnalno
                let resp = await api.insertSales(branchCode, date, jurnalno)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", resp.message)
                    let data = {
                        total: resp.data.total
                    }

                    context.commit("update_alert_success", true)
                    var msg = resp.data.msg
                    context.commit("update_msg_success", msg)
                    context.dispatch("search", {
                        current_page: context.state.current_page,
                        xdate: context.state.xdate,
                        periodeid: context.state.selected_periode.id
                    })
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                context.commit("update_alert_error", true)
            }
        },
        async addConfirm(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.addConfirm(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", resp.message)
                    let data = {
                        total: resp.data.total
                    }

                    context.commit("update_alert_success", true)
                    var msg = " Fixed aset periode " + context.state.selected_periode.periode + " berhasil dikonfirmasi"
                    context.commit("update_msg_success", msg)
                    context.dispatch("search", {
                        current_page: context.state.current_page,
                        xdate: context.state.xdate,
                        periodeid: context.state.selected_periode.id
                    })
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                context.commit("update_alert_error", true)
            }
        },
    }
}