$(document).ready(function () {
    function formatToRupiah(amount) {
        return new Intl.NumberFormat('id-ID', {
            style: 'currency',
            currency: 'IDR',
            maximumFractionDigits: 0,
            useGrouping: true
        }).format(amount).replace('Rp ', 'Rp').replace(/\s/g, '');
    }

    $("#filter-button").on("click", function (e) {
        e.preventDefault();

        const startDate = $("#start_date").val();
        const endDate = $("#end_date").val();

        $.ajax({
            url: "/dashboard/analytic/filter",
            method: "GET",
            data: {
                start_date: startDate,
                end_date: endDate,
                _token: $('meta[name="csrf-token"]').attr('content')
            },
            success: function (response) {
                $("#total-asset").text(formatToRupiah(response.data.totalAsset));
                $("#net-income").text(formatToRupiah(response.data.totalIncome + response.data.totalExpense));
                $("#total-barber").text(response.data.totalBarber);
                $("#total-user").text(response.data.totalUser);
                $("#top-barber-name").text(response.data.topBarber.biography.full_name || "-deleted-");
                $("#top-barber-total").text(formatToRupiah(response.data.topBarber.transactions_sum_total));

                // income expense
                let totalIncome = response.data.totalIncome;
                let totalExpense = response.data.totalExpense;

                incomeExpenseChart.data.datasets[0].data = [
                    totalIncome,
                    totalExpense,
                ];
                incomeExpenseChart.update();

                // top service
                let topService = response.data.topService;
                let topServiceLabels = topService.map(item => item.name);
                let topServiceData = topService.map(item => item.transactions_count);

                topServiceChart.data.labels = topServiceLabels;
                topServiceChart.data.datasets[0].data = topServiceData;
                topServiceChart.update();

                // payment method
                let paymentMethod = response.data.paymentMethod;
                let paymentMethodLabels = paymentMethod.map(item => {
                    switch (item.payment_method) {
                        case '1':
                            return 'Tunai';
                        case '2':
                            return 'Qris';
                        case '3':
                            return 'Lainnya';
                        default:
                            return 'Unknown';
                    }
                });
                let paymentMethodData = paymentMethod.map(item => item.total_amount);

                paymentMethodChart.data.labels = paymentMethodLabels;
                paymentMethodChart.data.datasets[0].data = paymentMethodData;
                paymentMethodChart.update();

                // income by barber
                let updatedIncomeByBarber = response.data.incomeByBarber;
                let updatedLabels = updatedIncomeByBarber.map(item => item.biography.full_name);
                let updatedData = updatedIncomeByBarber.map(item => item.transactions_sum_total);

                incomeByBarberChart.data.labels = updatedLabels;
                incomeByBarberChart.data.datasets[0].data = updatedData;
                incomeByBarberChart.update();

                // transaction by barber
                let transactionByBarber = response.data.transactionByBarber;
                let transactionByBarberLabels = transactionByBarber.map(item => item.biography.full_name);
                let transactionByBarberData = transactionByBarber.map(item => item.transactions_count);

                transactionByBarberChart.data.labels = transactionByBarberLabels;
                transactionByBarberChart.data.datasets[0].data = transactionByBarberData;
                transactionByBarberChart.update();
            },
            error: function (xhr) { },
        });
    });
});
