const table = $j('#logTable');
var ajax = null;

/*
This is the format of the json object sent by bootstrap-table

var params =
{
"type":"get",
"data":
  {
  "search":"some search text",
  "sort":"DateTime",
  "order":"asc",
  "offset":0,
  "limit":25
  "filter":
    {
    "message":"some advanced search text"
    "level":"some more advanced search text"
    }
  },
"cache":true,
"contentType":"application/json",
"dataType":"json"
};
*/

// Called by bootstrap-table to retrieve zm log data
function ajaxRequest(params) {
  if ($j('#filterServerId').val()) {
    params.data.ServerId = $j('#filterServerId').val();
  }
  if ($j('#filterLevel').val()) {
    params.data.level = $j('#filterLevel').val();
  }
  if ($j('#filterComponent').val()) {
    params.data.Component = $j('#filterComponent').val();
  }
  if ($j('#filterStartDateTime').val()) {
    params.data.StartDateTime = $j('#filterStartDateTime').val();
  }
  if ($j('#filterEndDateTime').val()) {
    params.data.EndDateTime = $j('#filterEndDateTime').val();
  }

  if (ajax) ajax.abort();
  ajax = $j.ajax({
    url: thisUrl + '?view=request&request=log&task=query',
    data: params.data,
    timeout: 0,
    success: function(data) {
      if (!data.rows.length) {
        // If page is > 1, bt infinitely loops
        table.bootstrapTable('selectPage', 1);
      }
      // rearrange the result into what bootstrap-table expects
      params.success({
        total: data.total,
        totalNotFiltered: data.totalNotFiltered,
        rows: processRows(data.rows)
      });
      updateHeaderStats(data);
    },
    error: function(jqxhr) {
      logAjaxFail(jqxhr);
    }
  });
}

function processRows(rows) {
  $j.each(rows, function(ndx, row) {
    try {
      row.Message = decodeURIComponent(row.Message)
          .replace(/</g, "&lt;").replace(/>/g, "&gt;") // Replace link tags
          .replace(/event (\d+)/g, "<a href=\"?view=event&eid=$1\">event $1</a>");
    } catch (e) {
      console.log("Error decoding ", row.Message, e);
      // ignore errors
    }
  });
  return rows;
}

function filterLog() {
  table.bootstrapTable('refresh');
}

function updateHeaderStats(data) {
  var pageNum = table.bootstrapTable('getOptions').pageNumber;
  var pageSize = table.bootstrapTable('getOptions').pageSize;
  var startRow = ( (pageNum - 1 ) * pageSize ) + 1;
  var stopRow = pageNum * pageSize;
  var newClass = (data.logstate == 'ok') ? 'text-success' : (data.logstate == 'alert' ? 'text-warning' : ((data.logstate == 'alarm' ? 'text-danger' : '')));

  $j('#logState').text(data.logstate);
  $j('#logState').removeClass('text-success');
  $j('#logState').removeClass('text-warning');
  $j('#logState').removeClass('text-danger');
  $j('#logState').addClass(newClass);

  $j('#totalLogs').text(data.total);
  $j('#availLogs').text(data.totalNotFiltered);
  $j('#lastUpdate').text(data.updated);
  $j('#displayLogs').text(startRow + ' to ' + stopRow);
}

function manageClearLogsModalBtns() {
  document.getElementById('clearLogsConfirmBtn').addEventListener('click', function onClearLogsConfirmClick(evt) {
    evt.preventDefault();
    document.getElementById('clearLogsConfirmBtn').disabled = true;
    deleteLogs(getIdSelections());
  });
  document.getElementById('clearLogsCancelBtn').addEventListener('click', function onClearLogsCancelClick(evt) {
    $j('#clearLogsConfirm').modal('hide');
  });
}

function getIdSelections() {
  return $j.map(table.bootstrapTable('getSelections'), function(row) {
    return row.Id;
  });
}

