<%
  const check = await requirePerm(req, "manage");
  let limit = +req.query.limit;
  if (!isFinite(limit) || limit <= 0) limit = 100;
  if (check !== true) { %><%- check %><% }
  else {
    let query = "select * from auditlog left join users on users.userid=auditlog.userid";
    if (req.query.clantag) query += " where auditlog.clantag=$[clantag]";
    if (req.query.userid) query += " where auditlog.userid=$[userid]";
    query += " order by ts desc limit $[limit];"
    resp = await db.any(query, {...req.query, limit});
%>
  <%- await include("partials/header.ejs", {req, res, title: "Audit Log"}) %>
  <h3>Audit Log</h3>
  <p>To filter by userid or clantag, go to Manage Users or View Clan respectively.</p>
  <p>Row limit: <%= limit %> rows <button onclick="addSearch('limit', prompt('New row limit?', '<%= limit %>'))">Change</button>
  <table>
    <thead>
      <tr>
        <td><b>Timestamp</b></td>
        <td><b>User ID</b></td>
        <td><b>User Name</b></td>
        <td><b>Clan Tag</b></td>
        <td><b>Action</b></td>
        <td><b>Data</b></td>
      </tr>
    </thead>
    <tbody>
      <% for await (let {ts, userid, name, clantag, action, cdata} of resp) { %>
        <tr>
          <td><%= ts.toISOString() %></td>
          <td><%= userid %></td>
          <td><%= name %></td>
          <td><%= clantag %></td>
          <td><%= action %></td>
          <td><tt><%= JSON.stringify(cdata) %></tt></td>
        </tr>
      <% } %>
    </tbody>
  </table>
  <%- await include("partials/footer.ejs", {req, res}) %>
<%
}
%>
