$(function () {
  'use strict';
  $('[data-toggle="tooltip"]').tooltip();
  var table = $('#data').DataTable({
    "columnDefs": [{"sortable": false, "targets": [0, 4]}],
    "order": [[3, "asc"], [0, "desc"]],
    "lengthMenu": [[100, 200, 500], [100, 200, 500]],
    "ajax": $('#route-sending-domains').data('route'),
    "drawCallback": function ( settings, data ) {
      var api = this.api();
      var rows = api.rows( {page:'current'} ).nodes();
      var last=null;
      var groupColumn=3;
      api.column(groupColumn, {page:'current'} ).data().each( function ( group, i ) {
         var spanElement = $(group);
         var group_id = spanElement.attr('id');

         let url = $('#route-group-edit').data('route');
         let route_url = url.replace("group-id", group_id);

        if ( last !== group ) {
          $(rows).eq( i ).before(
            '<tr class="dt_group"><td colspan="9" class="fw-bold">'+group+'</td>'+
            '<td>'+
              '&nbsp;<i data-bs-toggle="tooltip" data-bs-placement="top" class="bi bi-pencil-square" style="cursor: pointer;" title="'+$('#tooltip-group-edit').data('value')+'" onclick="viewModal(\'modal\', \''+route_url+'\')"></i>'+
              '&nbsp;&nbsp;<i data-bs-toggle="tooltip" data-bs-placement="top" class="bi bi-trash" style="cursor: pointer;" title="'+$('#tooltip-group-delete-list').data('value')+'" onclick="deleteGroup(this, \''+$('#msg-group-delete-list').data('value')+'\')"></i>'+
              '&nbsp;&nbsp;<i data-bs-toggle="tooltip" data-bs-placement="top" class="bi bi-eraser" style="cursor: pointer;" title="'+$('#tooltip-group-eraser-list').data('value')+'" onclick="eraseGroup(this, \''+$('#route-group-eraser-list').data('route')+'\', \''+$('#msg-group-erase-list').data('value')+'\')"></i>'+
            '</td></tr>'
          );
          last = group;
        }
      });
      // Reinitialize Bootstrap tooltips after DataTable redraw
      $('[data-bs-toggle="tooltip"]').tooltip();
    },
  });
  table.on('draw', function() {
    $('#data_paginate a').addClass('btn btn-primary me-1');
    $('#data_paginate .paginate_button.current').addClass('btn-light').removeClass('btn-primary');
  });
});