{% extends "tesm_core/base.html" %}
{% from "tesm_core/macros.html" import pill, empty_state, alert, search_box %}
{% block page_title %}Neustarts{% endblock %}
{% block page_sub %}{{ rows | length }} Neustarts (neueste zuerst){% endblock %}
{% block topbar_extra %}{{ search_box('restart-table', 'Geraet, Ergebnis, Methode ...') }}{% endblock %}
{% block content %}
{{ alert('Jeder Neustart wird mit Methode, Ergebnis, Dauer und Ausloeser festgehalten -- automatisch
wie manuell, ueber den PoE-Port des Switches ebenso wie per Neustart-Befehl aus der Wartung.
Der Vorgaenger protokollierte Neustarts ausdruecklich nicht.', 'info') }}

<section class="card">
  <div class="card__header">
    <div class="flex-1">
      <div class="card__title">Verlauf</div>
      <div class="card__sub">
        {% for value, label in methods %}
          <a class="btn btn--sm {{ 'btn--ghost' if method != value else '' }}"
             href="{{ url_for('tesmlogs.restarts', methode=value) }}">{{ label }}</a>
        {% endfor %}
        <a class="btn btn--sm {{ 'btn--ghost' if method else '' }}"
           href="{{ url_for('tesmlogs.restarts') }}">Alle</a>
      </div>
    </div>
  </div>
  <div class="table-wrap">
    <table class="table table--compact" id="restart-table" data-behavior="sortable">
      <thead><tr>
        <th class="sortable">Zeit</th><th class="sortable">Geraet</th>
        <th class="sortable">Methode</th><th class="sortable">Ausloeser</th>
        <th class="sortable">Benutzer</th><th class="sortable">Ergebnis</th>
        <th class="sortable numeric">Dauer</th><th>Detail</th>
      </tr></thead>
      <tbody>
        {% for row in rows %}
          <tr data-filter-text="{{ row.device_name }} {{ row.result }} {{ row.actor }} {{ method_labels.get(row.method, row.method) }} {{ row.detail }}">
            <td class="fs-sm nowrap" data-sort-value="{{ row.ts }}">{{ row.ts | dt }}</td>
            <td class="fw-600 fs-sm">{{ row.device_name or '&ndash;' | safe }}</td>
            <td class="fs-sm">
              {# Kein if/else mit zwei Zweigen: es gibt drei Wege, und alles
                 ausser 'ssh' als "PoE-Port" anzuzeigen hat RPC-Neustarts
                 jahrelang als PoE ausgegeben. #}
              {% if row.method == 'ssh' %}{{ pill('SSH', 'info') }}
              {% elif row.method == 'rpc' %}{{ pill('RPC', 'accent') }}
              {% elif row.method == 'poe' %}{{ pill('PoE-Port', '') }}
              {% else %}{{ pill(row.method or 'unbekannt', 'warning') }}{% endif %}
            </td>
            <td class="fs-sm">{{ row.trigger }}</td>
            <td class="fs-sm">{{ row.actor }}</td>
            <td>
              {% if row.result == 'ok' %}{{ pill('erfolgreich', 'online') }}
              {% elif row.result == 'skipped' %}{{ pill('uebersprungen', 'warning') }}
              {% else %}{{ pill('fehlgeschlagen', 'danger') }}{% endif %}
            </td>
            <td class="numeric fs-sm">{{ row.duration_ms or 0 }} ms</td>
            <td class="fs-xs muted break-all" style="max-width:420px">{{ row.detail }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% if not rows %}{{ empty_state('Noch keine Neustarts', '', 'power') }}{% endif %}
</section>
{% endblock %}
