/* Pi-hole: A black hole for Internet advertisements
 *  (c) 2017 Pi-hole, LLC (https://pi-hole.net)
 *  Network-wide ad blocking via your own hardware.
 *
 *  This file is copyright under the latest version of the EUPL.
 *  Please see LICENSE file for your rights under this license. */

/* global utils:false */

var table;
var token = $("#token").html();

function render_timestamp(data, type) {
  // Display and search content
  if (type === "display" || type === "filter") {
    return utils.datetime(data);
  }
  // Sorting content
  return data;
}

function render_message(data, type, row) {
  // Display and search content
  switch (row["type"]) {
    case "REGEX":
      return "Encountered an error when processing <a href=\"groups-domains.php?domainid=" + row["blob3"] + "\">" + row["blob1"] + " regex filter with ID " + row["blob3"] + "</a>:<pre>"+row["blob2"]+"</pre>Error message: <pre>"+row["message"]+"</pre>";
  
    default:
      return "Unknown message type<pre>" + JSON.stringify(row) + "</pre>";
  }
  return data;
}

$(document).ready(function() {
  table = $("#messagesTable").DataTable({
    ajax: {
      url: "api_db.php?messages",
      data: { token: token },
      type: "POST",
      dataSrc: "messages" 
    },
    order: [[0, "asc"]],
    columns: [
      { data: "id", visible: false },
      { data: "timestamp", 
        width: "8%",
        render: render_timestamp
      },
      { data: "type", width: "8%" },
      { data: "message", orderable: false, render: render_message },
      { data: "blob1", visible: false },
      { data: "blob2", visible: false },
      { data: "blob3", visible: false },
      { data: "blob4", visible: false },
      { data: "blob5", visible: false },
    ],
    dom:
      "<'row'<'col-sm-4'l><'col-sm-8'f>>" +
      "<'row'<'col-sm-12'<'table-responsive'tr>>>" +
      "<'row'<'col-sm-5'i><'col-sm-7'p>>",
    lengthMenu: [
      [10, 25, 50, 100, -1],
      [10, 25, 50, 100, "All"]
    ],
    language: {
      emptyTable: "No issues found."
    },
    stateSave: true,
    stateSaveCallback: function(settings, data) {
      // Store current state in client's local storage area
      localStorage.setItem("messages-table", JSON.stringify(data));
    },
    stateLoadCallback: function() {
      // Receive previous state from client's local storage area
      var data = localStorage.getItem("messages-table");
      // Return if not available
      if (data === null) {
        return null;
      }
      // Parse loaded data
      data = JSON.parse(data);
      // Always start on the first page to show most recent queries
      data.start = 0;
      // Always start with empty search field
      data.search.search = "";
      // Reset visibility of ID and blob columns
      var hiddenCols = [0,4,5,6,7,8];
      for (var key in hiddenCols) {
        if (hiddenCols.hasOwnProperty(key)) {
          data.columns[hiddenCols[key]].visible = false;
        }
      }
      
      // Apply loaded state to table
      return data;
    }
  });
});

@@ -549,16 +549,84 @@
 ata;%0A%7D%0A%0A
+function multline(input) %7B%0A  return input.split(%22,%22).join(%22%5Cn%22);%0A%7D%0A%0A
 function
@@ -656,24 +656,24 @@
 ype, row) %7B%0A
-
   // Display
@@ -988,18 +988,318 @@
 /pre%3E%22;%0A
+%0A
   
+  case %22SUBNET%22:%0A      return %22Client %3Ccode%3E%22+row%5B%22message%22%5D+%22%3C/code%3E is managed by %22+row%5B%22blob1%22%5D+%22 groups (database IDs %5B%22+row%5B%22blob3%22%5D+%22%5D):%3Cpre%3E%22+multline(row%5B%22blob2%22%5D)+%22%3C/pre%3E%22 +%0A      %22FTL chose the most recent entry %3Cpre%3E%22+row%5B%22blob4%22%5D+%22%3C/pre%3E to get the group configuration for this client.%22;
 %0A    def

Add support for SUBNET messages.