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

{% macro switch_form(switch=None) %}
  <div class="form-grid">
    {{ field('hostname', 'Hostname', value=switch.hostname if switch else '', required=True) }}
    {{ field('ip', 'IP-Adresse', value=switch.ip if switch else '', required=True) }}
    {{ field('ssh_port', 'SSH-Port', value=switch.ssh_port if switch else 22, type='number') }}
    <label class="field">
      <span class="field__label">Zugangsdaten</span>
      <select class="select" name="credential_id">
        <option value="">keine</option>
        {% for credential in credentials %}
          <option value="{{ credential.id }}"
            {% if switch and switch.credential_id == credential.id %}selected{% endif %}>
            {{ credential.name }} ({{ credential.username }})
          </option>
        {% endfor %}
      </select>
    </label>
    {{ field('model', 'Modell', value=switch.model if switch else '', placeholder='z. B. Aruba 2930M') }}
    {{ field('location', 'Standort', value=switch.location if switch else '') }}
    {{ textarea_field('notes', 'Notizen', value=switch.notes if switch else '', rows=2,
                      extra_class='field--full') }}
  </div>
{% endmacro %}

{% block page_title %}Switche{% endblock %}
{% block page_sub %}{{ switches | length }} Switche{% endblock %}

{% block topbar_extra %}
  {{ search_box('switches-table', 'Hostname, IP, Standort ...') }}
  {% if current_user.has('switches.create') %}
    <button class="btn btn--primary btn--sm" type="button" data-dialog-open="switch-new">
      {{ icon('plus', size=15) }} Switch anlegen
    </button>
  {% endif %}
{% endblock %}

{% block content %}
{{ alert('Vor dem ersten Zugriff muss der SSH-Host-Schluessel eines Switches ausdruecklich
freigegeben werden. Unbekannte oder geaenderte Schluessel werden abgewiesen -- nicht
stillschweigend uebernommen.', 'info', 'Host-Schluessel') }}

<section class="card">
  <div class="table-wrap">
    <table class="table" id="switches-table" data-behavior="sortable">
      <thead>
        <tr>
          <th class="sortable">Hostname</th>
          <th class="sortable">IP</th>
          <th class="sortable numeric">Port</th>
          <th class="sortable">Zugangsdaten</th>
          <th class="sortable numeric">Geraete</th>
          <th class="sortable">Host-Schluessel</th>
          <th class="actions">Aktion</th>
        </tr>
      </thead>
      <tbody>
        {% for switch in switches %}
          <tr data-filter-text="{{ switch.hostname }} {{ switch.ip }} {{ switch.location }} {{ switch.model }}">
            <td>
              <a class="fw-600" href="{{ url_for('switches.detail', switch_id=switch.id) }}">
                {{ switch.hostname }}
              </a>
              {% if switch.model %}<div class="fs-xs faint">{{ switch.model }}</div>{% endif %}
            </td>
            <td class="mono fs-sm">{{ switch.ip }}</td>
            <td class="numeric fs-sm">{{ switch.ssh_port }}</td>
            <td class="fs-sm">
              {% if switch.credential_name %}{{ switch.credential_name }}
              {% else %}<span class="faint">keine</span>{% endif %}
            </td>
            <td class="numeric">{{ switch.device_count }}</td>
            <td data-sort-value="{{ '1' if switch.host_key_known else '0' }}">
              {% if switch.host_key_known %}{{ pill('freigegeben', 'online') }}
              {% else %}
                <a href="{{ url_for('hostkeys.overview') }}"
                   title="Ohne Freigabe verweigern Konsole und PoE-Schaltung die Verbindung">
                  {{ pill('offen', 'warning') }}
                </a>
              {% endif %}
            </td>
            <td class="actions">
              {% if terminal_enabled and current_user.has('switches.execute') and switch.host_key_known %}
                <a class="btn btn--sm btn--ghost" title="Konsole"
                   href="{{ url_for('terminal.switch_console', switch_id=switch.id) }}">
                  {{ icon('terminal', size=15) }}
                </a>
              {% endif %}
              {% if current_user.has('switches.edit') %}
                <button class="btn btn--sm btn--ghost" type="button"
                        data-dialog-open="switch-edit-{{ switch.id }}" aria-label="Bearbeiten">
                  {{ icon('pencil', size=15) }}
                </button>
              {% endif %}
              {% if current_user.has('switches.delete') %}
                <form method="post" action="{{ url_for('switches.delete', switch_id=switch.id) }}"
                      style="display:inline" data-no-guard>
                  <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
                  <button class="btn btn--sm btn--ghost danger-text" type="submit" aria-label="Loeschen"
                          data-confirm="{{ switch.hostname }} in den Papierkorb verschieben?"
                          data-confirm-title="Switch loeschen" data-confirm-ok="Loeschen">
                    {{ icon('trash', size=15) }}</button>
                </form>
              {% endif %}
            </td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% if not switches %}
    {{ empty_state('Keine Switche', 'Legen Sie den ersten Switch an.', 'switch') }}
  {% endif %}
</section>

{% if host_keys %}
  <section class="card">
    <div class="card__header">
      <div class="flex-1">
        <div class="card__title">{{ icon('lock', size=17) }} Freigegebene Host-Schluessel</div>
        <div class="card__sub">{{ host_keys | length }} Eintraege</div>
      </div>
    </div>
    <div class="table-wrap">
      <table class="table table--compact">
        <thead><tr><th>Host</th><th>Port</th><th>Typ</th><th>Fingerprint</th><th>Freigegeben</th></tr></thead>
        <tbody>
          {% for key in host_keys %}
            <tr>
              <td class="mono fs-sm">{{ key.host }}</td>
              <td class="fs-sm">{{ key.port }}</td>
              <td class="fs-sm">{{ key.key_type }}</td>
              <td class="mono fs-xs break-all">{{ key.fingerprint }}</td>
              <td class="fs-sm">{{ key.added_at | dt }} <span class="faint">von {{ key.added_by }}</span></td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </section>
{% endif %}
{% endblock %}

{% block dialogs %}
{% if current_user.has('switches.create') %}
  {% call modal('switch-new', 'Switch anlegen', 'Host-Schluessel wird danach freigegeben') %}
    <form method="post" action="{{ url_for('switches.create') }}">
      <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
      <div class="modal__body">{{ switch_form() }}</div>
      <div class="modal__footer">
        <button class="btn" type="button" data-dialog-close>Abbrechen</button>
        <button class="btn btn--primary" type="submit">Anlegen</button>
      </div>
    </form>
  {% endcall %}
{% endif %}
{% if current_user.has('switches.edit') %}
  {% for switch in switches %}
    {% call modal('switch-edit-' ~ switch.id, switch.hostname, switch.ip) %}
      <form method="post" action="{{ url_for('switches.update', switch_id=switch.id) }}">
        <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
        <div class="modal__body">{{ switch_form(switch) }}</div>
        <div class="modal__footer">
          <button class="btn" type="button" data-dialog-close>Abbrechen</button>
          <button class="btn btn--primary" type="submit">Speichern</button>
        </div>
      </form>
    {% endcall %}
  {% endfor %}
{% endif %}
{% endblock %}
