jQuery($ => {
  const $table = $('#table');
  const $clearLogsBtn = $('#exp-clear-logs');

  let jobTable = $table.DataTable({
    processing: true,
    serverSide: true,
    ajax: {
      url: datatablesajax.url,
      data: { action: 'exp_get_logs', nonce : datatablesajax.nonce }
    },
    columnDefs: [{ searchable: false, targets: 6 }],
    dom: 'Bfrtip',
    buttons: [{ extend: 'colvis', columnText: (dt, idx, title) => (idx + 1) + ': ' + title }],
    order: [[0, 'desc']]
  });

  $clearLogsBtn.click(e => {
    e.preventDefault();
    if (confirm('Are you sure?')) {
      clearLogs();
    }
  });

  function clearLogs() {
    $.ajax({
      url: datatablesajax.url,
      type: 'post',
      dataType: 'json',
      data: { action: 'exp_clear_logs', data: true, nonce: datatablesajax.clear_logs_nonce }
    })
      .done(() => location.reload())
      .fail(result => alert(`Sorry there was an error. ${result.responseJSON.message}`));
  }

});