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

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

{% block content %}

    <!-- Header -->
    <div class="row mb-4">
        <div class="col-md-6">
            <h1 class="h3">
                <i class="fas fa-calculator text-success me-2"></i>
                Landabrechnungen verwalten
            </h1>
        </div>
        <div class="col-md-6 text-end">
            <a href="{% url 'stiftung:land_abrechnung_create' %}" class="btn btn-success">
                <i class="fas fa-plus me-2"></i>Neue Abrechnung
            </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-filter me-2"></i>Filter & Suche
            </h6>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <label for="jahr" class="form-label">Jahr</label>
                    <select class="form-select" id="jahr" name="jahr">
                        <option value="">Alle Jahre</option>
                        {% for jahr in jahre %}
                        <option value="{{ jahr }}" {% if jahr_filter == jahr|stringformat:"s" %}selected{% endif %}>
                            {{ jahr }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-4">
                    <label for="land" class="form-label">Länderei</label>
                    <select class="form-select" id="land" name="land">
                        <option value="">Alle Ländereien</option>
                        {% for land in laendereien %}
                        <option value="{{ land.pk }}" {% if land_filter == land.pk|stringformat:"s" %}selected{% endif %}>
                            {{ land }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="paechter" class="form-label">Pächter</label>
                    <select class="form-select" id="paechter" name="paechter">
                        <option value="">Alle Pächter</option>
                        {% for paechter in paechter_list %}
                        <option value="{{ paechter.pk }}" {% if paechter_filter == paechter.pk|stringformat:"s" %}selected{% endif %}>
                            {{ paechter.get_full_name }}
                        </option>
                        {% endfor %}
                    </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>Filtern
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Statistics -->
    {% if stats %}
    <div class="row mb-4">
        <div class="col-md-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">
                                Gesamteinnahmen
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                €{{ stats.total_einnahmen|default:0|floatformat:2 }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-euro-sign fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-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">
                                Gesamtausgaben
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                €{{ stats.total_ausgaben|default:0|floatformat:2 }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-receipt fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-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">
                                Anzahl Abrechnungen
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ stats.anzahl_abrechnungen|default:0 }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-calculator fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- Abrechnungen Liste -->
    <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>Landabrechnungen
            </h6>
        </div>
        <div class="card-body">
            {% if abrechnungen %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>Jahr</th>
                                <th>Länderei</th>
                                <th>Pächter</th>
                                <th>Einnahmen</th>
                                <th>Ausgaben</th>
                                <th>Nettoergebnis</th>
                                <th>USt</th>
                                <th>Aktionen</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for abrechnung in abrechnungen %}
                            <tr>
                                <td><strong>{{ abrechnung.abrechnungsjahr }}</strong></td>
                                <td>
                                    <a href="{% url 'stiftung:land_detail' abrechnung.land.pk %}">
                                        {{ abrechnung.land }}
                                    </a>
                                </td>
                                <td>
                                    {% if abrechnung.land.aktueller_paechter %}
                                        <a href="{% url 'stiftung:paechter_detail' abrechnung.land.aktueller_paechter.pk %}">
                                            {{ abrechnung.land.aktueller_paechter.get_full_name }}
                                        </a>
                                    {% else %}
                                        <span class="text-muted">Kein Pächter</span>
                                    {% endif %}
                                </td>
                                <td class="text-success">€{{ abrechnung.einnahmen_gesamt|floatformat:2 }}</td>
                                <td class="text-danger">€{{ abrechnung.ausgaben_gesamt|floatformat:2 }}</td>
                                <td>
                                    <strong class="{% if abrechnung.nettoergebnis >= 0 %}text-success{% else %}text-danger{% endif %}">
                                        €{{ abrechnung.nettoergebnis|floatformat:2 }}
                                    </strong>
                                </td>
                                <td>
                                    {% if abrechnung.ust_pacht_betrag > 0 %}
                                        €{{ abrechnung.ust_pacht_betrag|floatformat:2 }}
                                    {% else %}
                                        <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <a href="{% url 'stiftung:land_abrechnung_detail' abrechnung.pk %}" 
                                           class="btn btn-sm btn-outline-primary" title="Anzeigen">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{% url 'stiftung:land_abrechnung_update' abrechnung.pk %}" 
                                           class="btn btn-sm btn-outline-warning" title="Bearbeiten">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="{% url 'stiftung:land_abrechnung_delete' abrechnung.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 abrechnungen.has_other_pages %}
                <nav aria-label="Abrechnungen Pagination">
                    <ul class="pagination justify-content-center">
                        {% if abrechnungen.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ abrechnungen.previous_page_number }}{% if jahr_filter %}&jahr={{ jahr_filter }}{% endif %}{% if land_filter %}&land={{ land_filter }}{% endif %}{% if paechter_filter %}&paechter={{ paechter_filter }}{% endif %}">Zurück</a>
                            </li>
                        {% endif %}

                        {% for num in abrechnungen.paginator.page_range %}
                            {% if abrechnungen.number == num %}
                                <li class="page-item active">
                                    <span class="page-link">{{ num }}</span>
                                </li>
                            {% elif num > abrechnungen.number|add:'-3' and num < abrechnungen.number|add:'3' %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ num }}{% if jahr_filter %}&jahr={{ jahr_filter }}{% endif %}{% if land_filter %}&land={{ land_filter }}{% endif %}{% if paechter_filter %}&paechter={{ paechter_filter }}{% endif %}">{{ num }}</a>
                                </li>
                            {% endif %}
                        {% endfor %}

                        {% if abrechnungen.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ abrechnungen.next_page_number }}{% if jahr_filter %}&jahr={{ jahr_filter }}{% endif %}{% if land_filter %}&land={{ land_filter }}{% endif %}{% if paechter_filter %}&paechter={{ paechter_filter }}{% endif %}">Weiter</a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
            {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-calculator fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">Keine Landabrechnungen vorhanden</h5>
                    <p class="text-muted">Erstellen Sie die erste Landabrechnung.</p>
                    <a href="{% url 'stiftung:land_abrechnung_create' %}" class="btn btn-success">
                        <i class="fas fa-plus me-2"></i>Erste Abrechnung erstellen
                    </a>
                </div>
            {% endif %}
        </div>
    </div>

{% endblock %}
