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

{% block title %}Personen - Stiftungsverwaltung{% endblock %}

{% block content %}

    <!-- Header -->
    <div class="row mb-4">
        <div class="col-md-6">
            <h1 class="h3">
                <i class="fas fa-users text-primary me-2"></i>
                Personenverwaltung
            </h1>
            <p class="text-muted">Verwalten Sie alle Stiftungsmitglieder und deren Informationen</p>
        </div>
        <div class="col-md-6 text-end">
            <a href="{% url 'stiftung:person_create' %}" class="btn btn-primary">
                <i class="fas fa-user-plus me-2"></i>Neue Person
            </a>
        </div>
    </div>

    <!-- Search and Filters -->
    <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-search me-2"></i>Suche & Filter
            </h6>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <label for="search" class="form-label">Suche</label>
                    <input type="text" class="form-control" id="search" name="search" 
                           value="{{ search_query }}" placeholder="Nach Namen, E-Mail oder Familienzweig suchen...">
                </div>
                <div class="col-md-3">
                    <label for="familienzweig" class="form-label">Familienzweig</label>
                    <select class="form-control" id="familienzweig" name="familienzweig">
                        <option value="">Alle Familienzweige</option>
                        {% for value, label in familienzweig_choices %}
                        <option value="{{ value }}" {% if value == familienzweig_filter %}selected{% endif %}>
                            {{ label }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="aktiv" class="form-label">Status</label>
                    <select class="form-control" id="aktiv" name="aktiv">
                        <option value="">Alle</option>
                        <option value="true" {% if aktiv_filter == 'true' %}selected{% endif %}>Nur Aktive</option>
                        <option value="false" {% if aktiv_filter == 'false' %}selected{% endif %}>Nur Inaktive</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">&nbsp;</label>
                    <div>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search me-2"></i>Suchen
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Results -->
    <div class="card shadow">
        <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-list me-2"></i>Ergebnisse
                {% if page_obj %}
                <span class="badge bg-secondary ms-2">{{ page_obj.paginator.count }} Personen</span>
                {% endif %}
            </h6>
            <div>
                <a href="{% url 'stiftung:home' %}" class="btn btn-outline-secondary btn-sm">
                    <i class="fas fa-arrow-left me-2"></i>Zurück zur Startseite
                </a>
            </div>
        </div>
        <div class="card-body">
            {% if page_obj %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>Name</th>
                                <th>Familienzweig</th>
                                <th>Geburtsdatum</th>
                                <th>E-Mail</th>
                                <th>Telefon</th>
                                <th>Status</th>
                                <th>Förderungen</th>
                                <th>Aktionen</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for person in page_obj %}
                            <tr>
                                <td>
                                    <strong>{{ person.get_full_name }}</strong>
                                    {% if person.iban %}
                                    <br><small class="text-muted font-monospace">{{ person.iban }}</small>
                                    {% endif %}
                                </td>
                                <td>
                                    <span class="badge bg-info">{{ person.get_familienzweig_display }}</span>
                                </td>
                                <td>
                                    {% if person.geburtsdatum %}
                                        {{ person.geburtsdatum|date:"d.m.Y" }}
                                    {% else %}
                                        <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if person.email %}
                                        <a href="mailto:{{ person.email }}">{{ person.email }}</a>
                                    {% else %}
                                        <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if person.telefon %}
                                        {{ person.telefon }}
                                    {% else %}
                                        <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if person.aktiv %}
                                        <span class="badge bg-success">Aktiv</span>
                                    {% else %}
                                        <span class="badge bg-secondary">Inaktiv</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if person.total_foerderungen %}
                                        <strong class="text-success">€{{ person.total_foerderungen|floatformat:2 }}</strong>
                                    {% else %}
                                        <span class="text-muted">€0.00</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <a href="{% url 'stiftung:destinataer_detail' person.pk %}" 
                                           class="btn btn-sm btn-outline-primary" title="Anzeigen">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{% url 'stiftung:person_update' person.pk %}" 
                                           class="btn btn-sm btn-outline-warning" title="Bearbeiten">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="{% url 'stiftung:person_delete' person.pk %}" 
                                           class="btn btn-sm btn-outline-danger" title="Löschen">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                <nav aria-label="Personen Navigation">
                    <ul class="pagination justify-content-center">
                        {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page=1{% if search_query %}&search={{ search_query }}{% endif %}{% if familienzweig_filter %}&familienzweig={{ familienzweig_filter }}{% endif %}{% if aktiv_filter %}&aktiv={{ aktiv_filter }}{% endif %}">
                                    <i class="fas fa-angle-double-left"></i>
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if familienzweig_filter %}&familienzweig={{ familienzweig_filter }}{% endif %}{% if aktiv_filter %}&aktiv={{ aktiv_filter }}{% endif %}">
                                    <i class="fas fa-angle-left"></i>
                                </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="?page={{ num }}{% if search_query %}&search={{ search_query }}{% endif %}{% if familienzweig_filter %}&familienzweig={{ familienzweig_filter }}{% endif %}{% if aktiv_filter %}&aktiv={{ aktiv_filter }}{% endif %}">
                                        {{ num }}
                                    </a>
                                </li>
                            {% endif %}
                        {% endfor %}

                        {% if page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if familienzweig_filter %}&familienzweig={{ familienzweig_filter }}{% endif %}{% if aktiv_filter %}&aktiv={{ aktiv_filter }}{% endif %}">
                                    <i class="fas fa-angle-right"></i>
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&search={{ search_query }}{% endif %}{% if familienzweig_filter %}&familienzweig={{ familienzweig_filter }}{% endif %}{% if aktiv_filter %}&aktiv={{ aktiv_filter }}{% endif %}">
                                    <i class="fas fa-angle-double-right"></i>
                                </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 Personen gefunden</h5>
                    <p class="text-muted">
                        {% if search_query or familienzweig_filter or aktiv_filter %}
                            Versuchen Sie andere Suchkriterien oder 
                            <a href="{% url 'stiftung:person_list' %}">alle Personen anzeigen</a>.
                        {% else %}
                            Erstellen Sie Ihre erste Person mit dem Button oben.
                        {% endif %}
                    </p>
                </div>
            {% endif %}
        </div>
    </div>

{% endblock %}

{% block extra_js %}
<script>
    // Auto-submit form when filters change
    document.getElementById('familienzweig').addEventListener('change', function() {
        this.form.submit();
    });
    
    document.getElementById('aktiv').addEventListener('change', function() {
        this.form.submit();
    });
</script>
{% endblock %} 
