{% extends "tesm_core/base.html" %}
{% from "tesm_core/macros.html" import pill, empty_state, alert %}

{% block page_title %}Aenderungsprotokoll{% endblock %}
{% block page_sub %}{{ total }} Eintraege &middot; Hash-Kette {{ 'unversehrt' if chain.ok else 'gebrochen' }}{% endblock %}

{% block topbar_extra %}
  {% if current_user.has('logs_audit.export') %}
    <a class="btn btn--sm" href="{{ url_for('logs.audit_export') }}">{{ icon('download', size=15) }} CSV</a>
  {% endif %}
{% endblock %}

{% block content %}
{% if not chain.ok %}
  {{ alert(chain.reason ~ ' Erster Bruch bei Eintrag ' ~ chain.broken_at ~ '.', 'danger',
           'Das Protokoll wurde veraendert') }}
{% else %}
  {{ alert('Jeder Eintrag bindet den Hash seines Vorgaengers ein. Eine nachtraeglich entfernte
  oder geaenderte Zeile faellt bei der Pruefung auf -- ' ~ chain.checked ~ ' Zeilen geprueft.',
  'success') }}
{% endif %}

<section class="card">
  <form method="get" class="table-toolbar" data-no-guard>
    <span class="search-input">
      {{ icon('search', size=15) }}
      <input class="input" type="search" name="q" value="{{ filters.q }}" placeholder="Ziel, Detail, Aktion ...">
    </span>
    <select class="select" name="aktion" style="width:auto;max-width:240px">
      <option value="">alle Aktionen</option>
      {% for action in actions %}
        <option value="{{ action }}" {% if action == filters.aktion %}selected{% endif %}>{{ action }}</option>
      {% endfor %}
    </select>
    <select class="select" name="stufe" style="width:auto">
      <option value="">alle Stufen</option>
      {% for level in ['info', 'notice', 'warning', 'critical'] %}
        <option value="{{ level }}" {% if level == filters.stufe %}selected{% endif %}>{{ level }}</option>
      {% endfor %}
    </select>
    <input class="input" type="text" name="benutzer" value="{{ filters.benutzer }}"
           placeholder="Benutzer" style="width:150px">
    <button class="btn btn--sm" type="submit">Filtern</button>
    <a class="btn btn--sm btn--ghost" href="{{ url_for('logs.audit_view') }}">Zuruecksetzen</a>
  </form>

  <div class="table-wrap">
    <table class="table table--compact" data-behavior="sortable">
      <thead>
        <tr>
          <th class="sortable">Zeit</th>
          <th class="sortable">Benutzer</th>
          <th class="sortable">Aktion</th>
          <th>Ziel</th>
          <th>Detail</th>
          <th class="sortable">Stufe</th>
          <th>Herkunft</th>
        </tr>
      </thead>
      <tbody>
        {% for row in rows %}
          <tr>
            <td class="fs-sm nowrap" data-sort-value="{{ row.ts }}">{{ row.ts | dt }}</td>
            <td class="fs-sm fw-600">{{ row.actor }}</td>
            <td class="mono fs-xs">{{ row.action }}</td>
            <td class="fs-sm">{{ row.target }}</td>
            <td class="fs-sm muted break-all">{{ row.detail }}</td>
            <td>
              {% if row.severity == 'critical' %}{{ pill('kritisch', 'danger') }}
              {% elif row.severity == 'warning' %}{{ pill('Warnung', 'warning') }}
              {% elif row.severity == 'notice' %}{{ pill('Hinweis', 'info') }}
              {% else %}{{ pill('Info', '') }}{% endif %}
            </td>
            <td class="mono fs-xs faint">{{ row.ip or '&ndash;' | safe }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% if not rows %}{{ empty_state('Keine Eintraege', 'Zu diesem Filter gibt es nichts.', 'history') }}{% endif %}

  <div class="card__footer" style="justify-content:space-between">
    <span class="fs-sm muted">Seite {{ page }}</span>
    <span class="row row--tight">
      {% if page > 1 %}
        <a class="btn btn--sm" href="{{ url_for('logs.audit_view', seite=page-1, **filters) }}">Zurueck</a>
      {% endif %}
      {% if rows | length == per_page %}
        <a class="btn btn--sm" href="{{ url_for('logs.audit_view', seite=page+1, **filters) }}">Weiter</a>
      {% endif %}
    </span>
  </div>
</section>
{% endblock %}
