<table id="PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidlaDataTable" class="table table-hover"
       style="width:100%;margin:0!important;">
    <thead class="bg-secondary">
    <tr>
        <th class="text-left">Technik</th>
        <th class="text-center">B/BKAT</th>
        <th class="text-center">B/NKAT</th>
        <th class="text-center">B/RKAT</th>
        <th class="text-center">B/RKAT OBD</th>
        <th class="text-center">D/BKAT</th>
        <th class="text-center">D/NKAT</th>
        <th class="text-center">D/NKAT OBD</th>
        <th class="text-center">B/LPG/BKAT</th>
        <th class="text-center">B/LPG/NKAT</th>
        <th class="text-center">B/LPG/RKAT</th>
        <th class="text-center">B/LPG/RKAT OBD</th>
        <th class="text-center">B/NG/BKAT</th>
        <th class="text-center">B/NG/NKAT</th>
        <th class="text-center">B/NG/RKAT</th>
        <th class="text-center">B/NG/RKAT OBD</th>
        <th class="text-center">LPG/BKAT</th>
        <th class="text-center">LPG/NKAT</th>
        <th class="text-center">LPG/RKAT</th>
        <th class="text-center">LPG/RKAT OBD</th>
        <th class="text-center">NG/BKAT</th>
        <th class="text-center">NG/NKAT</th>
        <th class="text-center">NG/RKAT</th>
        <th class="text-center">NG/RKAT OBD</th>
        <th class="text-center">Spolu</th>
    </tr>
    </thead>
    <tfoot>
    <tr>
        <th class="text-left">Spolu</th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
        <th class="text-center"></th>
    </tr>
    </tfoot>
</table>

@push('scripts')
    <script type="text/javascript">
        $(document).ready(function () {
            $('#PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidlaDataTable').DataTable({
                dom: '<"wrapper"<"row"<"col"B><"col"f>><"row"<"col"<"card card-sm mt-1 mb-0"<"table-responsive"<t>>>>>>',
                language: {
                    url: 'https://cdn.datatables.net/plug-ins/1.10.24/i18n/Slovak.json',
                },
                data: @json($PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidlaDataTable ?? null),
                info: false,
                paging: false,
                searching: true,
                buttons: [
                    {
                        extend: 'print',
                        text: 'Tlačiť',
                        title: 'Počet kontrol podľa emisného systému vozidla za obdobie od {{ $datumOd }} do {{ $datumDo }}',
                        footer: true,
                    },
                    {
                        extend: 'pdfHtml5',
                        orientation: 'landscape',
                        pageSize: 'A4',
                        title: 'Počet kontrol podľa emisného systému vozidla za obdobie od {{ $datumOd }} do {{ $datumDo }}',
                        footer: true,
                        filename: 'pek_pocet_kontrol_podla_emisneho_systemu_vozidla_{{ $datumOd }}_{{ $datumDo }}',
                    },
                    {
                        extend: 'excelHtml5',
                        footer: false,
                        filename: 'pek_pocet_kontrol_podla_emisneho_systemu_vozidla_{{ $datumOd }}_{{ $datumDo }}',
                        sheetName: 'zoznam',
                    },
                ],
                columns: [
                    {
                        title: 'Technik',
                        data: 'label',
                        class: 'text-left',
                    },
                    {
                        data: 'data.emisnySystem.B_BKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_NKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_RKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_RKAT_OBD',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.D_BKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.D_NKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.D_NKAT_OBD',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_LPG_BKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_LPG_NKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_LPG_RKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_LPG_RKAT_OBD',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_NG_BKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_NG_NKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_NG_RKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.B_NG_RKAT_OBD',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.LPG_BKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.LPG_NKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.LPG_RKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.LPG_RKAT_OBD',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.NG_BKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.NG_NKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.NG_RKAT',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.emisnySystem.NG_RKAT_OBD',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.spolu',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                ],
                footerCallback: function (row, data, start, end, display) {
                    if (data.length === 0) {
                        $(row).hide()
                        return
                    }

                    const total = [
                        {index: 1, hodnota: getValue(this.api(), 1)},
                        {index: 2, hodnota: getValue(this.api(), 2)},
                        {index: 3, hodnota: getValue(this.api(), 3)},
                        {index: 4, hodnota: getValue(this.api(), 4)},
                        {index: 5, hodnota: getValue(this.api(), 5)},
                        {index: 6, hodnota: getValue(this.api(), 6)},
                        {index: 7, hodnota: getValue(this.api(), 7)},
                        {index: 8, hodnota: getValue(this.api(), 8)},
                        {index: 9, hodnota: getValue(this.api(), 9)},
                        {index: 10, hodnota: getValue(this.api(), 10)},
                        {index: 11, hodnota: getValue(this.api(), 11)},
                        {index: 12, hodnota: getValue(this.api(), 12)},
                        {index: 13, hodnota: getValue(this.api(), 13)},
                        {index: 14, hodnota: getValue(this.api(), 14)},
                        {index: 15, hodnota: getValue(this.api(), 15)},
                        {index: 16, hodnota: getValue(this.api(), 16)},
                        {index: 17, hodnota: getValue(this.api(), 17)},
                        {index: 18, hodnota: getValue(this.api(), 18)},
                        {index: 19, hodnota: getValue(this.api(), 19)},
                        {index: 20, hodnota: getValue(this.api(), 20)},
                        {index: 21, hodnota: getValue(this.api(), 21)},
                        {index: 22, hodnota: getValue(this.api(), 22)},
                        {index: 23, hodnota: getValue(this.api(), 23)},
                        {index: 24, hodnota: getValue(this.api(), 24)},
                    ]

                    setFooter(total, this.api())
                },
            })

            function getValue(api, col) {
                return api.column(col, {page: 'current'}).data().reduce(function (a, b) {
                    return Number(a) + Number(b)
                }, 0)
            }

            function setFooter(cols, api) {
                cols.forEach(function (col) {
                    $(api.column(col.index).footer()).html(col.hodnota)
                })
            }
        })
    </script>
@endpush