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

{% block title %}Verpachtung {{ landverpachtung.vertragsnummer }} - Stiftung{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1><i class="fas fa-handshake me-2"></i>Verpachtung {{ landverpachtung.vertragsnummer }}</h1>
    <div>
        <a href="{% url 'stiftung:land_verpachtung_update' landverpachtung.pk %}" class="btn btn-warning">
            <i class="fas fa-edit me-1"></i>Bearbeiten
        </a>
        {% if landverpachtung.status == 'aktiv' %}
        <a href="{% url 'stiftung:land_verpachtung_end_direct' landverpachtung.pk %}" class="btn btn-danger">
            <i class="fas fa-stop me-1"></i>Beenden
        </a>
        {% endif %}
        <a href="{% url 'stiftung:land_detail' landverpachtung.land.pk %}" class="btn btn-secondary">
            <i class="fas fa-arrow-left me-1"></i>Zur Länderei
        </a>
    </div>
</div>

<div class="row">
    <!-- Grunddaten -->
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-info-circle me-2"></i>Grunddaten</h5>
            </div>
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Vertragsnummer:</strong></div>
                    <div class="col-sm-8">{{ landverpachtung.vertragsnummer }}</div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Status:</strong></div>
                    <div class="col-sm-8">
                        {% if landverpachtung.status == 'aktiv' %}
                            <span class="badge bg-success">{{ landverpachtung.get_status_display }}</span>
                        {% elif landverpachtung.status == 'beendet' %}
                            <span class="badge bg-secondary">{{ landverpachtung.get_status_display }}</span>
                        {% else %}
                            <span class="badge bg-warning">{{ landverpachtung.get_status_display }}</span>
                        {% endif %}
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Länderei:</strong></div>
                    <div class="col-sm-8">
                        <a href="{% url 'stiftung:land_detail' landverpachtung.land.pk %}">
                            {{ landverpachtung.land.gemarkung }} - {{ landverpachtung.land.gemeinde }}, 
                            Flur {{ landverpachtung.land.flur }}, Flurstück {{ landverpachtung.land.flurstueck|default:"N/A" }}
                        </a>
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Pächter:</strong></div>
                    <div class="col-sm-8">
                        <a href="{% url 'stiftung:paechter_detail' landverpachtung.paechter.pk %}">
                            {{ landverpachtung.paechter.get_full_name }}
                        </a>
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Verpachtete Fläche:</strong></div>
                    <div class="col-sm-8">{{ landverpachtung.verpachtete_flaeche|floatformat:0 }} m² ({{ landverpachtung.verpachtete_flaeche_hektar }} ha)</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Vertragsdaten -->
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-calendar-alt me-2"></i>Vertragsdaten</h5>
            </div>
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Pachtbeginn:</strong></div>
                    <div class="col-sm-8">{{ landverpachtung.pachtbeginn|date:"d.m.Y" }}</div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Pachtende:</strong></div>
                    <div class="col-sm-8">{{ landverpachtung.pachtende|date:"d.m.Y"|default:"Unbefristet" }}</div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Verlängerung:</strong></div>
                    <div class="col-sm-8">
                        {% if landverpachtung.verlaengerung_klausel %}
                            <i class="fas fa-check text-success me-1"></i>Automatische Verlängerung
                        {% else %}
                            <i class="fas fa-times text-danger me-1"></i>Keine automatische Verlängerung
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- Finanzielle Daten -->
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-euro-sign me-2"></i>Finanzielle Daten</h5>
            </div>
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Pachtzins pauschal:</strong></div>
                    <div class="col-sm-8">{{ landverpachtung.pachtzins_pauschal|floatformat:2 }} € / Jahr</div>
                </div>
                {% if landverpachtung.pachtzins_pro_ha %}
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Pachtzins pro ha:</strong></div>
                    <div class="col-sm-8">{{ landverpachtung.pachtzins_pro_ha|floatformat:2 }} € / ha</div>
                </div>
                {% endif %}
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>Zahlungsweise:</strong></div>
                    <div class="col-sm-8">{{ landverpachtung.get_zahlungsweise_display }}</div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-4"><strong>USt-Option:</strong></div>
                    <div class="col-sm-8">
                        {% if landverpachtung.ust_option %}
                            <i class="fas fa-check text-success me-1"></i>Ja ({{ landverpachtung.ust_satz }}%)
                        {% else %}
                            <i class="fas fa-times text-danger me-1"></i>Nein
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Umlagen -->
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-calculator me-2"></i>Umlagen</h5>
            </div>
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-sm-6"><strong>Grundsteuer:</strong></div>
                    <div class="col-sm-6">
                        {% if landverpachtung.grundsteuer_umlage %}
                            <i class="fas fa-check text-success me-1"></i>Umlagefähig
                        {% else %}
                            <i class="fas fa-times text-danger me-1"></i>Nicht umlagefähig
                        {% endif %}
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-6"><strong>Versicherungen:</strong></div>
                    <div class="col-sm-6">
                        {% if landverpachtung.versicherungen_umlage %}
                            <i class="fas fa-check text-success me-1"></i>Umlagefähig
                        {% else %}
                            <i class="fas fa-times text-danger me-1"></i>Nicht umlagefähig
                        {% endif %}
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-6"><strong>Verbandsbeiträge:</strong></div>
                    <div class="col-sm-6">
                        {% if landverpachtung.verbandsbeitraege_umlage %}
                            <i class="fas fa-check text-success me-1"></i>Umlagefähig
                        {% else %}
                            <i class="fas fa-times text-danger me-1"></i>Nicht umlagefähig
                        {% endif %}
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-sm-6"><strong>Jagdpachtanteile:</strong></div>
                    <div class="col-sm-6">
                        {% if landverpachtung.jagdpacht_anteil_umlage %}
                            <i class="fas fa-check text-success me-1"></i>Umlagefähig
                        {% else %}
                            <i class="fas fa-times text-danger me-1"></i>Nicht umlagefähig
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Verknüpfte Dokumente -->
<div class="card mb-4">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0"><i class="fas fa-folder-open me-2"></i>Verknüpfte Dokumente</h5>
        <a href="/dokumente/verwaltung/" class="btn btn-sm btn-outline-primary">
            <i class="fas fa-link me-1"></i>Dokument verknüpfen
        </a>
    </div>
    <div class="card-body">
        {% if verknuepfte_dokumente %}
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>Titel</th>
                            <th>Kontext</th>
                            <th>Aktionen</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for doc in verknuepfte_dokumente %}
                        <tr>
                            <td>
                                <strong>{{ doc.titel|default:"Ohne Titel" }}</strong>
                                <br>
                                <small class="text-muted">Paperless-ID: {{ doc.paperless_document_id }}</small>
                            </td>
                            <td>{{ doc.get_kontext_display }}</td>
                            <td>
                                <a href="{{ doc.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>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        {% else %}
            <p class="text-muted">Keine Dokumente verknüpft.</p>
        {% endif %}
    </div>
</div>

<!-- Bemerkungen -->
{% if landverpachtung.bemerkungen %}
<div class="card">
    <div class="card-header">
        <h5 class="mb-0"><i class="fas fa-sticky-note me-2"></i>Bemerkungen</h5>
    </div>
    <div class="card-body">
        <p class="mb-0">{{ landverpachtung.bemerkungen|linebreaks }}</p>
    </div>
</div>
{% endif %}

{% endblock %}
