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

{% block title %}{{ destinataer.get_full_name }} - Destinatär - 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-users text-primary me-2"></i>
                    Destinatär: {{ destinataer.get_full_name }}
                </h1>
                <div>
                    <a href="{% url 'stiftung:foerderung_create' %}?destinataer={{ destinataer.pk }}" class="btn btn-success me-2">
                        <i class="fas fa-gift me-2"></i>Neue Förderung
                    </a>
                    <a href="{% url 'stiftung:destinataer_update' pk=destinataer.pk %}" class="btn btn-warning me-2">
                        <i class="fas fa-edit me-2"></i>Bearbeiten
                    </a>
                    <a href="{% url 'stiftung:destinataer_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> {{ destinataer.vorname }}</p>
                                    <p><strong>Nachname:</strong> {{ destinataer.nachname }}</p>
                                    {% if destinataer.geburtsdatum %}
                                        <p><strong>Geburtsdatum:</strong> {{ destinataer.geburtsdatum|date:"d.m.Y" }}</p>
                                    {% endif %}
                                    <p><strong>Familienzweig:</strong> 
                                        <span class="badge bg-info">{{ destinataer.get_familienzweig_display }}</span>
                                    </p>
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Status:</strong> 
                                        {% if destinataer.aktiv %}
                                            <span class="badge bg-success">Aktiv</span>
                                        {% else %}
                                            <span class="badge bg-secondary">Inaktiv</span>
                                        {% endif %}
                                    </p>
                                    <p><strong>Berufsgruppe:</strong> 
                                        <span class="badge bg-secondary">{{ destinataer.get_berufsgruppe_display }}</span>
                                    </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 destinataer.email %}
                                        <p><strong>E-Mail:</strong> <a href="mailto:{{ destinataer.email }}">{{ destinataer.email }}</a></p>
                                    {% endif %}
                                    {% if destinataer.telefon %}
                                        <p><strong>Telefon:</strong> {{ destinataer.telefon }}</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    {% if destinataer.iban %}
                                        <p><strong>IBAN:</strong> {{ destinataer.iban }}</p>
                                    {% endif %}
                                </div>
                            </div>
                            {% if destinataer.strasse or destinataer.plz or destinataer.ort %}
                                <p><strong>Adresse:</strong><br>
                                    {% if destinataer.strasse %}{{ destinataer.strasse }}{% endif %}
                                    {% if destinataer.plz or destinataer.ort %}<br>{% endif %}
                                    {% if destinataer.plz %}{{ destinataer.plz }}{% endif %}
                                    {% if destinataer.plz and destinataer.ort %} {% endif %}
                                    {% if destinataer.ort %}{{ destinataer.ort }}{% endif %}
                                </p>
                            {% endif %}
                        </div>
                    </div>

                    <!-- Berufliche 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-briefcase me-2"></i>Berufliche Informationen
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    {% if destinataer.ausbildungsstand %}
                                        <p><strong>Ausbildungsstand:</strong> {{ destinataer.ausbildungsstand }}</p>
                                    {% endif %}
                                    {% if destinataer.institution %}
                                        <p><strong>Institution/Organisation:</strong> {{ destinataer.institution }}</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    {% if destinataer.projekt_beschreibung %}
                                        <p><strong>Projektbeschreibung:</strong><br>{{ destinataer.projekt_beschreibung|linebreaks }}</p>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Finanzielle Informationen -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-warning text-dark">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-euro-sign me-2"></i>Finanzielle Informationen
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    {% if destinataer.jaehrliches_einkommen %}
                                        <p><strong>Jährliches Einkommen:</strong> <span class="text-primary fw-bold">€{{ destinataer.jaehrliches_einkommen|floatformat:2 }}</span></p>
                                    {% endif %}
                                    {% if destinataer.monatliche_bezuege %}
                                        <p><strong>Monatliche Bezüge:</strong> €{{ destinataer.monatliche_bezuege|floatformat:2 }}</p>
                                    {% endif %}
                                    {% if destinataer.vermoegen %}
                                        <p><strong>Vermögen:</strong> €{{ destinataer.vermoegen|floatformat:2 }}</p>
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Finanzielle Notlage:</strong> 
                                        {% if destinataer.finanzielle_notlage %}
                                            <span class="badge bg-danger">
                                                <i class="fas fa-exclamation-triangle me-1"></i>Ja
                                            </span>
                                        {% else %}
                                            <span class="badge bg-success">
                                                <i class="fas fa-check me-1"></i>Nein
                                            </span>
                                        {% endif %}
                                    </p>
                                    {% if destinataer.vierteljaehrlicher_betrag %}
                                        <p><strong>Vierteljährlicher Betrag:</strong> <span class="fw-bold">€{{ destinataer.vierteljaehrlicher_betrag|floatformat:2 }}</span></p>
                                    {% endif %}
                                    <p><strong>Unterstützung bestätigt:</strong>
                                        {% if destinataer.unterstuetzung_bestaetigt %}
                                            <span class="badge bg-success">Ja</span>
                                        {% else %}
                                            <span class="badge bg-secondary">Nein</span>
                                        {% endif %}
                                    </p>
                                    {% if destinataer.standard_konto %}
                                        <p><strong>Standardkonto:</strong> {{ destinataer.standard_konto }}</p>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Studiennachweis / Voraussetzungen -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-light">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-graduation-cap me-2"></i>Studiennachweis & Voraussetzungen
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>Abkömmling gem. Satzung:</strong>
                                        {% if destinataer.ist_abkoemmling %}<span class="badge bg-success">Ja</span>{% else %}<span class="badge bg-secondary">Nein</span>{% endif %}
                                    </p>
                                    <p><strong>Haushaltsgröße:</strong> {{ destinataer.haushaltsgroesse }}</p>
                                </div>
                                <div class="col-md-6">
                                    <p><strong>Studiennachweis erforderlich:</strong>
                                        {% if destinataer.studiennachweis_erforderlich %}<span class="badge bg-info">Ja</span>{% else %}<span class="badge bg-secondary">Nein</span>{% endif %}
                                    </p>
                                    {% if destinataer.letzter_studiennachweis %}
                                        <p><strong>Letzter Nachweis:</strong> {{ destinataer.letzter_studiennachweis|date:"d.m.Y" }}</p>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Förderungen -->
                    {% if foerderungen %}
                        <div class="card shadow mb-4">
                            <div class="card-header bg-secondary text-white">
                                <h5 class="card-title mb-0">
                                    <i class="fas fa-gift me-2"></i>Förderungen ({{ foerderungen.count }})
                                </h5>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-hover">
                                        <thead class="table-light">
                                            <tr>
                                                <th>Jahr</th>
                                                <th>Betrag</th>
                                                <th>Kategorie</th>
                                                <th>Status</th>
                                                <th>Verwendungsnachweis</th>
                                                <th>Aktionen</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for foerderung in foerderungen %}
                                            <tr>
                                                <td>{{ foerderung.jahr }}</td>
                                                <td><span class="text-success fw-bold">€{{ foerderung.betrag|floatformat:2 }}</span></td>
                                                <td>{{ foerderung.get_kategorie_display }}</td>
                                                <td>
                                                    {% if foerderung.status == 'bewilligt' %}
                                                        <span class="badge bg-success">Bewilligt</span>
                                                    {% elif foerderung.status == 'abgelehnt' %}
                                                        <span class="badge bg-danger">Abgelehnt</span>
                                                    {% elif foerderung.status == 'in_bearbeitung' %}
                                                        <span class="badge bg-warning">In Bearbeitung</span>
                                                    {% else %}
                                                        <span class="badge bg-secondary">{{ foerderung.get_status_display }}</span>
                                                    {% endif %}
                                                </td>
                                                <td>
                                                    {% if foerderung.verwendungsnachweis %}
                                                        <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 %}
                                                </td>
                                                <td>
                                                    <a href="{% url 'stiftung:foerderung_detail' pk=foerderung.pk %}" 
                                                       class="btn btn-sm btn-outline-primary">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                </td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    {% endif %}

                    <!-- Unterstützungen -->
                    {% if unterstuetzungen %}
                        <div class="card shadow mb-4">
                            <div class="card-header bg-info text-white">
                                <div class="d-flex justify-content-between align-items-center">
                                    <h5 class="card-title mb-0">
                                        <i class="fas fa-hand-holding-usd me-2"></i>Unterstützungen ({{ unterstuetzungen.count }})
                                    </h5>
                                    <a href="{% url 'stiftung:unterstuetzung_create' %}?destinataer={{ destinataer.pk }}" 
                                       class="btn btn-light btn-sm">
                                        <i class="fas fa-plus me-1"></i>Neue Unterstützung
                                    </a>
                                </div>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-hover">
                                        <thead class="table-light">
                                            <tr>
                                                <th>Fällig am</th>
                                                <th>Betrag</th>
                                                <th>Status</th>
                                                <th>Beschreibung</th>
                                                <th>Aktionen</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for unterstuetzung in unterstuetzungen %}
                                            <tr>
                                                <td>{{ unterstuetzung.faellig_am|date:"d.m.Y" }}</td>
                                                <td><span class="text-success fw-bold">€{{ unterstuetzung.betrag|floatformat:2 }}</span></td>
                                                <td>
                                                    {% if unterstuetzung.status == 'ausgezahlt' %}
                                                        <span class="badge bg-success">Ausgezahlt</span>
                                                    {% elif unterstuetzung.status == 'in_bearbeitung' %}
                                                        <span class="badge bg-warning">In Bearbeitung</span>
                                                    {% elif unterstuetzung.status == 'geplant' %}
                                                        <span class="badge bg-secondary">Geplant</span>
                                                    {% else %}
                                                        <span class="badge bg-danger">{{ unterstuetzung.get_status_display }}</span>
                                                    {% endif %}
                                                </td>
                                                <td>{{ unterstuetzung.beschreibung|truncatechars:40 }}</td>
                                                <td>
                                                    <a href="{% url 'stiftung:unterstuetzung_detail' pk=unterstuetzung.pk %}" 
                                                       class="btn btn-sm btn-outline-primary">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                </td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    {% endif %}

                    <!-- Verknüpfte Dokumente -->
                    <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>Verknüpfte Dokumente
                            </h5>
                            <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-light btn-sm">
                                <i class="fas fa-plus me-1"></i>Dokument verknüpfen
                            </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>
                                                    <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 diesem Destinatär.</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>

                    <!-- Notizen -->
                    <div class="card shadow mb-4">
                        <div class="card-header bg-light d-flex justify-content-between align-items-center">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-sticky-note me-2"></i>Notizen
                            </h5>
                            <a class="btn btn-sm btn-primary" href="{% url 'stiftung:destinataer_notiz_create' pk=destinataer.pk %}">
                                <i class="fas fa-plus me-1"></i>Notiz hinzufügen
                            </a>
                        </div>
                        <div class="card-body">
                            {% if notizen_eintraege %}
                                <div class="list-group">
                                    {% for n in notizen_eintraege %}
                                    <div class="list-group-item">
                                        <div class="d-flex justify-content-between">
                                            <div>
                                                <strong>{{ n.erstellt_am|date:"d.m.Y H:i" }}</strong>
                                                {% if n.erstellt_von %}<span class="text-muted">— {{ n.erstellt_von.username }}</span>{% endif %}
                                            </div>
                                            {% if n.datei %}
                                                <a href="{{ n.datei.url }}" target="_blank" class="btn btn-sm btn-outline-secondary"><i class="fas fa-paperclip"></i> Anhang</a>
                                            {% endif %}
                                        </div>
                                        {% if n.titel %}<div class="mt-1"><em>{{ n.titel }}</em></div>{% endif %}
                                        {% if n.text %}<div class="mt-1">{{ n.text|linebreaks }}</div>{% endif %}
                                    </div>
                                    {% endfor %}
                                </div>
                            {% else %}
                                <p class="text-muted mb-0">Keine Notizen vorhanden.</p>
                            {% endif %}
                        </div>
                    </div>
                </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">{{ foerderungen.count }}</h4>
                                            <small class="text-muted">Förderungen</small>
                                        </div>
                                    </div>
                                    <div class="col-6">
                                        <h4 class="text-success">
                                            {% if foerderungen %}
                                                {% with total_betrag=foerderungen|length %}
                                                    €{{ total_betrag|floatformat:0 }}
                                                {% endwith %}
                                            {% else %}
                                                €0
                                            {% endif %}
                                        </h4>
                                        <small class="text-muted">Gesamtbetrag</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:destinataer_update' pk=destinataer.pk %}" class="btn btn-warning">
                                    <i class="fas fa-edit me-2"></i>Bearbeiten
                                </a>
                                <a href="{% url 'stiftung:destinataer_export' pk=destinataer.pk %}" class="btn btn-success">
                                    <i class="fas fa-download me-2"></i>Export
                                </a>
                                <a href="{% url 'stiftung:destinataer_delete' pk=destinataer.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> {{ destinataer.id }}</p>
                            <p><strong>Erstellt:</strong> {{ destinataer.id|slice:":8" }}</p>
                            {% if destinataer.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 %} 
