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

export default {
    namespaced: true,
    state: {
        search_bill: 0,
        search_error_message: '',
        start_date: moment(new Date()).format('YYYY-MM-DD'),
        end_date: moment(new Date()).format('YYYY-MM-DD'),
        init_sdate: moment(new Date()).format('YYYY-MM-DD'),
        init_edate: moment(new Date()).format('YYYY-MM-DD'),
        search: '',
        ssupplier: '',
        isauto:'N',
        bills: [],
        total_bill: 0,
        selected_bill: {
            totalbill: 0,
            mindp_percent: 0
        },
        save_error_message: '',
        statuses: [{
            name: 'Belum Konfirmasi',
            value: 'N'
        }, {
            name: 'Konfirmasi',
            value: 'Y'
        }],
        selected_status: {
            name: 'Belum Konfirmasi',
            value: 'N'
        },
        open_alert_no_pay: false,
        msg_alert_no_pay: "Loh ... Gak jadi bayar dong ?",
        current_page: 1,
        total_page: 0,
        pay_disabled: 'Y',
        get_data_status: 0,
        get_data_error_message: '',
        dialog_form_order: false,
        suppliers: [],
        supplier: {},
        invpayments: [],
        invpayment: {},
        errors: [],
        act_comp: 'new',
        in_saving: false
    },
    mutations: {
        update_in_saving(state, val) {
            state.in_saving = val
        },
        update_act_comp(state, val) {
            state.act_comp = val
        },
        update_errors(state, val) {
            state.errors = val
        },
        update_suppliers(state, val) {
            state.suppliers = val
        },
        update_supplier(state, val) {
            state.supplier = val
        },
        update_invpayments(state, val) {
            state.invpayments = val
        },
        update_invpayment(state, val) {
            state.invpayment = val
        },
        update_dialog_form_order(state, val) {
            state.dialog_form_order = val
        },
        update_init_sdate(state, val) {
            state.init_sdate = val
        },
        update_init_edate(state, val) {
            state.init_edate = val
        },
        update_pay_disabled(state, val) {
            state.pay_disabled = val
        },
        update_total_page(state, val) {
            state.total_page = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_search_error_message(state, bill) {
            state.search_error_message = bill
        },
        update_search_bill(state, bill) {
            state.search_bill = bill
        },
        update_bills(state, data) {
            state.bills = data.records
            state.total_bill = data.total
            state.total_page = data.total
        },
        update_selected_bill(state, val) {
            state.selected_bill = val
        },
        update_start_date(state, val) {
            state.start_date = val
        },
        update_end_date(state, val) {
            state.end_date = val
        },
        update_search(state, val) {
            state.search = val
        },
        update_ssupplier(state, val) {
            state.ssupplier = val
        },
        update_selected_status(state, val) {
            state.selected_status = val
        },
        update_open_alert_no_pay(state, val) {
            state.open_alert_no_pay = val
        },
        update_msg_alert_no_pay(state, val) {
            state.msg_alert_no_pay = val
        },
        update_isauto(state, val) {
            state.isauto = val
        },
        update_get_data_status(state, val) {
            state.get_data_status = val
        },
        update_get_data_error_message(state, val) {
            state.get_data_error_message = val
        }
    },
    actions: {
        async search(context, prm) {
            context.commit("update_search_bill", 1)
            try {
                prm.token = one_token()
                let resp = await api.search(prm)
                if (resp.status != "OK") {
                    context.commit("update_search_bill", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_search_bill", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_bills", data)
                    context.commit("update_total_page", data.total)
                    if (prm.lastidx === 0 && data.total > 0) {
                        context.commit("update_selected_bill", data.records[0])
                        
                    } else if (prm.lastidx > 0 && data.total > 0) {
                        context.commit("update_selected_bill", data.records[prm.lastidx])
                    } else {
                        context.commit("update_selected_bill", '')
                    }

                }
            } catch (e) {
                context.commit("update_search_bill", 3)
                context.commit("update_search_error_message", e.message)
                console.log(e)
            }
        },
        async searchsupplier(context, prm) {
                    context.commit("update_search_bill", 1)
                    try {
                        prm.token = one_token()
                        let resp = await api.searchsupplier(prm)
                        if (resp.status != "OK") {
                            context.commit("update_search_bill", 3)
                        } else {
                            context.commit("update_search_bill", 2)
                            let data = {
                                records: resp.data.records,
                                total: resp.data.total
                            }
                            context.commit("update_suppliers", resp.data.records)
                        }
                    } catch (e) {
                        context.commit("update_search_bill", 3)
                    }
                },
                async searchinvpayment(context, prm) {
                    context.commit("update_search_bill", 1)
                    try {
                        prm.token = one_token()
                        let resp = await api.searchinvpayment(prm)
                        if (resp.status != "OK") {
                            context.commit("update_search_bill", 3)
                        } else {
                            context.commit("update_search_bill", 2)
                            let data = {
                                records: resp.data.records,
                                total: resp.data.total
                            }
                            context.commit("update_invpayments", resp.data.records)
                        }
                    } catch (e) {
                        context.commit("update_search_bill", 3)
                    }
                },
    }
}