<table id="PocetKontrolAObratuPodlaSubjektuFakturacieDataTable" class="table table-hover"
       style="width:100%;margin:0!important;">
    <thead class="bg-secondary">
    <tr>
        <th class="text-left">Názov subjektu</th>
        <th class="text-center">Počet</th>
        <th class="text-center">Obrat [EUR]</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 () {
            $('#PocetKontrolAObratuPodlaSubjektuFakturacieDataTable').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($PocetKontrolAObratuPodlaSubjektuFakturacieDataTable ?? null),
                paging: false,
                searching: true,
                buttons: [
                    {
                        extend: 'print',
                        text: 'Tlačiť',
                        title: 'Počet kontrol podľa subjektu fakturácie za obdobie od {{ $datumOd }} do {{ $datumDo }}',
                        footer: true,
                    },
                    {
                        extend: 'pdfHtml5',
                        orientation: 'landscape',
                        pageSize: 'A4',
                        title: 'Počet kontrol podľa subjektu fakturácie za obdobie od {{ $datumOd }} do {{ $datumDo }}',
                        footer: true,
                        filename: 'pek_pocet_kontrol_podla_subjektu_a_fakturacie_{{ $datumOd }}_{{ $datumDo }}',
                        customize: function (doc) {
                            let colCount = []

                            $('#PocetKontrolAObratuPodlaSubjektuFakturacieDataTable').find('tbody tr:first-child td').each(function () {
                                if ($(this).attr('colspan')) {
                                    for (var i = 1; i <= $(this).attr('colspan'); i++) {
                                        colCount.push('*')
                                    }
                                } else {
                                    colCount.push('*')
                                }
                            })

                            doc.content[1].table.widths = colCount
                        },
                    },
                    {
                        extend: 'excelHtml5',
                        footer: false,
                        filename: 'pek_pocet_kontrol_podla_subjektu_a_fakturacie_{{ $datumOd }}_{{ $datumDo }}',
                        sheetName: 'zoznam',
                    },
                ],
                columns: [
                    {
                        title: 'Názov subjektu',
                        data: 'label',
                        class: 'text-left',
                    },
                    {
                        data: 'data.spolu',
                        class: 'text-center',
                    },
                    {
                        data: 'data.obrat',
                        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