// 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 Lunas',
            value: 'N'
        }, {
            name: 'Lunas',
            value: 'Y'
        }],
        selected_status: {
            name: 'Belum Lunas',
            value: 'N'
        },
        selected_multibill: [],
        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: '',
        total_payment : 0,
        xdialogmulti: false,
        xrounding: 0,
        paymenttypes: [],
        paymenttype: {},
        xtanggalbayar: '',
        a_coa_search: '',
        a_loading_coa: false,
        in_saving: false,
        dialog_pay_success: false,
        paynumber: '',
        bar_chx_all: false,
        indeterminatex: false
    },
    mutations: {
        update_bar_chx_all(state, val) {
            state.bar_chx_all = val
        },
        update_indeterminatex(state, val) {
            state.indeterminatex = val
        },
        update_paynumber(state, val) {
            state.paynumber = val
        },
        update_dialog_pay_success(state, val) {
            state.dialog_pay_success = val
        },
        update_in_saving(state, val) {
            state.in_saving = val
        },
        update_a_loading_coa(state, val) {
            state.a_loading_coa = val
        },
        update_a_coa_search(state, val) {
            state.a_coa_search = val
        },
        update_xtanggalbayar(state, val) {
            state.xtanggalbayar = val
        },
        update_paymenttypes(state, data) {
            state.paymenttypes = data
        },
        update_paymenttype(state, val) {
            state.paymenttype = val
        },
        update_xrounding(state, val) {
            state.xrounding = val
        },
        update_xdialogmulti(state, val) {
            state.xdialogmulti = val
        },
        update_total_payment(state, val) {
            state.total_payment = val
        },
        update_selected_multibill(state, val) {
            state.selected_multibill = 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])
                        if (!_.isEmpty(data.records[0])) {
                            context.commit("paymentmanual/update_xbayar", data.records[0].totalbill, {root: true})
                            context.commit("paymentmanual/update_xtanggalbayar", data.records[0].SupplierInvoiceDraftPaymentDate, {root: true})
                            context.commit("paymentnew/update_notes", data.records[0].notes, {root: true})
                            context.commit("paymentmanual/update_notes", data.records[0].notes, {root: true})

                            if (data.records[0].flaglunas === 'N') {
                                context.commit("paymentmanual/update_tagihans", data.records[0].tagihans, {
                                    root: true
                                })
                            } else {
                                context.commit("paymentmanual/update_tagihans", [], {
                                    root: true
                                })
                            }

                            var arr = context.rootState.paymentmanual.tagihans
                            var value = true
                            context.commit("paymentmanual/update_indeterminatex", false, {root: true})
                            context.commit("paymentmanual/update_bar_chx_all", value, {root: true})

                            arr.forEach((el) => {
                            el.chex = value
                            el.tagihan_bayar = value == true ? el.tagihan_tagihan : 0
                                })

                            var selected = _.filter(arr, function (o) {
                            return o.chex;
                             })
                            context.commit("paymentmanual/update_selected_tagihan", selected, {
                            root: true
                            })

                            var xval = context.rootState.paymentmanual.selected_tagihan
                            var valx = 0
                            xval.forEach(function (obj) {
                            valx += parseInt(obj.tagihan_bayar)
                            })
                            let totpaid = valx
                            context.commit("paymentmanual/update_total_payment", totpaid, {root: true})
                        } else {
                            context.commit("paymentnew/update_notes", [], {
                                root: true
                            })
                            context.commit("paymentmanual/update_notes", [], {
                                root: true
                            })
                            context.commit("paymentmanual/update_tagihans", [], {
                                root: true
                            })

                        }
                    } else if (prm.lastidx > 0 && data.total > 0) {
                        context.commit("update_selected_bill", data.records[0])
                        context.commit("paymentmanual/update_xbayar", data.records[0].totalbill, {
                            root: true
                        })
                        context.commit("paymentnew/update_notes", data.records[prm.lastidx].notes, {
                            root: true
                        })
                        context.commit("paymentmanual/update_notes", data.records[prm.lastidx].notes, {
                            root: true
                        })
                        context.commit("paymentmanual/update_xtanggalbayar", data.records[prm.lastidx].SupplierInvoiceDraftPaymentDate, {
                            root: true
                        })
                        if (data.records[prm.lastidx].flaglunas === 'N') {
                            context.commit("paymentmanual/update_tagihans", data.records[prm.lastidx].tagihans, {
                                root: true
                            })
                        } else {
                            context.commit("paymentmanual/update_tagihans", [], {
                                root: true
                            })
                        }
                        var arr = context.rootState.paymentmanual.tagihans
                            var value = true
                            context.commit("paymentmanual/update_indeterminatex", false, {root: true})
                            context.commit("paymentmanual/update_bar_chx_all", value, {root: true})

                            arr.forEach((el) => {
                            el.chex = value
                            el.tagihan_bayar = value == true ? el.tagihan_tagihan : 0
                                })

                            var selected = _.filter(arr, function (o) {
                            return o.chex;
                             })
                            context.commit("paymentmanual/update_selected_tagihan", selected, {
                            root: true
                            })

                            var xval = context.rootState.paymentmanual.selected_tagihan
                            var valx = 0
                            xval.forEach(function (obj) {
                            valx += parseInt(obj.tagihan_bayar)
                            })
                            let totpaid = valx
                            context.commit("paymentmanual/update_total_payment", totpaid, {root: true})

                    } else {
                        context.commit("update_selected_bill", '')
                        context.commit("paymentnew/update_notes", [], {
                            root: true
                        })
                        context.commit("paymentmanual/update_notes", [], {
                            root: true
                        })
                        context.commit("paymentmanual/update_tagihans", [], {
                            root: true
                        })
                        context.commit("paymentmanual/update_xtanggalbayar", '', {
                            root: true
                        })
                        context.commit("paymentmanual/update_xbayar", 0, {
                            root: true
                        })
                        context.commit("paymentmanual/update_total_payment", 0, {root: true})
                        context.commit("paymentmanual/update_bar_chx_all", false, {root: true})
                    }

                }
            } catch (e) {
                context.commit("update_search_bill", 3)
                context.commit("update_search_error_message", e.message)
                console.log(e)
            }
        },
        async selectpaymenttype(context,payload) {
            context.commit("update_get_data_status", 1)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.selectpaymenttype(prm)
                if (resp.status != "OK") {
                    context.commit("update_get_data_status", 3)
                    context.commit("update_get_data_error_message", resp.message)
                } else {
                    context.commit("update_get_data_status", 2)
                    context.commit("update_get_data_error_message", "")
                    context.commit("update_paymenttypes", resp.data.records.paymenttypes)
                    context.commit("update_paymenttypes_edit", resp.data.records.paymenttypes)

                }
            } catch (e) {
                context.commit("update_get_data_status", 3)
                context.commit("update_get_data_error_message", e.message)
                console.log(e)
            }
        },
        async pay(context, prm) {
            context.commit("paymentmanual/update_lookup_status", 1, {root: true})
            try {
                prm.token = one_token()
                let resp = await api.pay(prm)
                if (resp.status != "OK") {
                    context.commit("paymentmanual/update_lookup_status", 3, {root: true})
                    context.commit("paymentmanual/update_lookup_error_message", resp.message, {root: true})
                } else {
                    context.commit("paymentmanual/update_lookup_status", 2, {root: true})
                    context.commit("paymentmanual/update_lookup_error_message", "", {root: true})
                    context.commit("update_paynumber", "Pembayaran multi telah berhasil")
                    context.commit("update_dialog_pay_success", true)
                    context.commit("update_in_saving", false)
                    context.commit("update_xdialogmulti", false)
                    console.log('berhasil bayar')
                    context.dispatch("search", {
                        supplier: context.state.ssupplier,
                        search: context.state.search,
                        status: context.state.selected_status.value,
                        current_page: 1,
                        startdate: context.state.init_sdate,
                        enddate: context.state.init_edate,
                        lastidx: 0
                    })
                }
            } catch (e) {
                context.commit("paymentmanual/update_lookup_status", 3, {root: true})
                context.commit("paymentmanual/update_lookup_error_message", e.message, {root: true})
            }
        },
    }
}
