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

{% block title %}{{ paechter.get_full_name }} - Pächter - Stiftungsverwaltung{% endblock %}

{% block content %}

    <div class="row">
        <div class="col-12">
            <!-- Header -->
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3">
                    <i class="fas fa-user-tie text-primary me-2"></i>
                    Pächter: {{ paechter.get_full_name }}
                </h1>
                <div>
                    <a href="{% url 'stiftung:paechter_update' pk=paechter.pk %}" class="btn btn-warning me-2">
                        <i class="fas fa-edit me-2"></i>Bearbeiten
                    </a>
                    <a href="{% url 'stiftung:paechter_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-2"></i>Zurück zur Liste
                    </a>
                </div>
            </div>

            <div class="row">
                <!-- Main Content -->
                <div class="col-lg-8">
                    <!-- Persönliche Informationen -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-primary text-white">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-user me-2"></i>Persönliche Informationen
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>Vorname:</strong> {{ paechter.vorname }}</p>
                                    <p><strong>Nachname:</strong> {{ paechter.nachname }}</p>
                                    {% if paechter.geburtsdatum %}
                                        <p><strong>Geburtsdatum:</strong> {{ paechter.geburtsdatum|date:"d.m.Y" }}</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Status:</strong> 
                                        {% if paechter.aktiv %}
                                            <span class="badge bg-success">Aktiv</span>
                                        {% else %}
                                            <span class="badge bg-secondary">Inaktiv</span>
                                        {% endif %}
                                    </p>
                                    {% if paechter.pachtnummer %}
                                        <p><strong>Pachtnummer:</strong> 
                                            <span class="badge bg-secondary">{{ paechter.pachtnummer }}</span>
                                        </p>
                                    {% endif %}
                                    <p><strong>Personentyp:</strong> 
                                        {% if paechter.personentyp == 'natuerlich' %}
                                            <span class="badge bg-primary">Natürliche Person</span>
                                        {% elif paechter.personentyp == 'gesellschaft' %}
                                            <span class="badge bg-info">Gesellschaft</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{{ paechter.get_personentyp_display }}</span>
                                        {% endif %}
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Kontaktinformationen -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-info text-white">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-address-book me-2"></i>Kontaktinformationen
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    {% if paechter.email %}
                                        <p><strong>E-Mail:</strong> <a href="mailto:{{ paechter.email }}">{{ paechter.email }}</a></p>
                                    {% endif %}
                                    {% if paechter.telefon %}
                                        <p><strong>Telefon:</strong> {{ paechter.telefon }}</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    {% if paechter.iban %}
                                        <p><strong>IBAN:</strong> {{ paechter.iban }}</p>
                                    {% endif %}
                                </div>
                            </div>
                            {% if paechter.strasse or paechter.plz or paechter.ort %}
                                <p><strong>Adresse:</strong><br>
                                    {% if paechter.strasse %}{{ paechter.strasse }}{% endif %}
                                    {% if paechter.plz or paechter.ort %}<br>{% endif %}
                                    {% if paechter.plz %}{{ paechter.plz }}{% endif %}
                                    {% if paechter.plz and paechter.ort %} {% endif %}
                                    {% if paechter.ort %}{{ paechter.ort }}{% endif %}
                                </p>
                            {% endif %}
                        </div>
                    </div>

                    <!-- Pacht-spezifische Informationen -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-success text-white">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-tractor me-2"></i>Pacht-spezifische Informationen
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    {% if paechter.pachtbeginn_erste %}
                                        <p><strong>Erster Pachtbeginn:</strong> {{ paechter.pachtbeginn_erste|date:"d.m.Y" }}</p>
                                    {% endif %}
                                    {% if paechter.pachtende_letzte %}
                                        <p><strong>Letztes Pachtende:</strong> {{ paechter.pachtende_letzte|date:"d.m.Y" }}</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    {% if paechter.pachtzins_aktuell %}
                                        <p><strong>Aktueller Pachtzins:</strong> <span class="text-success fw-bold">€{{ paechter.pachtzins_aktuell|floatformat:2 }}/Jahr</span></p>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Landwirtschaftliche Qualifikationen -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-warning text-dark">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-graduation-cap me-2"></i>Landwirtschaftliche Qualifikationen
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>Landwirtschaftliche Ausbildung:</strong> 
                                        {% if paechter.landwirtschaftliche_ausbildung %}
                                            <span class="badge bg-success">
                                                <i class="fas fa-check me-1"></i>Ja
                                            </span>
                                        {% else %}
                                            <span class="badge bg-warning">
                                                <i class="fas fa-times me-1"></i>Nein
                                            </span>
                                        {% endif %}
                                    </p>
                                    {% if paechter.berufserfahrung_jahre %}
                                        <p><strong>Berufserfahrung:</strong> {{ paechter.berufserfahrung_jahre }} Jahre</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    {% if paechter.spezialisierung %}
                                        <p><strong>Spezialisierung:</strong> {{ paechter.spezialisierung }}</p>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Gepachtete Ländereien (Neue Struktur) -->
                    {% if gepachtete_laendereien %}
                        <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 me-2"></i>Gepachtete Ländereien ({{ gepachtete_laendereien.count }})
                                </h5>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-hover">
                                        <thead class="table-light">
                                            <tr>
                                                <th>Länderei</th>
                                                <th>Pachtzeit</th>
                                                <th>Fläche</th>
                                                <th>Pachtzins/Jahr</th>
                                                <th>USt-Option</th>
                                                <th>Zahlungsweise</th>
                                                <th>Aktionen</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for land in gepachtete_laendereien %}
                                            <tr>
                                                <td>
                                                    <a href="{% url 'stiftung:land_detail' pk=land.pk %}">
                                                        <strong>{{ land }}</strong>
                                                    </a>
                                                    {% if land.adresse %}
                                                    <br><small class="text-muted">{{ land.adresse }}</small>
                                                    {% endif %}
                                                </td>
                                                <td>
                                                    {{ land.pachtbeginn|date:"d.m.Y"|default:"Nicht angegeben" }}
                                                    {% if land.pachtende %} - {{ land.pachtende|date:"d.m.Y" }}{% else %} - <span class="text-muted">Unbefristet</span>{% endif %}
                                                    {% if land.verlaengerung_klausel %}
                                                    <br><small class="badge bg-info">Auto-Verlängerung</small>
                                                    {% endif %}
                                                </td>
                                                <td>
                                                    {{ land.verp_flaeche_aktuell|floatformat:2 }} qm
                                                    <br><small class="text-muted">({{ land.verp_flaeche_aktuell_hektar|floatformat:2 }} ha)</small>
                                                </td>
                                                <td>
                                                    {% if land.pachtzins_pauschal %}
                                                        <strong>€{{ land.pachtzins_pauschal|floatformat:2 }}</strong>
                                                        {% if land.pachtzins_pro_ha %}
                                                        <br><small class="text-muted">€{{ land.pachtzins_pro_ha|floatformat:2 }}/ha</small>
                                                        {% endif %}
                                                    {% else %}
                                                        <span class="text-muted">Nicht angegeben</span>
                                                    {% endif %}
                                                </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>
                                                <td>{{ land.get_zahlungsweise_display }}</td>
                                                <td>
                                                    <div class="btn-group" role="group">
                                                        <a href="{% url 'stiftung:land_detail' pk=land.pk %}" 
                                                           class="btn btn-sm btn-outline-primary" title="Länderei anzeigen">
                                                            <i class="fas fa-eye"></i>
                                                        </a>
                                                        <a href="{% url 'stiftung:land_update' pk=land.pk %}" 
                                                           class="btn btn-sm btn-outline-warning" title="Bearbeiten">
                                                            <i class="fas fa-edit"></i>
                                                        </a>
                                                    </div>
                                                </td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                                
                                <!-- Zusammenfassung -->
                                <div class="row mt-3">
                                    <div class="col-md-4 text-center">
                                        <div class="border rounded p-3">
                                            <h5 class="text-success">{{ total_flaeche_neu|floatformat:0 }} qm</h5>
                                            <p class="text-muted mb-0">Gesamtfläche</p>
                                            <small class="text-muted">({{ total_flaeche_neu_hektar|floatformat:2 }} ha)</small>
                                        </div>
                                    </div>
                                    <div class="col-md-4 text-center">
                                        <div class="border rounded p-3">
                                            <h5 class="text-primary">€{{ total_pachtzins_neu|floatformat:0 }}</h5>
                                            <p class="text-muted mb-0">Pachtzins/Jahr</p>
                                        </div>
                                    </div>
                                    <div class="col-md-4 text-center">
                                        <div class="border rounded p-3">
                                            <h5 class="text-info">{{ gepachtete_laendereien.count }}</h5>
                                            <p class="text-muted mb-0">Ländereien</p>
                                        </div>
                                    </div>
                                </div>
                                
                                <!-- Link zu Abrechnungen -->
                                <div class="text-center mt-3">
                                    <a href="{% url 'stiftung:land_abrechnung_list' %}?paechter={{ paechter.pk }}" class="btn btn-outline-success">
                                        <i class="fas fa-calculator me-2"></i>Abrechnungen anzeigen
                                    </a>
                                </div>
                            </div>
                        </div>
                    {% endif %}

                    <!-- Legacy Verpachtungen entfernt für saubere UI -->

                    <!-- Dokumente (DMS) -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-success text-white d-flex justify-content-between align-items-center">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-file-alt me-2"></i>Dokumente
                            </h5>
                            <a href="{% url 'stiftung:dms_upload' %}?paechter={{ paechter.pk }}" class="btn btn-light btn-sm">
                                <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">
                                        <thead>
                                            <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>
                                                    {% 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>
                                                    {% 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="{% 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 %}
                                <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 vorhanden</h5>
                                    <p class="text-muted">Laden Sie Dokumente direkt hoch und verknüpfen Sie sie mit diesem Pächter.</p>
                                    <a href="{% url 'stiftung:dms_upload' %}?paechter={{ paechter.pk }}" class="btn btn-success">
                                        <i class="fas fa-upload me-2"></i>Erstes Dokument hochladen
                                    </a>
                                </div>
                            {% endif %}
                        </div>
                    </div>

                    <!-- Notizen -->
                    {% if paechter.notizen %}
                        <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>Notizen
                                </h5>
                            </div>
                            <div class="card-body">
                                {{ paechter.notizen|linebreaks }}
                            </div>
                        </div>
                    {% endif %}
                </div>

                <!-- Sidebar -->
                <div class="col-lg-4">
                    <!-- Statistiken -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-primary text-white">
                            <h6 class="card-title mb-0">
                                <i class="fas fa-chart-bar me-2"></i>Statistiken
                            </h6>
                        </div>
                        <div class="card-body">
                            <div class="text-center">
                                <div class="row">
                                    <div class="col-6">
                                        <div class="border-end">
                                            <h4 class="text-primary">{{ verpachtungen.count }}</h4>
                                            <small class="text-muted">Verpachtungen</small>
                                        </div>
                                    </div>
                                    <div class="col-6">
                                        <h4 class="text-success">
                                            {% if verpachtungen %}
                                                {% with total_flaeche=verpachtungen|length %}
                                                    {{ total_flaeche|floatformat:0 }}
                                                {% endwith %}
                                            {% else %}
                                                0
                                            {% endif %}
                                        </h4>
                                        <small class="text-muted">Aktive Verträge</small>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Aktionen -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-success text-white">
                            <h6 class="card-title mb-0">
                                <i class="fas fa-tools me-2"></i>Aktionen
                            </h6>
                        </div>
                        <div class="card-body">
                            <div class="d-grid gap-2">
                                <a href="{% url 'stiftung:paechter_update' pk=paechter.pk %}" class="btn btn-warning">
                                    <i class="fas fa-edit me-2"></i>Bearbeiten
                                </a>
                                <a href="{% url 'stiftung:paechter_export' pk=paechter.pk %}" class="btn btn-success">
                                    <i class="fas fa-download me-2"></i>Export
                                </a>
                                <a href="{% url 'stiftung:paechter_delete' pk=paechter.pk %}" class="btn btn-danger">
                                    <i class="fas fa-trash me-2"></i>Löschen
                                </a>
                            </div>
                        </div>
                    </div>

                    <!-- Informationen -->
                    <div class="card shadow">
                        <div class="card-header bg-info text-white">
                            <h6 class="card-title mb-0">
                                <i class="fas fa-info-circle me-2"></i>Informationen
                            </h6>
                        </div>
                        <div class="card-body">
                            <p><strong>ID:</strong> {{ paechter.id }}</p>
                            <p><strong>Erstellt:</strong> {{ paechter.id|slice:":8" }}</p>
                            {% if paechter.aktiv %}
                                <p><strong>Status:</strong> <span class="badge bg-success">Aktiv</span></p>
                            {% else %}
                                <p><strong>Status:</strong> <span class="badge bg-secondary">Inaktiv</span></p>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %} 
