{% extends "base.html" %}

{% block title %}Session-Tracking{% endblock %}

{% block extra_css %}
<style>
    .session-active { background-color: #d4edda; }
    .session-warning { background-color: #fff3cd; }
    .session-inactive { background-color: #f8f9fa; }
</style>
{% endblock %}

{% block content %}
<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Session-Tracking</h2>
        <div>
            <a href="/" class="btn btn-secondary">📊 Dashboard</a>
            <a href="/licenses" class="btn btn-secondary">📋 Lizenzen</a>
            <a href="/customers" class="btn btn-secondary">👥 Kunden</a>
            <a href="/audit" class="btn btn-secondary">📋 Audit</a>
            <a href="/backups" class="btn btn-secondary">💾 Backups</a>
        </div>
    </div>

    <!-- Aktive Sessions -->
    <div class="card mb-4">
        <div class="card-header bg-success text-white">
            <h5 class="mb-0">🟢 Aktive Sessions ({{ active_sessions|length }})</h5>
        </div>
        <div class="card-body">
            {% if active_sessions %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Kunde</th>
                            <th>Lizenz</th>
                            <th>IP-Adresse</th>
                            <th>Gestartet</th>
                            <th>Letzter Heartbeat</th>
                            <th>Inaktiv seit</th>
                            <th>Aktion</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for session in active_sessions %}
                        <tr class="{% if session[8] > 5 %}session-warning{% else %}session-active{% endif %}">
                            <td>{{ session[3] }}</td>
                            <td><small><code>{{ session[2][:12] }}...</code></small></td>
                            <td>{{ session[4] or '-' }}</td>
                            <td>{{ session[6].strftime('%d.%m %H:%M') }}</td>
                            <td>{{ session[7].strftime('%d.%m %H:%M') }}</td>
                            <td>
                                {% if session[8] < 1 %}
                                    <span class="badge bg-success">Aktiv</span>
                                {% elif session[8] < 5 %}
                                    <span class="badge bg-warning">{{ session[8]|round|int }} Min.</span>
                                {% else %}
                                    <span class="badge bg-danger">{{ session[8]|round|int }} Min.</span>
                                {% endif %}
                            </td>
                            <td>
                                <form method="post" action="/session/end/{{ session[0] }}" style="display: inline;">
                                    <button type="submit" class="btn btn-sm btn-outline-danger" 
                                            onclick="return confirm('Session wirklich beenden?');">
                                        ⏹️ Beenden
                                    </button>
                                </form>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            <small class="text-muted">
                Sessions gelten als inaktiv nach 5 Minuten ohne Heartbeat
            </small>
            {% else %}
            <p class="text-muted mb-0">Keine aktiven Sessions vorhanden.</p>
            {% endif %}
        </div>
    </div>

    <!-- Beendete Sessions -->
    <div class="card">
        <div class="card-header bg-secondary text-white">
            <h5 class="mb-0">⏸️ Beendete Sessions (letzte 24 Stunden)</h5>
        </div>
        <div class="card-body">
            {% if recent_sessions %}
            <div class="table-responsive">
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Kunde</th>
                            <th>Lizenz</th>
                            <th>IP-Adresse</th>
                            <th>Gestartet</th>
                            <th>Beendet</th>
                            <th>Dauer</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for session in recent_sessions %}
                        <tr class="session-inactive">
                            <td>{{ session[3] }}</td>
                            <td><small><code>{{ session[2][:12] }}...</code></small></td>
                            <td>{{ session[4] or '-' }}</td>
                            <td>{{ session[5].strftime('%d.%m %H:%M') }}</td>
                            <td>{{ session[6].strftime('%d.%m %H:%M') }}</td>
                            <td>
                                {% if session[7] < 60 %}
                                    {{ session[7]|round|int }} Min.
                                {% else %}
                                    {{ (session[7]/60)|round(1) }} Std.
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <p class="text-muted mb-0">Keine beendeten Sessions in den letzten 24 Stunden.</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}