{% extends "base.html" %}
{% set active_page = "logs" %}
{% block page_title %}Verlauf{% endblock %}
{% block page_sub %}<div class="topbar-sub">{{ selected_file.range_label if selected_file else "kein Log" }}</div>{% endblock %}

{% block content %}
{% import "_hint_icon.html" as hi %}

<div class="section-head">
  <div>
    <h2 style="font-size:16px;">Log-Verlauf</h2>
    <div class="hint">Aktuelles Live-Log sowie ältere, von logrotate rotierte Kopien davon — {{ files|length }} Stand{{ 'e' if files|length != 1 else '' }} verfügbar, auswählbar nach Zeitraum. Aus Performance-Gründen unformatiert (RAW) dargestellt, ohne farbliche Aufbereitung.</div>
    <div class="hint">Zusätzlich weiter unten: bereits archivierte Auditlog-Tage (das Auditlog wird ab {{ "{:,}".format(audit_threshold).replace(",", ".") }} Einträgen automatisch tageweise archiviert, bis {{ "{:,}".format(audit_target).replace(",", ".") }} Einträge unterschritten sind).</div>
  </div>
  <div class="flex gap-2">
    <a href="{{ url_for('logs') }}" class="btn btn-secondary">
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
      Zurück zur Live-Ansicht
    </a>
    <button type="button" class="btn btn-secondary"
            onclick="openRawLogModal('{{ url_for('logs_history_raw', file=selected_name) if selected_name else '' }}', 'Komplettes Log (RAW) — {{ selected_file.range_label if selected_file else '' }}', true)"
            {% if not selected_name %}disabled{% endif %}>
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><path d="M14 2v6h6"/></svg>
      Komplettes Log (RAW)
    </button>
  </div>
</div>

{% if not files %}
<p class="text-faint" style="font-size:12.5px;">
  Kein Live-Log gefunden.
</p>
{% else %}

<div class="flex gap-2" style="align-items:center; margin-bottom:14px;">
  <label for="history-file-select" class="text-faint" style="font-size:12.5px; font-weight:600;">Zeitraum:</label>
  <select id="history-file-select" onchange="window.location.href=this.value;" class="mono" style="max-width:460px;">
    {% for f in files %}
    <option value="{{ url_for('logs_history', file=f.filename) }}" {% if f.filename == selected_name %}selected{% endif %}>
      {% if f.generation == 0 %}Aktuell — {{ f.range_label }}{% else %}{{ f.range_label }}{% endif %} ({{ f.size_str }})
    </option>
    {% endfor %}
  </select>
</div>

<div class="log-shell">
  <div class="log-toolbar">
    <div class="log-dots"><span></span><span></span><span></span></div>
    <span class="text-faint mono" style="font-size:11.5px;">{{ selected_file.range_label if selected_file else "" }}</span>
    <span class="text-faint mono" style="font-size:11.5px;">{% if total_lines %}letzte {{ tail_lines }} von {{ total_lines }} Zeilen — RAW{% endif %}</span>
  </div>
  <pre id="log-box" class="raw-log-content" style="height:100%;">{{ log_content or "" }}</pre>
</div>
{% endif %}

<div class="section-head" style="margin-top:28px;">
  <div>
    <h2 style="font-size:16px;">Archivierte Auditlog-Tage {{ hi.hint_icon("Ältere Auditlog-Einträge, tageweise als eigene Datei ausgelagert, sobald die laufende Tabelle den Schwellenwert überschreitet — die Dateien selbst bleiben unbegrenzt erhalten, bis sie hier bewusst exportiert werden.", "Archivierte Auditlog-Tage") }}</h2>
  </div>
  {% if current_user.can_manage_log_history and audit_files %}
  <form method="post" action="{{ url_for('logs_history_audit_export') }}"
        data-confirm="Alle {{ audit_files|length }} archivierten Auditlog-Datei(en) als ZIP herunterladen und danach vom Server löschen?"
        data-confirm-title="Export &amp; Löschen">
    <button type="submit" class="btn btn-secondary">
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4"/><path d="M7 10l5 5 5-5"/><path d="M12 15V3"/></svg>
      Alle exportieren &amp; löschen (ZIP)
    </button>
  </form>
  {% endif %}
</div>

{% if disk_warning %}
<div class="notice-banner notice-banner--warning" style="margin-bottom:14px;">{{ disk_warning }}</div>
{% endif %}

{% if not audit_files %}
<p class="text-faint" style="font-size:12.5px;">Noch keine archivierten Auditlog-Tage vorhanden.</p>
{% else %}
<div class="table-wrap">
  <table class="data-table">
    <thead><tr>
      <th style="width:1%; white-space:nowrap;">Tag</th>
      <th style="width:1%; white-space:nowrap;">Größe</th>
      <th></th>
    </tr></thead>
    <tbody>
      {% for f in audit_files %}
      <tr>
        <td class="mono">{{ f.day }}</td>
        <td class="text-dim mono" style="font-size:12.5px;">{{ (f.size / 1024)|round(1) }} KB</td>
        <td>
          <button type="button" class="btn btn-secondary btn-sm"
                  onclick="openRawLogModal('{{ url_for('logs_history_audit_raw', file=f.filename) }}', 'Auditlog-Archiv — {{ f.day }}', true)">
            Ansehen (RAW)
          </button>
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>
{% endif %}
{% endblock %}

{% block scripts %}
<script>
document.addEventListener("DOMContentLoaded", () => {
    const box = document.getElementById("log-box");
    if (box) box.scrollTop = box.scrollHeight;
});
</script>
{% endblock %}
