<div class="table-responsive">
    <table id="TekZoznamPoctuKontrolSObratomDataTable" class="table table-hover" style="width:100%">
        <thead class="bg-secondary"/>
        <tbody></tbody>
        <tfoot>
        <tr>
            <th class="text-left">Spolu</th>
            @foreach($TekZoznamPoctuKontrolSObratomDataTableComposer['stlpce'] as $stlpec)
                @if(!$loop->last)
                    <th class="text-left"></th>
                @endif
            @endforeach
        </tr>
        </tfoot>
    </table>
</div>

@push('scripts')
    <script type="text/javascript">
      $(document).ready(function () {
        $('#TekZoznamPoctuKontrolSObratomDataTable').DataTable({
          dom: '<"wrapper"<"row"<"col"B><"col"f>><t>>',
          language: {
            url: 'https://cdn.datatables.net/plug-ins/1.10.24/i18n/Slovak.json',
          },
          columns: @json(empty($TekZoznamPoctuKontrolSObratomDataTableComposer['stlpce']) ? null : $TekZoznamPoctuKontrolSObratomDataTableComposer['stlpce']),
          data: @json(empty($TekZoznamPoctuKontrolSObratomDataTableComposer['udaje']) ? null : $TekZoznamPoctuKontrolSObratomDataTableComposer['udaje']),
          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',
            },
          ],
          rowCallback: function (row, data) {
            $('td:eq(1)', row).addClass('text-left')
            $('td:eq(2)', row).html(data.obrat + ' EUR').addClass('text-left')
          },
          footerCallback: function (row, data, start, end, display) {
            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(total.obrat + ' EUR')
          },
        })
      })
    </script>
@endpush