function deleteLogs(log_ids) {
  const ticker = document.getElementById('clearLogsProgressTicker');
  const chunk = log_ids.splice(0, 100);
  console.log('Deleting ' + chunk.length + ' log entries. ' + log_ids.length + ' remaining.');

  $j.ajax({
    method: 'post',
    timeout: 0,
    url: thisUrl + '?request=log&task=delete',
    data: {'ids[]': chunk},
    success: function(data) {
      if (!log_ids.length) {
        $j('#clearLogsConfirm').modal('hide');
        table.bootstrapTable('refresh');
      } else {
        if (ticker.innerHTML.length < 1 || ticker.innerHTML.length > 10) {
          ticker.innerHTML = '.';
        } else {
          ticker.innerHTML = ticker.innerHTML + '.';
        }
        deleteLogs(log_ids);
      }
    },
    error: function(jqxhr) {
      logAjaxFail(jqxhr);
      $j('#clearLogsConfirm').modal('hide');
      table.bootstrapTable('refresh');
    }
  });
}

function initPage() {
  var backBtn = $j('#backBtn');

  // Init the bootstrap-table with custom icons
  table.bootstrapTable({icons: icons});

  // Assign inf, err, fat, dbg color classes to the rows in the table
  table.on('post-body.bs.table', function(data) {
    var lvl_ndx = $j('#logTable tr th').filter(function() {
      return $j(this).attr('data-field') == "Code";
    }).index();

    $j('#logTable tr').each(function(ndx, row) {
      var row = $j(row);
      var level = row.find('td').eq(lvl_ndx).text().trim();

      if (( level == 'FAT' ) || ( level == 'PNC' )) {
        row.addClass('log-fat');
      } else if ( level == 'ERR' ) {
        row.addClass('log-err');
      } else if ( level == 'WAR' ) {
        row.addClass('log-war');
      } else if ( level == 'DBG' ) {
        row.addClass('log-dbg');
      }
    });
  });

  // Don't enable the back button if there is no previous zm page to go back to
  backBtn.prop('disabled', !document.referrer.length);

  // Manage the BACK button
  document.getElementById("backBtn").addEventListener("click", function onBackClick(evt) {
    evt.preventDefault();
    window.history.back();
  });

  // Manage the REFRESH Button
  document.getElementById("refreshBtn").addEventListener("click", function onRefreshClick(evt) {
    evt.preventDefault();
    window.location.reload(true);
  });

  // Manage the CLEAR LOGS button
  const clearLogsBtn = document.getElementById('clearLogsBtn');
  if (clearLogsBtn) {
    clearLogsBtn.addEventListener('click', function onClearLogsClick(evt) {
      evt.preventDefault();
      if (evt.ctrlKey) {
        // Bypass confirmation, but ensure the modal (and its ticker) exists
        if (!document.getElementById('clearLogsConfirm')) {
          $j.getJSON(thisUrl + '?request=modal&modal=clearlogsconfirm')
              .done(function(data) {
                insertModalHtml('clearLogsConfirm', data.html);
                manageClearLogsModalBtns();
                // Do not show the modal; just ensure required elements exist
                deleteLogs(getIdSelections());
              })
              .fail(function(jqXHR) {
                console.log('error getting clearlogsconfirm', jqXHR);
                logAjaxFail(jqXHR);
              });
        } else {
          deleteLogs(getIdSelections());
        }
      } else {
        if (!document.getElementById('clearLogsConfirm')) {
          $j.getJSON(thisUrl + '?request=modal&modal=clearlogsconfirm')
              .done(function(data) {
                insertModalHtml('clearLogsConfirm', data.html);
                manageClearLogsModalBtns();
                $j('#clearLogsConfirm').modal('show');
              })
              .fail(function(jqXHR) {
                console.log('error getting clearlogsconfirm', jqXHR);
                logAjaxFail(jqXHR);
              });
        } else {
          document.getElementById('clearLogsConfirmBtn').disabled = false;
          $j('#clearLogsConfirm').modal('show');
        }
      }
    });
  }

  // Enable or disable clear button based on selection
  table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function() {
    const selections = table.bootstrapTable('getSelections');
    const clearLogsBtn = document.getElementById('clearLogsBtn');
    if (clearLogsBtn) {
      clearLogsBtn.disabled = !selections.length;
    }
  });

  $j('#filterStartDateTime, #filterEndDateTime')
      .datetimepicker({timeFormat: "HH:mm:ss", dateFormat: "yy-mm-dd", maxDate: 0, constrainInput: false, onClose: filterLog});
  $j('#filterServerId')
      .on('change', filterLog);
  $j('#filterComponent')
      .on('change', filterLog);
}

$j(document).ready(function() {
  initPage();
});
