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

{% block page_title %}{{ switch.hostname }}{% endblock %}
{% block page_sub %}{{ switch.ip }}:{{ switch.ssh_port }}{% if switch.model %} &middot; {{ switch.model }}{% endif %}{% endblock %}

{% block breadcrumb %}
<nav class="breadcrumb">
  <span><a href="{{ url_for('switches.overview') }}">Switche</a></span><span>{{ switch.hostname }}</span>
</nav>
{% endblock %}

{% block topbar_extra %}
  {% if terminal_enabled and current_user.has('switches.execute') and host_keys %}
    <a class="btn btn--sm btn--primary" href="{{ url_for('terminal.switch_console', switch_id=switch.id) }}">
      {{ icon('terminal', size=15) }} Konsole
    </a>
  {% endif %}
{% endblock %}

{% block content %}
<div class="grid grid--split">
  <div class="stack">
    <section class="card {{ '' if host_keys else 'card--danger' }}">
      <div class="card__header">
        <div class="flex-1">
          <div class="card__title">
            {{ icon('lock', size=17) }} SSH-Host-Schluessel
            {% if host_keys %}{{ pill('freigegeben', 'online') }}{% else %}{{ pill('offen', 'danger') }}{% endif %}
          </div>
          <div class="card__sub">
            Ohne freigegebenen Schluessel verweigert die Anwendung jeden Zugriff auf diesen Switch.
          </div>
        </div>
        {% if current_user.has('switches.edit') %}
          <form method="post" action="{{ url_for('switches.probe_host_key', switch_id=switch.id) }}"
                data-no-guard>
            <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
            <button class="btn btn--sm" type="submit">{{ icon('refresh', size=14) }} Auslesen</button>
          </form>
        {% endif %}
      </div>
      <div class="card__body stack">
        {% if request.args.get('probed_fingerprint') %}
          <div class="alert alert--warning">
            {{ icon('alert', size=18) }}
            <div class="alert__body">
              <div class="alert__title">Gelesener Schluessel</div>
              <div class="mono fs-sm break-all">
                {{ request.args.get('probed_type') }} {{ request.args.get('probed_fingerprint') }}
              </div>
              <p class="fs-sm mt-2">
                Vergleichen Sie den Fingerprint mit einer unabhaengigen Quelle (Konsolenzugang am
                Switch, Inventardokumentation), bevor Sie ihn freigeben.
              </p>
              {% if current_user.has('switches.edit') %}
                <form method="post" class="mt-2"
                      action="{{ url_for('switches.trust_host_key', switch_id=switch.id) }}">
                  <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
                  <input type="hidden" name="key_type" value="{{ request.args.get('probed_type') }}">
                  <input type="hidden" name="fingerprint" value="{{ request.args.get('probed_fingerprint') }}">
                  <button class="btn btn--sm btn--primary" type="submit"
                          data-confirm="Diesen Fingerprint dauerhaft als vertrauenswuerdig hinterlegen?"
                          data-confirm-title="Host-Schluessel freigeben" data-confirm-ok="Freigeben"
                          data-confirm-style="primary">Freigeben</button>
                </form>
              {% endif %}
            </div>
          </div>
        {% endif %}

        {% if host_keys %}
          <dl class="kv fs-sm">
            {% for key in host_keys %}
              <dt>{{ key.key_type }}</dt>
              <dd class="mono break-all">{{ key.fingerprint }}
                <div class="fs-xs faint">freigegeben {{ key.added_at | dt }} von {{ key.added_by }}</div>
              </dd>
            {% endfor %}
          </dl>
          {% if current_user.has('switches.edit') %}
            <form method="post" action="{{ url_for('switches.forget_host_key', switch_id=switch.id) }}"
                  data-no-guard>
              <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
              <button class="btn btn--sm btn--ghost danger-text" type="submit"
                      data-confirm="Gespeicherte Host-Schluessel entfernen? Danach ist kein Zugriff mehr moeglich, bis erneut freigegeben wird."
                      data-confirm-title="Schluessel verwerfen" data-confirm-ok="Verwerfen">
                Schluessel verwerfen
              </button>
            </form>
          {% endif %}
        {% else %}
          <p class="muted fs-sm">Noch kein Schluessel hinterlegt. Bitte auslesen und pruefen.</p>
        {% endif %}
      </div>
    </section>

    {% if current_user.has('switches.execute') %}
      <section class="card">
        <div class="card__header">
          <div class="flex-1">
            <div class="card__title">{{ icon('terminal', size=17) }} Kommando ausfuehren</div>
            <div class="card__sub">
              Positivliste lesender Kommandos &ndash; verlangt eine frische Passwortbestaetigung
            </div>
          </div>
        </div>
        <form method="post" action="{{ url_for('switches.run_command', switch_id=switch.id) }}">
          <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
          <div class="card__body">
            <label class="field">
              <span class="field__label">Kommando</span>
              <select class="select" name="command">
                {% for command, label in safe_commands %}
                  <option value="{{ command }}">{{ label }} &ndash; {{ command }}</option>
                {% endfor %}
              </select>
            </label>
          </div>
          <div class="card__footer">
            <button class="btn btn--primary" type="submit" data-busy-label="Fuehre aus ...">
              Ausfuehren
            </button>
          </div>
        </form>
      </section>
    {% endif %}
  </div>

  <div class="stack">
    <section class="card">
      <div class="card__header">
        <div class="flex-1"><div class="card__title">{{ icon('info', size=17) }} Stammdaten</div></div>
      </div>
      <div class="card__body">
        <dl class="kv fs-sm">
          <dt>Hostname</dt><dd>{{ switch.hostname }}</dd>
          <dt>IP</dt><dd class="mono">{{ switch.ip }}</dd>
          <dt>SSH-Port</dt><dd>{{ switch.ssh_port }}</dd>
          <dt>Zugangsdaten</dt><dd>{{ switch.credential_name or 'keine' }}</dd>
          <dt>Standort</dt><dd>{{ switch.location or '&ndash;' | safe }}</dd>
          <dt>Zuletzt gesehen</dt><dd>{{ switch.last_seen_at | dt }}</dd>
          <dt>Geaendert</dt><dd>{{ switch.updated_at | dt }} <span class="faint">von {{ switch.updated_by or '-' }}</span></dd>
        </dl>
        {% if switch.notes %}<p class="fs-sm muted mt-3">{{ switch.notes }}</p>{% endif %}
      </div>
    </section>

    <section class="card">
      <div class="card__header">
        <div class="flex-1">
          <div class="card__title">{{ icon('cpu', size=17) }} Angeschlossene Geraete</div>
          <div class="card__sub">{{ devices | length }} zugeordnet</div>
        </div>
      </div>
      {% if devices %}
        <div class="table-wrap">
          <table class="table table--compact">
            <thead><tr><th>Port</th><th>Geraet</th><th>Zustand</th></tr></thead>
            <tbody>
              {% for device in devices %}
                <tr>
                  <td class="mono fs-sm">{{ device.port or '&ndash;' | safe }}</td>
                  <td class="fs-sm">
                    <a href="{{ url_for('devices.detail', device_id=device.id) }}">{{ device.name }}</a>
                    <div class="fs-xs faint mono">{{ device.ip }}</div>
                  </td>
                  <td>
                    {% if device.state == 'online' %}{{ pill('online', 'online') }}
                    {% elif device.state == 'offline' %}{{ pill('offline', 'offline') }}
                    {% else %}{{ pill('unbekannt', 'warning') }}{% endif %}
                  </td>
                </tr>
              {% endfor %}
            </tbody>
          </table>
        </div>
      {% else %}
        {{ empty_state('Keine Geraete', 'Diesem Switch ist noch kein Geraet zugeordnet.', 'cpu') }}
      {% endif %}
    </section>
  </div>
</div>
{% endblock %}
