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

{% block title %}Verwaltungskosten - 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-file-invoice-dollar me-2"></i>Verwaltungskosten
                </h1>
                <div class="btn-group" role="group">
                    <a href="{% url 'stiftung:geschaeftsfuehrung' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i>Zurück zur Übersicht
                    </a>
                    <a href="{% url 'stiftung:verwaltungskosten_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus me-1"></i>Neue Ausgabe
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter -->
    <div class="row mb-3">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form method="get" class="row g-3">
                        <div class="col-md-4">
                            <label for="kategorie" class="form-label">Kategorie</label>
                            <select name="kategorie" id="kategorie" class="form-select">
                                <option value="">Alle Kategorien</option>
                                {% for key, value in kategorien %}
                                    <option value="{{ key }}" {% if kategorie_filter == key %}selected{% endif %}>
                                        {{ value }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label for="status" class="form-label">Status</label>
                            <select name="status" id="status" class="form-select">
                                <option value="">Alle Status</option>
                                {% for key, value in status_choices %}
                                    <option value="{{ key }}" {% if status_filter == key %}selected{% endif %}>
                                        {{ value }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">&nbsp;</label>
                            <div class="d-flex gap-2">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-filter me-1"></i>Filtern
                                </button>
                                <a href="{% url 'stiftung:verwaltungskosten_list' %}" class="btn btn-outline-secondary">
                                    <i class="fas fa-times me-1"></i>Zurücksetzen
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- Kosten Liste -->
    <div class="row">
        <div class="col-12">
            <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-list me-2"></i>Verwaltungskosten
                        {% if page_obj.paginator.count %}
                            ({{ page_obj.paginator.count }} Einträge)
                        {% endif %}
                    </h6>
                </div>
                <div class="card-body">
                    {% if page_obj.object_list %}
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>Datum</th>
                                        <th>Bezeichnung</th>
                                        <th>Kategorie</th>
                                        <th>Lieferant/Firma</th>
                                        <th>Status</th>
                                        <th>Konto</th>
                                        <th class="text-end">Betrag</th>
                                        <th>Aktionen</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for kosten in page_obj %}
                                    <tr>
                                        <td>{{ kosten.datum|date:"d.m.Y" }}</td>
                                        <td>
                                            <a href="{% url 'stiftung:verwaltungskosten_detail' kosten.pk %}" class="text-decoration-none"><strong>{{ kosten.bezeichnung }}</strong></a>
                                            {% if kosten.rechnungsnummer %}
                                                <br><small class="text-muted">RG: {{ kosten.rechnungsnummer }}</small>
                                            {% endif %}
                                            {% if kosten.kategorie == 'fahrtkosten' and kosten.km_anzahl %}
                                                <br><small class="text-info">
                                                    <i class="fas fa-route me-1"></i>
                                                    {{ kosten.km_anzahl }} km 
                                                    {% if kosten.von_ort and kosten.nach_ort %}
                                                        ({{ kosten.von_ort }} → {{ kosten.nach_ort }})
                                                    {% endif %}
                                                </small>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <span class="badge bg-info">{{ kosten.get_kategorie_display }}</span>
                                        </td>
                                        <td>{{ kosten.lieferant_firma|default:"-" }}</td>
                                        <td>
                                            <span class="badge bg-{{ kosten.get_status_color }}">{{ kosten.get_status_display }}</span>
                                        </td>
                                        <td>
                                            {% if kosten.konto %}
                                                <small>{{ kosten.konto.bank_name }}</small><br>
                                                <small class="text-muted">{{ kosten.konto.kontoname }}</small>
                                            {% else %}
                                                <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                        <td class="text-end">
                                            <strong>€{{ kosten.betrag|floatformat:2 }}</strong>
                                            {% if kosten.kategorie == 'fahrtkosten' and kosten.km_satz %}
                                                <br><small class="text-muted">{{ kosten.km_satz }}€/km</small>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm" role="group">
                                                <a href="{% url 'stiftung:verwaltungskosten_edit' kosten.pk %}" class="btn btn-outline-primary" title="Bearbeiten">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="{% url 'stiftung:verwaltungskosten_delete' kosten.pk %}" class="btn 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="Seitennavigation">
                            <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 kategorie_filter %}&kategorie={{ kategorie_filter }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}">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="?page={{ num }}{% if kategorie_filter %}&kategorie={{ kategorie_filter }}{% endif %}{% if status_filter %}&status={{ status_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 kategorie_filter %}&kategorie={{ kategorie_filter }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}">Nächste</a>
                                    </li>
                                {% endif %}
                            </ul>
                        </nav>
                        {% endif %}

                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-file-invoice-dollar fa-3x text-muted mb-3"></i>
                            {% if kategorie_filter or status_filter %}
                                <p class="text-muted">Keine Verwaltungskosten mit den gewählten Filtern gefunden.</p>
                                <a href="{% url 'stiftung:verwaltungskosten_list' %}" class="btn btn-outline-secondary">
                                    <i class="fas fa-times me-1"></i>Filter zurücksetzen
                                </a>
                            {% else %}
                                <p class="text-muted">Noch keine Verwaltungskosten angelegt.</p>
                                <a href="#" class="btn btn-primary">
                                    <i class="fas fa-plus me-1"></i>Erste Ausgabe anlegen
                                </a>
                            {% endif %}
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

{% endblock %}
