{% extends 'base.html' %}
{% load humanize %}

{% block title %}Administration - van Hees-Theyssen-Vogel'sche Stiftung{% endblock %}

{% block content %}

    <div class="row">
        <div class="col-12">
            <div class="d-sm-flex align-items-center justify-content-between mb-4">
                <h1 class="h3 mb-0 text-gray-800">
                    <i class="fas fa-cogs me-2"></i>Administration
                </h1>
            </div>
        </div>
    </div>

    <!-- Statistiken -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Audit Logs Gesamt</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.total_logs|default:0|intcomma }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-history fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Heute</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.logs_today|default:0 }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-calendar-day fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">Diese Woche</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.logs_week|default:0 }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-calendar-week fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Letztes Backup</div>
                            <div class="small text-gray-800">
                                {% if stats.last_backup %}
                                    {{ stats.last_backup.completed_at|date:"d.m.Y H:i" }}
                                {% else %}
                                    Kein Backup vorhanden
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-database fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Aktuelle Audit-Aktivität -->
        <div class="col-lg-8">
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-history me-2"></i>Aktuelle Audit-Aktivität
                    </h6>
                    <a href="{% url 'stiftung:audit_log_list' %}" class="btn btn-primary btn-sm">
                        <i class="fas fa-list me-1"></i>Alle Logs anzeigen
                    </a>
                </div>
                <div class="card-body">
                    {% if recent_audit %}
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>Zeit</th>
                                        <th>Benutzer</th>
                                        <th>Aktion</th>
                                        <th>Beschreibung</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for log in recent_audit %}
                                    <tr>
                                        <td>
                                            <small>{{ log.timestamp|date:"d.m. H:i" }}</small>
                                        </td>
                                        <td>
                                            <span class="badge bg-secondary">{{ log.username }}</span>
                                        </td>
                                        <td>
                                            <span class="badge bg-{% if log.action == 'create' %}success{% elif log.action == 'update' %}warning{% elif log.action == 'delete' %}danger{% else %}info{% endif %}">
                                                {{ log.get_action_display }}
                                            </span>
                                        </td>
                                        <td>
                                            <small>{{ log.description|truncatechars:80 }}</small>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <div class="text-center py-4">
                            <i class="fas fa-history fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">Keine Audit-Logs vorhanden</h5>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Benutzeraktivität -->
        <div class="col-lg-4">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-users me-2"></i>Benutzeraktivität
                    </h6>
                </div>
                <div class="card-body">
                    {% if user_activity %}
                        {% for user in user_activity %}
                        <div class="d-flex justify-content-between align-items-center mb-2">
                            <span class="text-sm">{{ user.username }}</span>
                            <span class="badge bg-primary">{{ user.count }}</span>
                        </div>
                        {% endfor %}
                    {% else %}
                        <p class="text-muted">Keine Benutzeraktivität erfasst</p>
                    {% endif %}
                </div>
            </div>

            <!-- Backup Status -->
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-database me-2"></i>Backup Status
                    </h6>
                    <a href="{% url 'stiftung:backup_management' %}" class="btn btn-primary btn-sm">
                        <i class="fas fa-cog me-1"></i>Verwalten
                    </a>
                </div>
                <div class="card-body">
                    {% if stats.recent_backups %}
                        {% for backup in stats.recent_backups %}
                        <div class="d-flex justify-content-between align-items-center mb-2">
                            <div>
                                <small class="text-muted">{{ backup.created_at|date:"d.m. H:i" }}</small><br>
                                <span class="text-sm">{{ backup.get_backup_type_display }}</span>
                            </div>
                            <span class="badge bg-{% if backup.status == 'completed' %}success{% elif backup.status == 'failed' %}danger{% elif backup.status == 'running' %}primary{% else %}secondary{% endif %}">
                                {{ backup.get_status_display }}
                            </span>
                        </div>
                        {% endfor %}
                    {% else %}
                        <p class="text-muted">Keine Backups vorhanden</p>
                        <a href="{% url 'stiftung:backup_management' %}" class="btn btn-success btn-sm">
                            <i class="fas fa-plus me-1"></i>Erstes Backup erstellen
                        </a>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Schnellzugriff -->
    <div class="row">
        <div class="col-12">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-tachometer-alt me-2"></i>Schnellzugriff
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:app_settings' %}" class="btn btn-outline-primary w-100">
                                <i class="fas fa-cogs d-block mb-2 fa-2x"></i>
                                <span>App Settings</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:audit_log_list' %}" class="btn btn-outline-primary w-100">
                                <i class="fas fa-history d-block mb-2 fa-2x"></i>
                                <span>Audit Logs</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:backup_management' %}" class="btn btn-outline-success w-100">
                                <i class="fas fa-database d-block mb-2 fa-2x"></i>
                                <span>Backup & Restore</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:unterstuetzungen_list' %}" class="btn btn-outline-success w-100">
                                <i class="fas fa-hand-holding-usd d-block mb-2 fa-2x"></i>
                                <span>Destinatärunterstützungen</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="/admin/" class="btn btn-outline-warning w-100" target="_blank">
                                <i class="fas fa-user-shield d-block mb-2 fa-2x"></i>
                                <span>Django Admin</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:user_management' %}" class="btn btn-outline-info w-100">
                                <i class="fas fa-users d-block mb-2 fa-2x"></i>
                                <span>Benutzerverwaltung</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-outline-secondary w-100">
                                <i class="fas fa-folder-open d-block mb-2 fa-2x"></i>
                                <span>Dokumentenverwaltung</span>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}
