{% load sri %}
{% load i18n %}

<script>
    // Initialize dropdowns on page load
    document.addEventListener('DOMContentLoaded', function() {
        const BillboardData = {{ billboard|default:"null"|safe }};
        initCharts(BillboardData);

        const tableContainer = $('#ratting')
        const tableRatting = tableContainer.DataTable(
            {
                "order": [[1, "desc"]],
                "columnDefs": [
                    { "orderable": false, "targets": 7 }, // Disable sorting on the last column
                    {
                        "targets": [1, 2, 3, 4, 5],
                        "render": function (data, type, row, meta) {
                            // Remove ISK and thousands separator for sorting
                            if (type === 'sort' || type === 'type') {
                                if (typeof data === 'string') {
                                    return parseFloat(data.replace(/[^\d\-\.]/g, '')) || 0;
                                }
                                return data;
                            }
                            return data;
                        }
                    },
                    {
                        "targets": 6, // Total column
                        "render": function (data, type, row, meta) {
                            if (type === 'sort' || type === 'type') {
                                if (typeof data === 'string') {
                                    // Extrahiere Zahl inkl. Minuszeichen am Anfang, robust gegen Leerzeichen
                                    let match = data.match(/-?\d[\d.,]*/);
                                    if (match) {
                                        // Ersetze Kommas durch nichts, Punkte durch Dezimalpunkt
                                        let num = match[0].replace(/\./g, '').replace(/,/g, '.');
                                        return parseFloat(num) || 0;
                                    }
                                    return 0;
                                }
                                return data;
                            }
                            return data;
                        }
                    }
                ],
                "orderCellsTop": true,
                initComplete: function() {
                    $('[data-tooltip-toggle="ledger-tooltip"]').tooltip({ trigger: 'hover' });
                },
            }
        );

        tableRatting.on('draw', function() {
            $('[data-tooltip-toggle="ledger-tooltip"]').tooltip({ trigger: 'hover' });
        });

        // Show the ledger content after initialization
        $('#ledger-index').addClass('show');
    });
</script>

{% sri_static 'ledger/js/charts.js' %}
{% sri_static 'ledger/js/modal/modal-system.js' %}
