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

{% block page_title %}Systemeinstellungen{% endblock %}
{% block page_sub %}Ueberwachung, Protokolle, Host, Navigation{% endblock %}

{% block content %}
{% if not sysops_available %}
  {{ alert('Systemhelfer nicht verfuegbar -- Hostname und Zeitzone koennen hier nur angezeigt,
  nicht geaendert werden. In einer Entwicklungsumgebung ist das der erwartete Zustand.',
  'info') }}
{% endif %}

<div class="grid grid--split">
  <div class="stack">
    <section class="card">
      <div class="card__header">
        <div class="flex-1">
          <div class="card__title">{{ icon('activity', size=17) }} Ueberwachung</div>
          <div class="card__sub">Wie oft geprueft und wann automatisch geschaltet wird</div>
        </div>
        {% if current_user.has('settings_system.execute') %}
          <form method="post" action="{{ url_for('settings.run_check_now') }}" data-no-guard>
            <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
            <button class="btn btn--sm" type="submit">{{ icon('refresh', size=14) }} Jetzt pruefen</button>
          </form>
        {% endif %}
      </div>
      <form method="post" action="{{ url_for('settings.system_save') }}">
        <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
        <div class="card__body stack">
          {% if license_hint %}{{ alert(license_hint, 'warning') }}{% endif %}
          <div class="form-grid">
            {{ field('monitor_interval_seconds', 'Pruefintervall (Sekunden)',
                     value=values.monitor_interval_seconds, type='number',
                     disabled=bool(license_hint),
                     hint_text=specs['monitor_interval_seconds'].hint) }}
            {{ field('monitor_failures_before_restart', 'Fehlversuche bis zum PoE-Neustart',
                     value=values.monitor_failures_before_restart, type='number',
                     hint_text=specs['monitor_failures_before_restart'].hint) }}
          </div>
          {{ checkbox('monitor_auto_restart', 'Automatischen PoE-Neustart bei Ausfall aktivieren',
                      checked=values.monitor_auto_restart,
                      hint_text='Zusaetzlich lizenzpflichtig. Der manuelle Knopf im Dashboard
                      funktioniert unabhaengig davon.') }}
          {{ checkbox('dashboard_public', 'Statusuebersicht ohne Anmeldung erreichbar',
                      checked=values.dashboard_public,
                      hint_text=specs['dashboard_public'].hint) }}
        </div>
        <div class="card__footer"><button class="btn btn--primary" type="submit">Speichern</button></div>
      </form>
    </section>

    <section class="card">
      <div class="card__header">
        <div class="flex-1">
          <div class="card__title">{{ icon('terminal', size=17) }} Switch-Konsole</div>
          <div class="card__sub">
            Auch bei aktiver Konsole gilt: eigenes Recht, frische Passwortbestaetigung,
            vollstaendige Mitschrift
          </div>
        </div>
      </div>
      <form method="post" action="{{ url_for('settings.system_save') }}">
        <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
        <div class="card__body stack">
          {{ checkbox('terminal_enabled', 'Browser-Konsole fuer Switche zulassen',
                      checked=values.terminal_enabled) }}
          {{ field('terminal_idle_timeout', 'Trennen bei Untaetigkeit (Sekunden)',
                   value=values.terminal_idle_timeout, type='number') }}
        </div>
        <div class="card__footer"><button class="btn btn--primary" type="submit">Speichern</button></div>
      </form>
    </section>

    <section class="card">
      <div class="card__header">
        <div class="flex-1">
          <div class="card__title">{{ icon('file-text', size=17) }} Protokolle und Papierkorb</div>
        </div>
      </div>
      <form method="post" action="{{ url_for('settings.system_save') }}">
        <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
        <div class="card__body">
          <div class="form-grid">
            {{ select_field('log_rotate_interval', 'Rotationsintervall',
                            specs['log_rotate_interval'].choices, selected=values.log_rotate_interval) }}
            {{ field('log_rotate_keep', 'Aufbewahrte Rotationen', value=values.log_rotate_keep,
                     type='number') }}
            {{ field('trash_retention_days', 'Papierkorb-Aufbewahrung (Tage)',
                     value=values.trash_retention_days, type='number',
                     hint_text=specs['trash_retention_days'].hint) }}
          </div>
        </div>
        <div class="card__footer"><button class="btn btn--primary" type="submit">Speichern</button></div>
      </form>
    </section>
  </div>

  <div class="stack">
    <section class="card">
      <div class="card__header">
        <div class="flex-1"><div class="card__title">{{ icon('server', size=17) }} Host</div></div>
      </div>
      <div class="card__body stack">
        <form method="post" action="{{ url_for('settings.set_hostname') }}" class="stack stack--tight">
          <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
          {{ field('hostname', 'Hostname', value=hostname, disabled=not sysops_available) }}
          <button class="btn btn--sm" type="submit" {% if not sysops_available %}disabled{% endif %}>
            Hostname setzen
          </button>
        </form>
        <div class="divider"></div>
        <form method="post" action="{{ url_for('settings.set_timezone') }}" class="stack stack--tight">
          <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
          <label class="field">
            <span class="field__label">Zeitzone</span>
            <select class="select" name="timezone" {% if not sysops_available %}disabled{% endif %}>
              {% for zone in timezones %}
                <option value="{{ zone }}" {% if zone == timezone %}selected{% endif %}>{{ zone }}</option>
              {% endfor %}
            </select>
            <span class="field__hint">Wirkt sofort auf die laufende Anwendung.</span>
          </label>
          <button class="btn btn--sm" type="submit" {% if not sysops_available %}disabled{% endif %}>
            Zeitzone setzen
          </button>
        </form>
      </div>
    </section>

    {% if current_user.is_admin %}
      <section class="card">
        <div class="card__header">
          <div class="flex-1">
            <div class="card__title">{{ icon('sliders', size=17) }} Navigationsreihenfolge</div>
            <div class="card__sub">Gilt fuer alle Benutzer; jeder sieht weiter nur, was er darf</div>
          </div>
        </div>
        <form method="post" action="{{ url_for('admin.nav_order') }}">
          <input type="hidden" name="{{ csrf_field_name }}" value="{{ csrf_token() }}">
          <div class="card__body">
            <ol class="stack stack--tight">
              {% for key in nav_order %}
                {% set item = nav_items | selectattr('key', 'equalto', key) | list | first %}
                {% if item %}
                  <li class="row row--tight">
                    <input class="input" type="number" name="position_{{ item.key }}" value="{{ loop.index }}"
                           min="1" style="width:70px" disabled>
                    <input type="hidden" name="order" value="{{ item.key }}">
                    <span class="flex-1">{{ icon(item.icon, size=15) }} {{ item.label }}</span>
                  </li>
                {% endif %}
              {% endfor %}
            </ol>
            <p class="fs-xs faint mt-3">
              Die Reihenfolge folgt der Liste. Zum Umsortieren die Eintraege in der gewuenschten
              Reihenfolge speichern (Drag-and-drop folgt in einer spaeteren Version).
            </p>
          </div>
          <div class="card__footer">
            <button class="btn btn--sm" type="submit">Reihenfolge speichern</button>
          </div>
        </form>
      </section>
    {% endif %}
  </div>
</div>
{% endblock %}
