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

{% block title %}Geschäftsführung - van Hees-Theyssen-Vogel'sche Stiftung{% 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-briefcase me-2"></i>Geschäftsführung
                </h1>
                <div class="btn-group" role="group">
                    <a href="{% url 'stiftung:rentmeister_list' %}" class="btn btn-primary">
                        <i class="fas fa-user-tie me-1"></i>Rentmeister
                    </a>
                    <a href="{% url 'stiftung:konto_list' %}" class="btn btn-outline-primary">
                        <i class="fas fa-university me-1"></i>Konten
                    </a>
                    <a href="{% url 'stiftung:verwaltungskosten_list' %}" class="btn btn-outline-primary">
                        <i class="fas fa-file-invoice-dollar me-1"></i>Verwaltungskosten
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Rentmeister-Übersicht -->
    <div class="row">
        <div class="col-xl-12 col-lg-12">
            <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-user-tie me-2"></i>Aktive Rentmeister
                    </h6>
                    <a href="{% url 'stiftung:rentmeister_list' %}" class="btn btn-sm btn-outline-primary">
                        Alle verwalten
                    </a>
                </div>
                <div class="card-body">
                    {% if rentmeister %}
                        <div class="row">
                            {% for rm in rentmeister %}
                            <div class="col-md-6 col-lg-4 mb-3">
                                <div class="card border-primary">
                                    <div class="card-body text-center">
                                        <i class="fas fa-user-tie fa-2x text-primary mb-2"></i>
                                        <h6>{{ rm.get_full_name }}</h6>
                                        <small class="text-muted">seit {{ rm.seit_datum|date:"d.m.Y" }}</small>
                                        {% if rm.email %}
                                            <br><small><i class="fas fa-envelope me-1"></i>{{ rm.email }}</small>
                                        {% endif %}
                                        <div class="mt-2">
                                            <a href="{% url 'stiftung:rentmeister_detail' rm.pk %}" class="btn btn-sm btn-primary">
                                                Details
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <p class="text-muted">Keine aktiven Rentmeister vorhanden.</p>
                        <a href="{% url 'stiftung:rentmeister_create' %}" class="btn btn-primary">
                            <i class="fas fa-plus me-1"></i>Ersten Rentmeister anlegen
                        </a>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Konten-Übersicht -->
    <div class="row">
        <div class="col-xl-6 col-lg-6">
            <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-university me-2"></i>Stiftungskonten
                    </h6>
                    <span class="badge bg-primary">{{ konten.count }} Konten</span>
                </div>
                <div class="card-body">
                    {% if konten %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Bank</th>
                                        <th>Kontoname</th>
                                        <th>Typ</th>
                                        <th class="text-end">Saldo</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for konto in konten %}
                                    <tr>
                                        <td>{{ konto.bank_name }}</td>
                                        <td>{{ konto.kontoname }}</td>
                                        <td>
                                            <span class="badge bg-secondary">{{ konto.get_konto_typ_display }}</span>
                                        </td>
                                        <td class="text-end">
                                            <strong>€{{ konto.saldo|floatformat:2 }}</strong>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                                <tfoot>
                                    <tr class="table-primary">
                                        <th colspan="3">Gesamtsaldo</th>
                                        <th class="text-end">€{{ gesamtsaldo|floatformat:2 }}</th>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-muted">Keine Konten vorhanden.</p>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Kosten-Statistik -->
        <div class="col-xl-6 col-lg-6">
            <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-chart-pie me-2"></i>Kosten (letzte 30 Tage)
                    </h6>
                    <span class="badge bg-warning">€{{ kosten_summe_monat|floatformat:2 }}</span>
                </div>
                <div class="card-body">
                    {% if kosten_statistik %}
                        <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 kosten_statistik %}
                                    <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>
                    {% else %}
                        <p class="text-muted">Keine Kosten in den letzten 30 Tagen.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Aktuelle Kosten -->
    <div class="row">
        <div class="col-12">
            <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-file-invoice-dollar me-2"></i>Aktuelle Verwaltungskosten
                    </h6>
                    <a href="{% url 'stiftung:verwaltungskosten_list' %}" class="btn btn-sm btn-outline-primary">
                        Alle anzeigen
                    </a>
                </div>
                <div class="card-body">
                    {% if aktuelle_kosten %}
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>Datum</th>
                                        <th>Bezeichnung</th>
                                        <th>Kategorie</th>
                                        <th>Lieferant</th>
                                        <th>Status</th>
                                        <th class="text-end">Betrag</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for kosten in aktuelle_kosten %}
                                    <tr>
                                        <td>{{ kosten.datum|date:"d.m.Y" }}</td>
                                        <td>
                                            <strong>{{ kosten.bezeichnung }}</strong>
                                            {% if kosten.rechnungsnummer %}
                                                <br><small class="text-muted">RG: {{ kosten.rechnungsnummer }}</small>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <span class="badge bg-info">{{ kosten.get_kategorie_display }}</span>
                                        </td>
                                        <td>{{ kosten.lieferant_firma|default:"-" }}</td>
                                        <td>
                                            <span class="badge bg-{{ kosten.get_status_color }}">{{ kosten.get_status_display }}</span>
                                        </td>
                                        <td class="text-end">
                                            <strong>€{{ kosten.betrag|floatformat:2 }}</strong>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-muted">Keine aktuellen Verwaltungskosten vorhanden.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

{% endblock %}
