<table id="PocetKontrolSObratomDataTable" 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">Počet kontrol</th>
        <th class="text-center">Obrat</th>
    </tr>
    </thead>
    <tbody></tbody>
    <tfoot>
    <tr>
        <th class="text-left">Spolu</th>
        <th class="text-center"></th>
        <th class="text-center"></th>
    </tr>
    </tfoot>
</table>

@push('scripts')
    <script type="text/javascript">
        $(document).ready(function () {
            $('#PocetKontrolSObratomDataTable').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($PocetKontrolSObratomDataTable ?? null),
                info: false,
                paging: false,
                searching: true,
                buttons: [
                    {
                        extend: 'print',
                        text: 'Tlačiť',
                        title: 'Počet kontrol podľa obratu za obdobie od {{ $datumOd }} do {{ $datumDo }}',
                        footer: true,
                    },
                    {
                        extend: 'pdfHtml5',
                        orientation: 'landscape',
                        pageSize: 'A4',
                        title: 'Počet kontrol podľa obratu za obdobie od {{ $datumOd }} do {{ $datumDo }}',
                        footer: true,
                        filename: 'pek_pocet_kontrol_podla_obratu_{{ $datumOd }}_{{ $datumDo }}',
                    },
                    {
                        extend: 'excelHtml5',
                        footer: false,
                        filename: 'tek_pocet_kontrol_podla_obratu_{{ $datumOd }}_{{ $datumDo }}',
                        sheetName: 'zoznam',
                    },
                ],
                columns: [
                    {
                        title: 'Technik',
                        data: 'label',
                        class: 'text-left',
                    },
                    {
                        data: 'data.spolu',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                    {
                        data: 'data.obrat',
                        defaultContent: 0,
                        class: 'text-center',
                    },
                ],
                rowCallback: function (row, data) {
                    $('td:eq(2)', row).html(Number(data.data.obrat).toFixed(2) + ' EUR')
                },
                footerCallback: function (row, data, start, end, display) {
                    if (data.length === 0) {
                        $(row).hide()
                        return
                    }

                    const total = {
                        pocet: this.api().column(1, {page: 'current'}).data().reduce(function (a, b) {
                            return a + b
                        }, 0),
                        obrat: this.api().column(2, {page: 'current'}).data().reduce(function (a, b) {
                            return a + b
                        }, 0),
                    }

                    $(this.api().column(1).footer()).html(total.pocet)
                    $(this.api().column(2).footer()).html(Number(total.obrat).toFixed(2) + ' EUR')
                },
            })
        })
    </script>
@endpush