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

{% block title %}Benutzerverwaltung - 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-users me-2"></i>Benutzerverwaltung
                </h1>
                <div>
                    <a href="{% url 'stiftung:user_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus me-1"></i>Neuen Benutzer erstellen
                    </a>
                    <a href="{% url 'stiftung:administration' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i>Zurück zur Administration
                    </a>
                </div>
            </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">Benutzer Gesamt</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.total_users }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-users 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">Aktive Benutzer</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.active_users }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user-check 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">Staff Benutzer</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.staff_users }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user-shield 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-danger 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-danger text-uppercase mb-1">Inaktive Benutzer</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.inactive_users }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user-times fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter und Suche -->
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-filter me-2"></i>Filter und Suche
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">Suche</label>
                    <input type="text" class="form-control" name="search" value="{{ search }}" placeholder="Benutzername, E-Mail, Name...">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Status</label>
                    <select class="form-select" name="status">
                        <option value="">Alle Status</option>
                        <option value="active" {% if status_filter == 'active' %}selected{% endif %}>Aktiv</option>
                        <option value="inactive" {% if status_filter == 'inactive' %}selected{% endif %}>Inaktiv</option>
                        <option value="staff" {% if status_filter == 'staff' %}selected{% endif %}>Staff</option>
                    </select>
                </div>
                <div class="col-md-3 d-flex align-items-end">
                    <div class="btn-group w-100">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search me-1"></i>Filtern
                        </button>
                        <a href="{% url 'stiftung:user_management' %}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-1"></i>Reset
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Benutzer Liste -->
    <div class="card shadow">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">
                <i class="fas fa-list me-2"></i>Benutzer
                <span class="badge bg-primary ms-2">{{ page_obj.paginator.count|intcomma }}</span>
            </h6>
        </div>
        <div class="card-body">
            {% if page_obj %}
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Benutzer</th>
                                <th>E-Mail</th>
                                <th>Name</th>
                                <th>Status</th>
                                <th>Letzte Anmeldung</th>
                                <th>Mitglied seit</th>
                                <th>Aktionen</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for user in page_obj %}
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="avatar avatar-sm me-2">
                                            <i class="fas fa-user-circle fa-2x text-muted"></i>
                                        </div>
                                        <div>
                                            <strong>{{ user.username }}</strong>
                                            {% if user.is_superuser %}
                                            <span class="badge bg-danger ms-1">Superuser</span>
                                            {% endif %}
                                            {% if user.is_staff %}
                                            <span class="badge bg-warning ms-1">Staff</span>
                                            {% endif %}
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    {% if user.email %}
                                    <a href="mailto:{{ user.email }}">{{ user.email }}</a>
                                    {% else %}
                                    <span class="text-muted">Keine E-Mail</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if user.first_name or user.last_name %}
                                    {{ user.first_name }} {{ user.last_name }}
                                    {% else %}
                                    <span class="text-muted">Kein Name</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if user.is_active %}
                                    <span class="badge bg-success">Aktiv</span>
                                    {% else %}
                                    <span class="badge bg-danger">Inaktiv</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if user.last_login %}
                                    <span title="{{ user.last_login|date:'d.m.Y H:i:s' }}">
                                        {{ user.last_login|naturaltime }}
                                    </span>
                                    {% else %}
                                    <span class="text-muted">Nie angemeldet</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <span title="{{ user.date_joined|date:'d.m.Y H:i:s' }}">
                                        {{ user.date_joined|naturaltime }}
                                    </span>
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <a href="{% url 'stiftung:user_detail' user.pk %}" class="btn btn-outline-primary btn-sm" title="Details">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{% url 'stiftung:user_edit' user.pk %}" class="btn btn-outline-secondary btn-sm" title="Bearbeiten">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="{% url 'stiftung:user_permissions' user.pk %}" class="btn btn-outline-warning btn-sm" title="Berechtigungen">
                                            <i class="fas fa-key"></i>
                                        </a>
                                        {% if user != request.user %}
                                        <a href="{% url 'stiftung:user_delete' user.pk %}" class="btn btn-outline-danger btn-sm" title="Löschen">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                        {% endif %}
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                <nav aria-label="User Pagination">
                    <ul class="pagination justify-content-center">
                        {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.previous_page_number }}">Vorherige</a>
                        </li>
                        {% endif %}
                        
                        {% for num in page_obj.paginator.page_range %}
                        {% if page_obj.number == num %}
                        <li class="page-item active">
                            <span class="page-link">{{ num }}</span>
                        </li>
                        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ num }}">{{ num }}</a>
                        </li>
                        {% endif %}
                        {% endfor %}
                        
                        {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.next_page_number }}">Nächste</a>
                        </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
            {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-users fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">Keine Benutzer gefunden</h5>
                    <p class="text-muted">Passen Sie die Filter an oder erstellen Sie den ersten Benutzer.</p>
                    <a href="{% url 'stiftung:user_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus me-1"></i>Ersten Benutzer erstellen
                    </a>
                </div>
            {% endif %}
        </div>
    </div>

{% endblock %}
