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

{% block title %}{{ rentmeister.get_full_name }} - Rentmeister{% endblock %}

{% block content %}

    <div class="row">
        <div class="col-12">
            <div class="d-sm-flex align-items-center justify-content-between mb-4">
                <h1 class="h3 mb-0 text-gray-800">
                    <i class="fas fa-user-tie me-2"></i>{{ rentmeister }}
                </h1>
                <div class="btn-group" role="group">
                    <a href="{% url 'stiftung:rentmeister_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i>Zurück zur Liste
                    </a>
                    <a href="{% url 'stiftung:rentmeister_edit' rentmeister.pk %}" class="btn btn-outline-warning">
                        <i class="fas fa-edit me-1"></i>Bearbeiten
                    </a>
                    <a href="{% url 'stiftung:verwaltungskosten_create' %}?rentmeister={{ rentmeister.pk }}" class="btn btn-success">
                        <i class="fas fa-plus me-1"></i>Neue Ausgabe
                    </a>
                    <a href="{% url 'stiftung:rentmeister_ausgaben' rentmeister.pk %}" class="btn btn-primary">
                        <i class="fas fa-file-invoice-dollar me-1"></i>Alle Ausgaben
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Persönliche Informationen -->
    <div class="row">
        <div class="col-xl-4 col-lg-5">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-user me-2"></i>Persönliche Daten
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Name:</strong></div>
                        <div class="col-sm-8">{{ rentmeister.get_full_name }}</div>
                    </div>
                    {% if rentmeister.anrede %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Anrede:</strong></div>
                        <div class="col-sm-8">{{ rentmeister.get_anrede_display }}</div>
                    </div>
                    {% endif %}
                    {% if rentmeister.titel %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Titel:</strong></div>
                        <div class="col-sm-8">{{ rentmeister.titel }}</div>
                    </div>
                    {% endif %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Im Amt seit:</strong></div>
                        <div class="col-sm-8">{{ rentmeister.seit_datum|date:"d.m.Y" }}</div>
                    </div>
                    {% if rentmeister.bis_datum %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Im Amt bis:</strong></div>
                        <div class="col-sm-8">{{ rentmeister.bis_datum|date:"d.m.Y" }}</div>
                    </div>
                    {% endif %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Status:</strong></div>
                        <div class="col-sm-8">
                            {% if rentmeister.aktiv %}
                                <span class="badge bg-success">Aktiv</span>
                            {% else %}
                                <span class="badge bg-secondary">Inaktiv</span>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>

            <!-- Kontaktdaten -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-address-book me-2"></i>Kontaktdaten
                    </h6>
                </div>
                <div class="card-body">
                    {% if rentmeister.email %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>E-Mail:</strong></div>
                        <div class="col-sm-8">
                            <a href="mailto:{{ rentmeister.email }}">{{ rentmeister.email }}</a>
                        </div>
                    </div>
                    {% endif %}
                    {% if rentmeister.telefon %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Telefon:</strong></div>
                        <div class="col-sm-8">
                            <a href="tel:{{ rentmeister.telefon }}">{{ rentmeister.telefon }}</a>
                        </div>
                    </div>
                    {% endif %}
                    {% if rentmeister.mobil %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Mobil:</strong></div>
                        <div class="col-sm-8">
                            <a href="tel:{{ rentmeister.mobil }}">{{ rentmeister.mobil }}</a>
                        </div>
                    </div>
                    {% endif %}
                    {% if rentmeister.get_address %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Adresse:</strong></div>
                        <div class="col-sm-8">{{ rentmeister.get_address }}</div>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- Vergütung -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-euro-sign me-2"></i>Vergütung
                    </h6>
                </div>
                <div class="card-body">
                    {% if rentmeister.monatliche_verguetung %}
                    <div class="row mb-3">
                        <div class="col-sm-6"><strong>Monatlich:</strong></div>
                        <div class="col-sm-6">€{{ rentmeister.monatliche_verguetung|floatformat:2 }}</div>
                    </div>
                    {% endif %}
                    <div class="row mb-3">
                        <div class="col-sm-6"><strong>KM-Pauschale:</strong></div>
                        <div class="col-sm-6">€{{ rentmeister.km_pauschale|floatformat:2 }}/km</div>
                    </div>
                    {% if rentmeister.iban %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>IBAN:</strong></div>
                        <div class="col-sm-8"><code>{{ rentmeister.iban }}</code></div>
                    </div>
                    {% endif %}
                    {% if rentmeister.bank_name %}
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>Bank:</strong></div>
                        <div class="col-sm-8">{{ rentmeister.bank_name }}</div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Ausgaben und Statistiken -->
        <div class="col-xl-8 col-lg-7">
            <!-- Statistiken -->
            <div class="row">
                <div class="col-md-3 mb-4">
                    <div class="card border-primary">
                        <div class="card-body text-center">
                            <i class="fas fa-file-invoice-dollar fa-2x text-primary mb-2"></i>
                            <h4>{{ stats.anzahl_ausgaben }}</h4>
                            <small class="text-muted">Ausgaben gesamt</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 mb-4">
                    <div class="card border-success">
                        <div class="card-body text-center">
                            <i class="fas fa-euro-sign fa-2x text-success mb-2"></i>
                            <h4>€{{ stats.gesamt_ausgaben|floatformat:0 }}</h4>
                            <small class="text-muted">Summe gesamt</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 mb-4">
                    <div class="card border-info">
                        <div class="card-body text-center">
                            <i class="fas fa-calendar-alt fa-2x text-info mb-2"></i>
                            <h4>€{{ stats.jahr_ausgaben|floatformat:0 }}</h4>
                            <small class="text-muted">Dieses Jahr</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 mb-4">
                    <div class="card border-warning">
                        <div class="card-body text-center">
                            <i class="fas fa-clock fa-2x text-warning mb-2"></i>
                            <h4>{{ stats.offene_ausgaben }}</h4>
                            <small class="text-muted">Offen</small>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Kategorie-Aufschlüsselung -->
            {% if kategorie_stats %}
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-chart-pie me-2"></i>Ausgaben nach Kategorien
                    </h6>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>Kategorie</th>
                                    <th class="text-center">Anzahl</th>
                                    <th class="text-end">Summe</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for stat in kategorie_stats %}
                                <tr>
                                    <td>{{ stat.kategorie|capfirst }}</td>
                                    <td class="text-center">{{ stat.anzahl }}</td>
                                    <td class="text-end">€{{ stat.summe|floatformat:2 }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- Aktuelle Ausgaben -->
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-history me-2"></i>Aktuelle Ausgaben (letzte 30 Tage)
                    </h6>
                    <a href="{% url 'stiftung:rentmeister_ausgaben' rentmeister.pk %}" class="btn btn-sm btn-outline-primary">
                        Alle Ausgaben
                    </a>
                </div>
                <div class="card-body">
                    {% if aktuelle_ausgaben %}
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>Datum</th>
                                        <th>Bezeichnung</th>
                                        <th>Kategorie</th>
                                        <th>Status</th>
                                        <th class="text-end">Betrag</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for ausgabe in aktuelle_ausgaben %}
                                    <tr>
                                        <td>{{ ausgabe.datum|date:"d.m.Y" }}</td>
                                        <td>
                                            <strong>{{ ausgabe.bezeichnung }}</strong>
                                            {% if ausgabe.kategorie == 'fahrtkosten' and ausgabe.km_anzahl %}
                                                <br><small class="text-info">
                                                    <i class="fas fa-route me-1"></i>{{ ausgabe.km_anzahl }} km
                                                </small>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <span class="badge bg-info">{{ ausgabe.get_kategorie_display }}</span>
                                        </td>
                                        <td>
                                            <span class="badge bg-{{ ausgabe.get_status_color }}">{{ ausgabe.get_status_display }}</span>
                                        </td>
                                        <td class="text-end">
                                            <strong>€{{ ausgabe.betrag|floatformat:2 }}</strong>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-muted">Keine Ausgaben in den letzten 30 Tagen.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Verknüpfte Dokumente -->
    <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>Verknüpfte Dokumente
                        {% if verknuepfte_dokumente %}
                            <span class="badge bg-primary ms-2">{{ verknuepfte_dokumente.count }}</span>
                        {% endif %}
                    </h6>
                    <a href="{% url 'stiftung:dms_list' %}" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-folder-open me-1"></i>Dokumentenverwaltung
                    </a>
                </div>
                <div class="card-body">
                    {% if verknuepfte_dokumente %}
                        <div class="list-group list-group-flush">
                            {% for dokument in verknuepfte_dokumente %}
                            <div class="list-group-item d-flex justify-content-between align-items-start">
                                <div class="ms-2 me-auto">
                                    <div class="fw-bold">
                                        <i class="fas fa-file-pdf text-danger me-1"></i>
                                        {{ dokument.titel }}
                                    </div>
                                    <small class="text-muted">
                                        Kontext: {{ dokument.get_kontext_display }}
                                        {% if dokument.beschreibung %}
                                            • {{ dokument.beschreibung|truncatechars:100 }}
                                        {% endif %}
                                    </small>
                                </div>
                                <div class="d-flex gap-2">
                                    <span class="badge bg-secondary">Legacy</span>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                        
                        {% if verknuepfte_dokumente.count == 10 %}
                        <div class="text-center mt-3">
                            <small class="text-muted">
                                <i class="fas fa-info-circle me-1"></i>
                                Es werden nur die neuesten 10 Dokumente angezeigt. 
                                <a href="{% url 'stiftung:dms_list' %}" class="text-decoration-none">Alle Dokumente anzeigen</a>
                            </small>
                        </div>
                        {% endif %}
                    {% 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 über die 
                                <a href="{% url 'stiftung:dms_list' %}" class="text-decoration-none">Dokumentenverwaltung</a>.
                            </p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Notizen -->
    {% if rentmeister.notizen %}
    <div class="row">
        <div class="col-12">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-sticky-note me-2"></i>Notizen
                    </h6>
                </div>
                <div class="card-body">
                    <p>{{ rentmeister.notizen|linebreaks }}</p>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

{% endblock %}
