{% load sri %}

{% sri_static 'taxsystem/libs/x-editable/1.5.8/css/bootstrap-editable.css' %}
{% sri_static 'taxsystem/libs/x-editable/1.5.8/js/bootstrap-editable.js' %}
{% sri_static 'taxsystem/js/modals/members-delete.js' %}
{% sri_static 'taxsystem/js/modals/payments-reject.js' %}
{% sri_static 'taxsystem/js/modals/payments-approve.js' %}
{% sri_static 'taxsystem/js/modals/payments-undo.js' %}
{% sri_static 'taxsystem/js/modals/payments-add.js' %}
{% sri_static 'taxsystem/js/modals/payments-delete.js' %}
{% sri_static 'taxsystem/js/modals/paymentsystem-switchuser.js' %}

<script>
    const paymentsystemTable = $('#payment-system');
    const paymentsTable = $('#payments');

    // Funktion zum Neuladen der Statistikdaten (erwartete neue, verschachtelte Form)
    function reloadStatistics() {
        $.ajax({
            url: taxsystemsettings.DashboardUrl,
            type: 'GET',
            success: function (data) {
                const statistics = data.statistics || {};

                // Neue (aktuelle) Form: statistics.payment_system, statistics.payments, statistics.members
                const paymentSystem = statistics.payment_system || {};
                $('#statistics_payment_users').text(paymentSystem.ps_count || 0);
                $('#statistics_payment_users_active').text(paymentSystem.ps_count_active || 0);
                $('#statistics_payment_users_inactive').text(paymentSystem.ps_count_inactive || 0);
                $('#statistics_payment_users_deactivated').text(paymentSystem.ps_count_deactivated || 0);
                $('#psystem_payment_users_paid').text(paymentSystem.ps_count_paid || 0);
                $('#psystem_payment_users_unpaid').text(paymentSystem.ps_count_unpaid || 0);

                const paymentsObj = statistics.payments || {};
                $('#statistics_payments').text(paymentsObj.payments_count || 0);
                $('#statistics_payments_pending').text(paymentsObj.payments_pending || 0);
                $('#statistics_payments_auto').text(paymentsObj.payments_automatic || 0);
                $('#statistics_payments_manually').text(paymentsObj.payments_manual || 0);

                const membersObj = statistics.members || {};
                $('#statistics_members_count').text(membersObj.members_count || 0);
            },
            error: function(xhr, status, error) {
                console.error('Error fetching statistics data:', error);
            }
        });
    }
</script>
