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

{% block title %}Förderungen - Stiftungsverwaltung{% endblock %}

{% block content %}

    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3">
            <i class="fas fa-gift text-primary me-2"></i>Förderungen
        </h1>
        <a href="{% url 'stiftung:foerderung_create' %}" class="btn btn-primary">
            <i class="fas fa-plus me-2"></i> Neue Förderung
        </a>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <h5 class="card-title">Gesamtbetrag</h5>
                    <h3 class="card-text">€{{ total_betrag|floatformat:2 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white">
                <div class="card-body">
                    <h5 class="card-title">Durchschnitt</h5>
                    <h3 class="card-text">€{{ avg_betrag|floatformat:2 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <h5 class="card-title">Anzahl</h5>
                    <h3 class="card-text">{{ page_obj.paginator.count }}</h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="card mb-4">
        <div class="card-header">
            <h6 class="mb-0">
                <i class="fas fa-filter me-2"></i>Filter
            </h6>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-2">
                    <label for="jahr" class="form-label">Jahr</label>
                    <select name="jahr" id="jahr" class="form-select">
                        <option value="">Alle Jahre</option>
                        {% for year in jahre %}
                        <option value="{{ year }}" {% if filter_jahr == year %}selected{% endif %}>{{ year }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="kategorie" class="form-label">Kategorie</label>
                    <select name="kategorie" id="kategorie" class="form-select">
                        <option value="">Alle Kategorien</option>
                        {% for kategorie in kategorien %}
                        <option value="{{ kategorie.0 }}" {% if filter_kategorie == kategorie.0 %}selected{% endif %}>{{ kategorie.1 }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="status" class="form-label">Status</label>
                    <select name="status" id="status" class="form-select">
                        <option value="">Alle Status</option>
                        {% for status in status_choices %}
                        <option value="{{ status.0 }}" {% if filter_status == status.0 %}selected{% endif %}>{{ status.1 }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="person" class="form-label">Person</label>
                    <input type="text" name="person" id="person" class="form-control" value="{{ filter_person }}" placeholder="Nachname suchen">
                </div>
                <div class="col-md-3 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary me-2">
                        <i class="fas fa-search me-2"></i>Filtern
                    </button>
                    <a href="{% url 'stiftung:foerderung_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-times me-2"></i>Zurücksetzen
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Förderungen Table -->
    <div class="card">
        <div class="card-header">
            <h6 class="mb-0">
                <i class="fas fa-list me-2"></i>Förderungen
            </h6>
        </div>
        <div class="card-body">
            {% if page_obj %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Person</th>
                            <th>Jahr</th>
                            <th>Betrag</th>
                            <th>Kategorie</th>
                            <th>Status</th>
                            <th>Antragsdatum</th>
                            <th>Aktionen</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for foerderung in page_obj %}
                        <tr>
                            <td>
                                <a href="{% url 'stiftung:destinataer_detail' foerderung.destinataer.pk %}">
                                    {{ foerderung.person.get_full_name }}
                                </a>
                            </td>
                            <td>{{ foerderung.jahr }}</td>
                            <td>€{{ foerderung.betrag|floatformat:2 }}</td>
                            <td>
                                <span class="badge bg-secondary">{{ foerderung.get_kategorie_display }}</span>
                            </td>
                            <td>
                                <span class="badge bg-{{ foerderung.get_status_color }}">{{ foerderung.get_status_display }}</span>
                            </td>
                            <td>{{ foerderung.antragsdatum|date:"d.m.Y" }}</td>
                            <td>
                                <div class="btn-group" role="group">
                                    <a href="{% url 'stiftung:foerderung_detail' foerderung.pk %}" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <a href="{% url 'stiftung:foerderung_update' foerderung.pk %}" class="btn btn-sm btn-outline-warning">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="{% url 'stiftung:foerderung_delete' foerderung.pk %}" class="btn btn-sm btn-outline-danger">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            {% if page_obj.has_other_pages %}
            <nav aria-label="Förderungen Pagination">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if filter_jahr %}&jahr={{ filter_jahr }}{% endif %}{% if filter_kategorie %}&kategorie={{ filter_kategorie }}{% endif %}{% if filter_status %}&status={{ filter_status }}{% endif %}{% if filter_person %}&person={{ filter_person }}{% endif %}">
                            <i class="fas fa-chevron-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 filter_jahr %}&jahr={{ filter_jahr }}{% endif %}{% if filter_kategorie %}&kategorie={{ filter_kategorie }}{% endif %}{% if filter_status %}&status={{ filter_status }}{% endif %}{% if filter_person %}&person={{ filter_person }}{% 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 filter_jahr %}&jahr={{ filter_jahr }}{% endif %}{% if filter_kategorie %}&kategorie={{ filter_kategorie }}{% endif %}{% if filter_status %}&status={{ filter_status }}{% endif %}{% if filter_person %}&person={{ filter_person }}{% endif %}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}

            {% else %}
            <div class="text-center py-5">
                <i class="fas fa-gift fa-3x text-muted mb-3"></i>
                <h5 class="text-muted">Keine Förderungen gefunden</h5>
                <p class="text-muted">Erstellen Sie Ihre erste Förderung oder passen Sie die Filter an.</p>
                <a href="{% url 'stiftung:foerderung_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>Neue Förderung erstellen
                </a>
            </div>
            {% endif %}
        </div>
    </div>

{% endblock %} 
