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

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

{% block content %}

    <div class="row">
        <div class="col-lg-10 mx-auto">
            <!-- Header -->
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3">
                    <i class="fas fa-handshake text-primary me-2"></i>
                    Verpachtung: {{ verpachtung.vertragsnummer }}
                </h1>
                <div class="btn-group" role="group">
                    <a href="{% url 'stiftung:verpachtung_update' verpachtung.pk %}" class="btn btn-warning">
                        <i class="fas fa-edit me-1"></i>Bearbeiten
                    </a>
                    <a href="{% url 'stiftung:verpachtung_delete' verpachtung.pk %}" class="btn btn-danger">
                        <i class="fas fa-trash me-1"></i>Löschen
                    </a>
                </div>
            </div>

            <!-- Status Badge -->
            <div class="row mb-4">
                <div class="col-12">
                    {% if verpachtung.status == 'aktiv' %}
                        {% if verpachtung.is_aktiv %}
                            <span class="badge bg-success fs-6 me-2">
                                <i class="fas fa-check me-1"></i>Aktiv
                            </span>
                        {% else %}
                            <span class="badge bg-warning fs-6 me-2">
                                <i class="fas fa-clock me-1"></i>Inaktiv
                            </span>
                        {% endif %}
                    {% elif verpachtung.status == 'beendet' %}
                        <span class="badge bg-secondary fs-6 me-2">
                            <i class="fas fa-stop me-1"></i>Beendet
                        </span>
                    {% elif verpachtung.status == 'gekuendigt' %}
                        <span class="badge bg-danger fs-6 me-2">
                            <i class="fas fa-times me-1"></i>Gekündigt
                        </span>
                    {% elif verpachtung.status == 'verlaengert' %}
                        <span class="badge bg-info fs-6 me-2">
                            <i class="fas fa-refresh me-1"></i>Verlängert
                        </span>
                    {% endif %}

                    {% if verpachtung.verlaengerung_klausel %}
                        <span class="badge bg-info fs-6 me-2">
                            <i class="fas fa-sync me-1"></i>Auto-Verlängerung
                        </span>
                    {% endif %}

                    {% if verpachtung.ust_option %}
                        <span class="badge bg-warning fs-6">
                            <i class="fas fa-percent me-1"></i>USt-Option
                        </span>
                    {% endif %}
                </div>
            </div>

            <div class="row">
                <!-- Grunddaten -->
                <div class="col-lg-6">
                    <div class="card shadow mb-4">
                        <div class="card-header bg-primary text-white">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-info-circle me-2"></i>Grunddaten
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>Vertragsnummer:</strong><br>{{ verpachtung.vertragsnummer }}</p>
                                    <p><strong>Status:</strong><br>{{ verpachtung.get_status_display }}</p>
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Erstellt:</strong><br>{{ verpachtung.erstellt_am|date:"d.m.Y H:i" }}</p>
                                    <p><strong>Aktualisiert:</strong><br>{{ verpachtung.aktualisiert_am|date:"d.m.Y H:i" }}</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Land -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-success text-white">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-map-marked-alt me-2"></i>Länderei
                            </h5>
                        </div>
                        <div class="card-body">
                            <h6 class="text-primary">
                                <a href="{% url 'stiftung:land_detail' verpachtung.land.pk %}" 
                                   class="text-decoration-none">
                                    {{ verpachtung.land.bezeichnung }}
                                </a>
                            </h6>
                            <div class="row">
                                <div class="col-md-6">
                                    {% if verpachtung.land.flur %}
                                        <p><strong>Flur:</strong> {{ verpachtung.land.flur }}</p>
                                    {% endif %}
                                    {% if verpachtung.land.flurstueck %}
                                        <p><strong>Flurstück:</strong> {{ verpachtung.land.flurstueck }}</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Gesamtfläche:</strong><br>{{ verpachtung.land.groesse_qm|floatformat:0 }} qm ({{ verpachtung.land.groesse_hektar|floatformat:2 }} ha)</p>
                                </div>
                            </div>
                            {% if verpachtung.land.lage %}
                                <p><strong>Lage:</strong><br>{{ verpachtung.land.lage }}</p>
                            {% endif %}
                        </div>
                    </div>
                </div>

                <!-- Pächter -->
                <div class="col-lg-6">
                    <div class="card shadow mb-4">
                        <div class="card-header bg-info text-white">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-user me-2"></i>Pächter
                            </h5>
                        </div>
                        <div class="card-body">
                            <h6 class="text-primary">
                                <a href="{% url 'stiftung:paechter_detail' verpachtung.paechter.pk %}" 
                                   class="text-decoration-none">
                                    {{ verpachtung.paechter.get_full_name }}
                                </a>
                            </h6>
                            <div class="row">
                                <div class="col-md-6">
                                    {% if verpachtung.paechter.email %}
                                        <p><strong>E-Mail:</strong><br>
                                            <a href="mailto:{{ verpachtung.paechter.email }}">{{ verpachtung.paechter.email }}</a>
                                        </p>
                                    {% endif %}
                                    {% if verpachtung.paechter.telefon %}
                                        <p><strong>Telefon:</strong><br>{{ verpachtung.paechter.telefon }}</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    {% if verpachtung.paechter.strasse or verpachtung.paechter.plz or verpachtung.paechter.ort %}
                                        <p><strong>Adresse:</strong><br>
                                            {% if verpachtung.paechter.strasse %}{{ verpachtung.paechter.strasse }}<br>{% endif %}
                                            {% if verpachtung.paechter.plz %}{{ verpachtung.paechter.plz }} {% endif %}{{ verpachtung.paechter.ort }}
                                        </p>
                                    {% endif %}
                                </div>
                            </div>
                            <p><strong>Typ:</strong> {{ verpachtung.paechter.get_personentyp_display }}</p>
                        </div>
                    </div>

                    <!-- Pachtzeit -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-warning text-dark">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-calendar me-2"></i>Pachtzeit
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>Pachtbeginn:</strong><br>{{ verpachtung.pachtbeginn|date:"d.m.Y" }}</p>
                                    {% if verpachtung.pachtende %}
                                        <p><strong>Pachtende:</strong><br>{{ verpachtung.pachtende|date:"d.m.Y" }}</p>
                                    {% else %}
                                        <p><strong>Pachtende:</strong><br><span class="text-success">Unbefristet</span></p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Auto-Verlängerung:</strong><br>
                                        {% if verpachtung.verlaengerung_klausel %}
                                            <span class="badge bg-success">Ja</span>
                                        {% else %}
                                            <span class="badge bg-secondary">Nein</span>
                                        {% endif %}
                                    </p>
                                    {% if verpachtung.get_restlaufzeit_tage %}
                                        <p><strong>Restlaufzeit:</strong><br>{{ verpachtung.get_restlaufzeit_tage }} Tage</p>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Finanzielle Details -->
            <div class="row">
                <div class="col-lg-6">
                    <div class="card shadow mb-4">
                        <div class="card-header bg-secondary text-white">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-euro-sign me-2"></i>Pachtzins
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>Pachtzins pauschal:</strong><br>
                                        <span class="text-success fw-bold fs-5">€{{ verpachtung.pachtzins_pauschal|floatformat:2 }}</span> / Jahr
                                    </p>
                                    {% if verpachtung.pachtzins_pro_ha %}
                                        <p><strong>Pachtzins pro ha:</strong><br>€{{ verpachtung.pachtzins_pro_ha|floatformat:2 }} / ha</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Zahlungsweise:</strong><br>{{ verpachtung.get_zahlungsweise_display }}</p>
                                    <p><strong>Verpachtete Fläche:</strong><br>
                                        {{ verpachtung.verpachtete_flaeche|floatformat:0 }} qm<br>
                                        <small class="text-muted">({{ verpachtung.verpachtete_flaeche_hektar|floatformat:2 }} ha)</small>
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-lg-6">
                    <div class="card shadow mb-4">
                        <div class="card-header bg-warning text-dark">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-percent me-2"></i>Steuern & Umlagen
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>USt-Option:</strong><br>
                                        {% if verpachtung.ust_option %}
                                            <span class="badge bg-warning">Ja ({{ verpachtung.ust_satz }}%)</span>
                                            {% if verpachtung.ust_pacht_betrag %}
                                                <br><small class="text-muted">USt-Betrag: €{{ verpachtung.ust_pacht_betrag|floatformat:2 }}</small>
                                            {% endif %}
                                        {% else %}
                                            <span class="badge bg-secondary">Nein</span>
                                        {% endif %}
                                    </p>
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Umlagefähig:</strong></p>
                                    <ul class="list-unstyled">
                                        <li>
                                            <i class="fas fa-{% if verpachtung.grundsteuer_umlage %}check text-success{% else %}times text-danger{% endif %} me-1"></i>
                                            Grundsteuer
                                        </li>
                                        <li>
                                            <i class="fas fa-{% if verpachtung.versicherungen_umlage %}check text-success{% else %}times text-danger{% endif %} me-1"></i>
                                            Versicherungen
                                        </li>
                                        <li>
                                            <i class="fas fa-{% if verpachtung.verbandsbeitraege_umlage %}check text-success{% else %}times text-danger{% endif %} me-1"></i>
                                            Verbandsbeiträge
                                        </li>
                                        <li>
                                            <i class="fas fa-{% if verpachtung.jagdpacht_anteil_umlage %}check text-success{% else %}times text-danger{% endif %} me-1"></i>
                                            Jagdpachtanteile
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Bemerkungen -->
            {% if verpachtung.bemerkungen %}
            <div class="row">
                <div class="col-12">
                    <div class="card shadow mb-4">
                        <div class="card-header bg-light">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-sticky-note me-2"></i>Bemerkungen
                            </h5>
                        </div>
                        <div class="card-body">
                            <p>{{ verpachtung.bemerkungen|linebreaks }}</p>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- Dokumente (DMS) -->
            <div class="row mb-4">
                <div class="col-12">
                    <div class="card shadow">
                        <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-file-alt me-2"></i>Dokumente{% if verknuepfte_dokumente %} ({{ verknuepfte_dokumente.count }}){% endif %}
                            </h6>
                            <a href="{% url 'stiftung:dms_upload' %}?verpachtung={{ verpachtung.pk }}"
                               class="btn btn-sm btn-success">
                                <i class="fas fa-upload me-1"></i>Dokument hochladen
                            </a>
                        </div>
                        <div class="card-body">
                            {% if verknuepfte_dokumente %}
                                <div class="table-responsive">
                                    <table class="table table-hover table-sm align-middle">
                                        <thead class="table-light">
                                            <tr>
                                                <th>Titel</th>
                                                <th>Kontext</th>
                                                <th>Aktionen</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for dokument in verknuepfte_dokumente %}
                                            <tr>
                                                <td>
                                                    <strong>{{ dokument.titel }}</strong>
                                                    {% if dokument.dateiname_original %}<br><small class="text-muted">{{ dokument.dateiname_original }} ({{ dokument.get_human_size }})</small>{% endif %}
                                                </td>
                                                <td><span class="badge bg-secondary">{{ dokument.get_kontext_display }}</span></td>
                                                <td>
                                                    <div class="btn-group" role="group">
                                                        <a href="{% url 'stiftung:dms_download' dokument.pk %}" class="btn btn-sm btn-outline-primary" title="Herunterladen">
                                                            <i class="fas fa-download"></i>
                                                        </a>
                                                        <a href="{% url 'stiftung:dms_edit' dokument.pk %}" class="btn btn-sm btn-outline-warning" title="Bearbeiten">
                                                            <i class="fas fa-edit"></i>
                                                        </a>
                                                        <a href="{% url 'stiftung:dms_delete' dokument.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>
                            {% else %}
                                <p class="text-muted mb-0">
                                    <i class="fas fa-info-circle me-2"></i>
                                    Noch keine Dokumente vorhanden.
                                </p>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>

            <!-- Navigation -->
            <div class="row">
                <div class="col-12">
                    <div class="d-flex justify-content-between">
                        <a href="{% url 'stiftung:verpachtung_list' %}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-2"></i>Zurück zur Liste
                        </a>
                        <a href="{% url 'stiftung:verpachtung_update' verpachtung.pk %}" class="btn btn-primary">
                            <i class="fas fa-edit me-2"></i>Bearbeiten
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}