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

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

{% block content %}

    <!-- Header -->
    <div class="row mb-4">
        <div class="col-md-8">
            <h1 class="h3">
                <i class="fas fa-map text-success me-2"></i>
                {{ land }}
            </h1>
            <p class="text-muted">
                Lfd. Nr. {{ land.lfd_nr }}
                {% if land.ew_nummer %} | EW-Nummer: {{ land.ew_nummer }}{% endif %}
                {% if land.grundbuchblatt %} | Grundbuchblatt: {{ land.grundbuchblatt }}{% endif %}
            </p>
            {% if land.adresse %}
            <p class="text-muted">
                <i class="fas fa-map-marker-alt me-1"></i>{{ land.adresse }}
            </p>
            {% endif %}
        </div>
        <div class="col-md-4 text-end">
            <div class="btn-group" role="group">
                <a href="{% url 'stiftung:land_update' land.pk %}" class="btn btn-warning">
                    <i class="fas fa-edit me-2"></i>Bearbeiten
                </a>
                <a href="{% url 'stiftung:land_export' land.pk %}" class="btn btn-success">
                    <i class="fas fa-download me-2"></i>Export
                </a>
                <a href="{% url 'stiftung:land_delete' land.pk %}" class="btn btn-danger">
                    <i class="fas fa-trash me-2"></i>Löschen
                </a>
            </div>
            <div class="mt-2">
                <a href="{% url 'stiftung:land_list' %}" class="btn btn-outline-secondary btn-sm">
                    <i class="fas fa-arrow-left me-2"></i>Zurück zur Liste
                </a>
            </div>
        </div>
    </div>

    <!-- Status Badge -->
    <div class="row mb-4">
        <div class="col-12">
            {% if land.aktiv %}
                <span class="badge bg-success fs-6">
                    <i class="fas fa-check-circle me-2"></i>Aktiv
                </span>
            {% else %}
                <span class="badge bg-secondary fs-6">
                    <i class="fas fa-times-circle me-2"></i>Inaktiv
                </span>
            {% endif %}
        </div>
    </div>

    <!-- Main Information -->
    <div class="row">
        <!-- Left Column - Land Details -->
        <div class="col-lg-8">
            <!-- Basic Information -->
            <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-info-circle me-2"></i>Grundinformationen
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td><strong>Gemeinde:</strong></td>
                                    <td>{{ land.gemeinde }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Gemarkung:</strong></td>
                                    <td>{{ land.gemarkung }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Flur:</strong></td>
                                    <td>{{ land.flur }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Flurstück:</strong></td>
                                    <td>{{ land.flurstueck }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td><strong>Amtsgericht:</strong></td>
                                    <td>{{ land.amtsgericht }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Gesamtgröße:</strong></td>
                                    <td>
                                        <strong>{{ land.groesse_qm|floatformat:2 }} qm</strong>
                                        <br>
                                        <small class="text-muted">({{ land.groesse_hektar|floatformat:2 }} ha)</small>
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Verpachtet:</strong></td>
                                    <td>
                                        {{ land.get_verpachtete_flaeche_aktuell|floatformat:2 }} qm
                                        <br>
                                        <small class="text-muted">({{ land.get_verpachtete_flaeche_aktuell_hektar|floatformat:2 }} ha)</small>
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Verpachtungsgrad:</strong></td>
                                    <td>
                                        {% with verpachtungsgrad=land.get_verpachtungsgrad %}
                                        {% if verpachtungsgrad > 90 %}
                                            <span class="badge bg-success">{{ verpachtungsgrad|floatformat:1 }}%</span>
                                        {% elif verpachtungsgrad > 70 %}
                                            <span class="badge bg-warning">{{ verpachtungsgrad|floatformat:1 }}%</span>
                                        {% else %}
                                            <span class="badge bg-danger">{{ verpachtungsgrad|floatformat:1 }}%</span>
                                        {% endif %}
                                        {% endwith %}
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Land Use Details -->
            <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-leaf me-2"></i>Landnutzung
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3 text-center">
                            <div class="border rounded p-3">
                                <h4 class="text-success">{{ land.gruenland_qm|floatformat:0 }}</h4>
                                <p class="text-muted mb-0">Grünland (qm)</p>
                                <small class="text-muted">({{ land.gruenland_hektar|floatformat:2 }} ha)</small>
                            </div>
                        </div>
                        <div class="col-md-3 text-center">
                            <div class="border rounded p-3">
                                <h4 class="text-warning">{{ land.acker_qm|floatformat:0 }}</h4>
                                <p class="text-muted mb-0">Ackerland (qm)</p>
                                <small class="text-muted">({{ land.acker_hektar|floatformat:2 }} ha)</small>
                            </div>
                        </div>
                        <div class="col-md-3 text-center">
                            <div class="border rounded p-3">
                                <h4 class="text-info">{{ land.wald_qm|floatformat:0 }}</h4>
                                <p class="text-muted mb-0">Wald (qm)</p>
                                <small class="text-muted">({{ land.wald_hektar|floatformat:2 }} ha)</small>
                            </div>
                        </div>
                        <div class="col-md-3 text-center">
                            <div class="border rounded p-3">
                                <h4 class="text-secondary">{{ land.sonstiges_qm|floatformat:0 }}</h4>
                                <p class="text-muted mb-0">Sonstiges (qm)</p>
                                <small class="text-muted">({{ land.sonstiges_hektar|floatformat:2 }} ha)</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Aktuelle Verpachtung -->
            {% if land.aktueller_paechter %}
            <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-handshake me-2"></i>Aktuelle Verpachtung
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td><strong>Pächter:</strong></td>
                                    <td>
                                        <a href="{% url 'stiftung:paechter_detail' land.aktueller_paechter.pk %}">
                                            {{ land.aktueller_paechter.get_full_name }}
                                        </a>
                                        {% if land.paechter_name and land.paechter_name != land.aktueller_paechter.get_full_name %}
                                        <br><small class="text-muted">{{ land.paechter_name }}</small>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% if land.paechter_anschrift %}
                                <tr>
                                    <td><strong>Anschrift:</strong></td>
                                    <td>{{ land.paechter_anschrift|linebreaks }}</td>
                                </tr>
                                {% endif %}
                                <tr>
                                    <td><strong>Pachtbeginn:</strong></td>
                                    <td>{{ land.pachtbeginn|date:"d.m.Y"|default:"Nicht angegeben" }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Pachtende:</strong></td>
                                    <td>
                                        {% if land.pachtende %}
                                            {{ land.pachtende|date:"d.m.Y" }}
                                        {% else %}
                                            <span class="text-muted">Unbefristet</span>
                                        {% endif %}
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td><strong>Zahlungsweise:</strong></td>
                                    <td>{{ land.get_zahlungsweise_display }}</td>
                                </tr>
                                {% if land.pachtzins_pauschal %}
                                <tr>
                                    <td><strong>Pachtzins/Jahr:</strong></td>
                                    <td><strong>€{{ land.pachtzins_pauschal|floatformat:2 }}</strong></td>
                                </tr>
                                {% endif %}
                                {% if land.pachtzins_pro_ha %}
                                <tr>
                                    <td><strong>Pachtzins/ha:</strong></td>
                                    <td>€{{ land.pachtzins_pro_ha|floatformat:2 }}</td>
                                </tr>
                                {% endif %}
                                <tr>
                                    <td><strong>USt-Option:</strong></td>
                                    <td>
                                        {% if land.ust_option %}
                                            <span class="badge bg-info">{{ land.ust_satz|floatformat:1 }}% USt</span>
                                        {% else %}
                                            <span class="text-muted">Keine USt</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Autom. Verlängerung:</strong></td>
                                    <td>
                                        {% if land.verlaengerung_klausel %}
                                            <span class="badge bg-success">Ja</span>
                                        {% else %}
                                            <span class="badge bg-secondary">Nein</span>
                                        {% endif %}
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    
                    <!-- Umlagen -->
                    <div class="row mt-3">
                        <div class="col-12">
                            <h6 class="text-muted mb-3">Umlagefähige Kosten:</h6>
                            <div class="row">
                                <div class="col-md-3">
                                    {% if land.grundsteuer_umlage %}
                                        <span class="badge bg-success mb-1">✓ Grundsteuer</span>
                                    {% else %}
                                        <span class="badge bg-secondary mb-1">✗ Grundsteuer</span>
                                    {% endif %}
                                </div>
                                <div class="col-md-3">
                                    {% if land.versicherungen_umlage %}
                                        <span class="badge bg-success mb-1">✓ Versicherungen</span>
                                    {% else %}
                                        <span class="badge bg-secondary mb-1">✗ Versicherungen</span>
                                    {% endif %}
                                </div>
                                <div class="col-md-3">
                                    {% if land.verbandsbeitraege_umlage %}
                                        <span class="badge bg-success mb-1">✓ Verbandsbeiträge</span>
                                    {% else %}
                                        <span class="badge bg-secondary mb-1">✗ Verbandsbeiträge</span>
                                    {% endif %}
                                </div>
                                <div class="col-md-3">
                                    {% if land.jagdpacht_anteil_umlage %}
                                        <span class="badge bg-success mb-1">✓ Jagdpacht</span>
                                    {% else %}
                                        <span class="badge bg-secondary mb-1">✗ Jagdpacht</span>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- Verpachtungs-Übersicht -->
            {% if land.aktueller_paechter %}
            <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-chart-line me-2"></i>Verpachtungs-Übersicht
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td><strong>Verpachtete Fläche:</strong></td>
                                    <td>
                                        <strong>{{ land.verp_flaeche_aktuell|floatformat:2 }} qm</strong>
                                        <br>
                                        <small class="text-muted">({{ land.verp_flaeche_aktuell_hektar|floatformat:2 }} ha)</small>
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Verpachtungsgrad:</strong></td>
                                    <td>
                                        {% with verpachtungsgrad=land.get_verpachtungsgrad %}
                                        {% if verpachtungsgrad > 90 %}
                                            <span class="badge bg-success">{{ verpachtungsgrad|floatformat:1 }}%</span>
                                        {% elif verpachtungsgrad > 70 %}
                                            <span class="badge bg-warning">{{ verpachtungsgrad|floatformat:1 }}%</span>
                                        {% else %}
                                            <span class="badge bg-danger">{{ verpachtungsgrad|floatformat:1 }}%</span>
                                        {% endif %}
                                        {% endwith %}
                                    </td>
                                </tr>
                                {% if land.pachtzins_pauschal %}
                                <tr>
                                    <td><strong>Pachtzins/Jahr:</strong></td>
                                    <td><strong>€{{ land.pachtzins_pauschal|floatformat:2 }}</strong></td>
                                </tr>
                                {% endif %}
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                {% if land.pachtzins_pro_ha %}
                                <tr>
                                    <td><strong>Pachtzins/ha:</strong></td>
                                    <td>€{{ land.pachtzins_pro_ha|floatformat:2 }}</td>
                                </tr>
                                {% endif %}
                                <tr>
                                    <td><strong>Zahlungsweise:</strong></td>
                                    <td>{{ land.get_zahlungsweise_display }}</td>
                                </tr>
                                <tr>
                                    <td><strong>USt-Behandlung:</strong></td>
                                    <td>
                                        {% if land.ust_option %}
                                            <span class="badge bg-warning">{{ land.ust_satz|floatformat:1 }}% USt</span>
                                        {% else %}
                                            <span class="text-muted">Keine USt</span>
                                        {% endif %}
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Alle Verpachtungen (Historie) -->
            {% if land.neue_verpachtungen.exists %}
            <div class="card shadow mb-4">
                <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-history me-2"></i>Verpachtungshistorie
                    </h6>
                    <span class="badge bg-primary">{{ land.neue_verpachtungen.count }} Verpachtung{{ land.neue_verpachtungen.count|pluralize:"en" }}</span>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Vertragsnummer</th>
                                    <th>Pächter</th>
                                    <th>Laufzeit</th>
                                    <th>Fläche</th>
                                    <th>Pachtzins</th>
                                    <th>Status</th>
                                    <th>Aktionen</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for verpachtung in land.neue_verpachtungen.all %}
                                <tr>
                                    <td>
                                        <code>{{ verpachtung.vertragsnummer }}</code>
                                    </td>
                                    <td>
                                        <a href="{% url 'stiftung:paechter_detail' verpachtung.paechter.pk %}">
                                            {{ verpachtung.paechter.get_full_name }}
                                        </a>
                                    </td>
                                    <td>
                                        {{ verpachtung.pachtbeginn|date:"d.m.Y" }}
                                        {% if verpachtung.pachtende %}
                                            - {{ verpachtung.pachtende|date:"d.m.Y" }}
                                        {% else %}
                                            - unbefristet
                                        {% endif %}
                                    </td>
                                    <td>
                                        {{ verpachtung.verpachtete_flaeche|floatformat:2 }} qm
                                        <br><small class="text-muted">{{ verpachtung.verpachtete_flaeche_hektar }} ha</small>
                                    </td>
                                    <td>
                                        {{ verpachtung.pachtzins_pauschal|floatformat:2 }} €/Jahr
                                        {% if verpachtung.ust_option %}
                                            <br><small class="text-warning">zzgl. {{ verpachtung.ust_satz }}% USt</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if verpachtung.status == 'aktiv' %}
                                            <span class="badge bg-success">{{ verpachtung.get_status_display }}</span>
                                        {% elif verpachtung.status == 'beendet' %}
                                            <span class="badge bg-secondary">{{ verpachtung.get_status_display }}</span>
                                        {% elif verpachtung.status == 'gekuendigt' %}
                                            <span class="badge bg-warning">{{ verpachtung.get_status_display }}</span>
                                        {% else %}
                                            <span class="badge bg-info">{{ verpachtung.get_status_display }}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{% url 'stiftung:land_verpachtung_detail' verpachtung.pk %}" class="btn btn-outline-primary" title="Details anzeigen">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{% url 'stiftung:land_verpachtung_update' verpachtung.pk %}" class="btn btn-outline-warning" title="Bearbeiten">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            {% if verpachtung.status == 'aktiv' %}
                                            <a href="{% url 'stiftung:land_verpachtung_end_direct' verpachtung.pk %}" class="btn btn-outline-danger" title="Beenden">
                                                <i class="fas fa-stop"></i>
                                            </a>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            {% endif %}
            {% else %}
            <!-- Keine Verpachtung -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-muted">
                        <i class="fas fa-info-circle me-2"></i>Verpachtungs-Status
                    </h6>
                </div>
                <div class="card-body">
                    <div class="text-center py-3">
                        <i class="fas fa-exclamation-circle fa-2x text-muted mb-3"></i>
                        <h5 class="text-muted">Nicht verpachtet</h5>
                        <p class="text-muted">Diese Länderei ist aktuell nicht verpachtet.</p>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- Verknüpfte Dokumente -->
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-success">
                        <i class="fas fa-file-alt me-2"></i>Verknüpfte Dokumente
                    </h6>
                    <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-sm btn-success">
                        <i class="fas fa-plus me-2"></i>Dokument verknüpfen
                    </a>
                </div>
                <div class="card-body">
                    {% if verknuepfte_dokumente %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Dokument</th>
                                        <th>Kontext</th>
                                        <th>Beschreibung</th>
                                        <th>Aktionen</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for dokument in verknuepfte_dokumente %}
                                    <tr>
                                        <td>
                                            <strong>{{ dokument.titel }}</strong>
                                            <br>
                                            <small class="text-muted">ID: {{ dokument.paperless_document_id }}</small>
                                        </td>
                                        <td>
                                            <span class="badge bg-secondary">{{ dokument.get_kontext_display }}</span>
                                        </td>
                                        <td>
                                            {% if dokument.beschreibung %}
                                                {{ dokument.beschreibung|truncatewords:10 }}
                                            {% else %}
                                                <span class="text-muted">Keine Beschreibung</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <a href="{{ dokument.get_paperless_url }}" target="_blank" class="btn btn-sm btn-outline-primary" title="In Paperless öffnen">
                                                    <i class="fas fa-external-link-alt"></i>
                                                </a>
                                                <a href="{{ dokument.get_paperless_thumbnail_url }}" target="_blank" class="btn btn-sm btn-outline-info" title="Thumbnail anzeigen">
                                                    <i class="fas fa-image"></i>
                                                </a>
                                                <a href="{% url 'stiftung:dokument_update' dokument.pk %}" class="btn btn-sm btn-outline-warning" title="Bearbeiten">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="{% url 'stiftung:dokument_delete' dokument.pk %}" class="btn btn-sm btn-outline-danger" title="Verknüpfung löschen">
                                                    <i class="fas fa-unlink"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <div class="text-center py-4">
                            <i class="fas fa-file-alt fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">Keine Dokumente verknüpft</h5>
                            <p class="text-muted">Verknüpfen Sie Dokumente aus Paperless mit dieser Länderei.</p>
                            <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-success">
                                <i class="fas fa-plus me-2"></i>Erstes Dokument verknüpfen
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>

            <!-- Notes -->
            {% if land.notizen %}
            <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-sticky-note me-2"></i>Notizen
                    </h6>
                </div>
                <div class="card-body">
                    <p class="mb-0">{{ land.notizen|linebreaks }}</p>
                </div>
            </div>
            {% endif %}

            <!-- Jahresabrechnungen -->
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-success">
                        <i class="fas fa-calculator me-2"></i>Jahresabrechnungen
                    </h6>
                    <a href="{% url 'stiftung:land_abrechnung_create' %}?land={{ land.pk }}" class="btn btn-sm btn-success">
                        <i class="fas fa-plus me-2"></i>Neue Abrechnung
                    </a>
                </div>
                <div class="card-body">
                    {% if land.abrechnungen.all %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Jahr</th>
                                        <th>Einnahmen</th>
                                        <th>Ausgaben</th>
                                        <th>Nettoergebnis</th>
                                        <th>USt</th>
                                        <th>Aktionen</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for abrechnung in land.abrechnungen.all %}
                                    <tr>
                                        <td><strong>{{ abrechnung.abrechnungsjahr }}</strong></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>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <div class="text-center py-4">
                            <i class="fas fa-calculator fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">Keine Abrechnungen vorhanden</h5>
                            <p class="text-muted">Erstellen Sie eine Jahresabrechnung für diese Länderei.</p>
                            <a href="{% url 'stiftung:land_abrechnung_create' %}?land={{ land.pk }}" class="btn btn-success">
                                <i class="fas fa-plus me-2"></i>Erste Abrechnung erstellen
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>

            <!-- Verpachtungs-Management -->
            <div class="card shadow mb-4">
                <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-handshake me-2"></i>Verpachtungs-Management
                    </h6>
                    <div class="btn-group">
                        {% if land.neue_verpachtungen.exists %}
                        <!-- New system: Create new LandVerpachtung -->
                        <a href="{% url 'stiftung:land_verpachtung_create' land_pk=land.pk %}" class="btn btn-sm btn-success">
                            <i class="fas fa-plus me-2"></i>Neue Verpachtung
                        </a>
                        {% elif not land.aktueller_paechter %}
                        <!-- Old system: No current tenant -->
                        <a href="{% url 'stiftung:land_verpachtung_create' land_pk=land.pk %}" class="btn btn-sm btn-success">
                            <i class="fas fa-plus me-2"></i>Verpachtung erstellen
                        </a>
                        {% else %}
                        <!-- Old system: Has current tenant -->
                        <a href="{% url 'stiftung:land_verpachtung_edit' land_pk=land.pk %}" class="btn btn-sm btn-warning">
                            <i class="fas fa-edit me-2"></i>Verpachtung bearbeiten
                        </a>
                        <a href="{% url 'stiftung:land_verpachtung_end' land_pk=land.pk %}" class="btn btn-sm btn-danger">
                            <i class="fas fa-stop me-2"></i>Verpachtung beenden
                        </a>
                        {% endif %}
                    </div>
                </div>
                <div class="card-body">
                    <!-- New LandVerpachtung objects -->
                    {% if land.neue_verpachtungen.all %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Vertragsnummer</th>
                                        <th>Pächter</th>
                                        <th>Zeitraum</th>
                                        <th>Fläche</th>
                                        <th>Pachtzins</th>
                                        <th>Status</th>
                                        <th>Aktionen</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for verpachtung in land.neue_verpachtungen.all %}
                                    <tr>
                                        <td><strong>{{ verpachtung.vertragsnummer }}</strong></td>
                                        <td>
                                            <a href="{% url 'stiftung:paechter_detail' verpachtung.paechter.pk %}">
                                                {{ verpachtung.paechter.get_full_name }}
                                            </a>
                                        </td>
                                        <td>
                                            {{ verpachtung.pachtbeginn|date:"d.m.Y" }} - 
                                            {% if verpachtung.pachtende %}
                                                {{ verpachtung.pachtende|date:"d.m.Y" }}
                                            {% else %}
                                                <span class="text-muted">Unbefristet</span>
                                            {% endif %}
                                            {% if verpachtung.verlaengerung_klausel %}
                                            <br><small class="badge bg-info">Auto-Verlängerung</small>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {{ verpachtung.verpachtete_flaeche|floatformat:2 }} qm
                                            <br><small class="text-muted">({{ verpachtung.verpachtete_flaeche_hektar|floatformat:2 }} ha)</small>
                                        </td>
                                        <td>
                                            €{{ verpachtung.pachtzins_pauschal|floatformat:2 }}/Jahr
                                            {% if verpachtung.ust_option %}
                                            <br><small class="badge bg-warning">+{{ verpachtung.ust_satz|floatformat:1 }}% USt</small>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if verpachtung.status == 'aktiv' %}
                                                <span class="badge bg-success">{{ verpachtung.get_status_display }}</span>
                                            {% elif verpachtung.status == 'beendet' %}
                                                <span class="badge bg-secondary">{{ verpachtung.get_status_display }}</span>
                                            {% else %}
                                                <span class="badge bg-warning">{{ verpachtung.get_status_display }}</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm" role="group">
                                                <a href="{% url 'stiftung:land_verpachtung_detail' verpachtung.pk %}" class="btn btn-outline-primary" title="Details anzeigen">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{% url 'stiftung:land_verpachtung_update' verpachtung.pk %}" class="btn btn-outline-warning" title="Bearbeiten">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                {% if verpachtung.status == 'aktiv' %}
                                                <a href="{% url 'stiftung:land_verpachtung_end_direct' verpachtung.pk %}" class="btn btn-outline-danger" title="Beenden">
                                                    <i class="fas fa-stop"></i>
                                                </a>
                                                {% endif %}
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- Verfügbare Fläche -->
                        <div class="row mt-3">
                            <div class="col-md-4 text-center">
                                <div class="border rounded p-3">
                                    <h5 class="text-success">{{ land.get_verpachtete_flaeche_aktuell|floatformat:0 }} qm</h5>
                                    <p class="text-muted mb-0">Verpachtet</p>
                                    <small class="text-muted">({{ land.get_verpachtete_flaeche_aktuell_hektar|floatformat:2 }} ha)</small>
                                </div>
                            </div>
                            <div class="col-md-4 text-center">
                                <div class="border rounded p-3">
                                    <h5 class="text-info">{{ land.get_verfuegbare_flaeche|floatformat:0 }} qm</h5>
                                    <p class="text-muted mb-0">Verfügbar</p>
                                    <small class="text-muted">(wird berechnet)</small>
                                </div>
                            </div>
                            <div class="col-md-4 text-center">
                                <div class="border rounded p-3">
                                    <h5 class="text-warning">{{ land.get_verpachtungsgrad_neu|floatformat:1 }}%</h5>
                                    <p class="text-muted mb-0">Verpachtungsgrad</p>
                                </div>
                            </div>
                        </div>
                    {% elif land.aktueller_paechter %}
                        <!-- Old system verpachtung display -->
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle me-2"></i>
                            <strong>Legacy Verpachtung:</strong> Diese Länderei verwendet das alte Verpachtungssystem.
                        </div>
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Pächter</th>
                                        <th>Zeitraum</th>
                                        <th>Fläche</th>
                                        <th>Pachtzins</th>
                                        <th>Status</th>
                                        <th>Aktionen</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>
                                            <a href="{% url 'stiftung:paechter_detail' land.aktueller_paechter.pk %}">
                                                {{ land.aktueller_paechter.get_full_name }}
                                            </a>
                                        </td>
                                        <td>
                                            {% if land.pachtbeginn %}{{ land.pachtbeginn|date:"d.m.Y" }}{% else %}<span class="text-muted">N/A</span>{% endif %} - 
                                            {% if land.pachtende %}{{ land.pachtende|date:"d.m.Y" }}{% else %}<span class="text-muted">Unbefristet</span>{% endif %}
                                        </td>
                                        <td>
                                            {{ land.verp_flaeche_aktuell|default_if_none:land.groesse_qm|floatformat:0 }} qm
                                        </td>
                                        <td>
                                            {% if land.pachtzins_aktuell %}€{{ land.pachtzins_aktuell|floatformat:2 }}/Jahr{% else %}<span class="text-muted">N/A</span>{% endif %}
                                        </td>
                                        <td>
                                            <span class="badge bg-warning">Legacy System</span>
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm" role="group">
                                                <a href="{% url 'stiftung:land_verpachtung_edit' land_pk=land.pk %}" class="btn btn-outline-warning" title="Bearbeiten">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="{% url 'stiftung:land_verpachtung_end' land_pk=land.pk %}" class="btn btn-outline-danger" title="Beenden">
                                                    <i class="fas fa-stop"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-handshake fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">Keine Verpachtungen</h5>
                            <p class="text-muted">Erstellen Sie eine neue Verpachtung für diese Länderei.</p>
                            <a href="{% url 'stiftung:land_verpachtung_create' land_pk=land.pk %}" class="btn btn-success">
                                <i class="fas fa-plus me-2"></i>Erste Verpachtung erstellen
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Right Column - Statistics and Actions -->
        <div class="col-lg-4">
            <!-- Quick Actions -->
            <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-plus-circle me-2"></i>Schnellzugriff
                    </h6>
                </div>
                <div class="card-body">
                    <div class="d-grid gap-2">
                        <a href="{% url 'stiftung:land_verpachtung_create' land_pk=land.pk %}" class="btn btn-info">
                            <i class="fas fa-handshake me-2"></i>Neue Verpachtung
                        </a>
                        <a href="{% url 'stiftung:land_update' land.pk %}" class="btn btn-warning">
                            <i class="fas fa-edit me-2"></i>Länderei bearbeiten
                        </a>
                        <a href="{% url 'stiftung:land_list' %}" class="btn btn-outline-secondary">
                            <i class="fas fa-list me-2"></i>Alle Ländereien
                        </a>
                    </div>
                </div>
            </div>

            <!-- System Information -->
            <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-cog me-2"></i>Systeminformationen
                    </h6>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-borderless">
                        <tr>
                            <td><small class="text-muted">Erstellt:</small></td>
                            <td><small class="text-muted">{{ land.erstellt_am|date:"d.m.Y H:i" }}</small></td>
                        </tr>
                        <tr>
                            <td><small class="text-muted">Aktualisiert:</small></td>
                            <td><small class="text-muted">{{ land.aktualisiert_am|date:"d.m.Y H:i" }}</small></td>
                        </tr>
                        <tr>
                            <td><small class="text-muted">ID:</small></td>
                            <td><small class="text-muted font-monospace">{{ land.id }}</small></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>


{% endblock %} 
