<%- include('../partials/header') %>
<%- include('../partials/adm-nav') %>

<section class="panel">
  <div class="panel-head">
    <div>
      <h2>Journal d'activité</h2>
      <p class="muted" style="margin:0.35rem 0 0"><%= formatInteger(total) %> événement<%= total > 1 ? 's' : '' %></p>
    </div>
  </div>

  <form method="GET" action="/adm/journal" class="form form-grid" style="margin-bottom:1rem;max-width:none">
    <label>
      <span>Type</span>
      <select name="entity">
        <option value="">Tous</option>
        <% Object.keys(entityLabels).forEach(function(key) { %>
          <option value="<%= key %>" <%= filters.entityType === key ? 'selected' : '' %>><%= entityLabels[key] %></option>
        <% }) %>
      </select>
    </label>
    <label>
      <span>Action</span>
      <select name="action">
        <option value="">Toutes</option>
        <% Object.keys(actionLabels).forEach(function(key) { %>
          <option value="<%= key %>" <%= filters.action === key ? 'selected' : '' %>><%= actionLabels[key] %></option>
        <% }) %>
      </select>
    </label>
    <div class="form-actions" style="align-items:flex-end">
      <button type="submit" class="btn btn-primary">Filtrer</button>
      <a href="/adm/journal" class="btn btn-ghost">Réinitialiser</a>
    </div>
  </form>

  <% if (logs.length === 0) { %>
    <p class="empty">Aucune entrée dans le journal pour le moment.</p>
  <% } else { %>
    <div class="table-wrap">
      <table>
        <thead>
          <tr>
            <th>Date</th>
            <th>Action</th>
            <th>Entité</th>
            <th>Résumé</th>
            <th>IP</th>
          </tr>
        </thead>
        <tbody>
          <% logs.forEach(function(log) { %>
            <tr>
              <td class="muted" style="white-space:nowrap">
                <%= new Date(log.created_at).toLocaleString('fr-FR') %>
              </td>
              <td><span class="tag"><%= actionLabels[log.action] || log.action %></span></td>
              <td>
                <%= entityLabels[log.entity_type] || log.entity_type %>
                <% if (log.entity_id != null) { %>
                  <span class="muted">#<%= log.entity_id %></span>
                <% } %>
              </td>
              <td><%= log.summary %></td>
              <td class="muted"><%= log.ip || '—' %></td>
            </tr>
          <% }) %>
        </tbody>
      </table>
    </div>
  <% } %>
</section>

<%- include('../partials/footer') %>
