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

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

{% block content %}
    <div class="row">
        <div class="col-12">
            <h1 class="h3 mb-4">
                <i class="fas fa-tachometer-alt text-primary me-2"></i>
                Dashboard - Stiftungsverwaltung
            </h1>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <!-- Land Statistics -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Ländereien
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ total_land }}
                            </div>
                            <div class="text-xs text-muted">
                                {{ total_flaeche|floatformat:0 }} qm Gesamtfläche
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-map fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Verpachtung Statistics -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                                Verpachtungen
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ active_verpachtungen }}
                            </div>
                            <div class="text-xs text-muted">
                                {{ total_verpachtet|floatformat:0 }} qm verpachtet
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-handshake fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Financial Statistics -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                Einnahmen
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                €{{ total_pachtzins|floatformat:0 }}
                            </div>
                            <div class="text-xs text-muted">
                                Jährlicher Pachtzins
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-euro-sign fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-bolt me-2"></i>Schnellzugriff
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:foerderung_create' %}" class="btn btn-outline-primary w-100 h-100 d-flex flex-column align-items-center justify-content-center p-3">
                                <i class="fas fa-gift fa-2x mb-2"></i>
                                <span>Neue Förderung</span>
                            </a>
                        </div>
                        <!-- Removed generic "Neue Verpachtung" - now created via specific Land pages -->
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:bericht_list' %}" class="btn btn-outline-info w-100 h-100 d-flex flex-column align-items-center justify-content-center p-3">
                                <i class="fas fa-chart-bar fa-2x mb-2"></i>
                                <span>Jahresberichte</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:csv_import_create' %}" class="btn btn-outline-warning w-100 h-100 d-flex flex-column align-items-center justify-content-center p-3">
                                <i class="fas fa-upload fa-2x mb-2"></i>
                                <span>CSV Import</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-outline-info w-100 h-100 d-flex flex-column align-items-center justify-content-center p-3">
                                <i class="fas fa-file-alt fa-2x mb-2"></i>
                                <span>Dokumentenverwaltung</span>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{% url 'stiftung:dokument_list' %}" class="btn btn-outline-secondary w-100 h-100 d-flex flex-column align-items-center justify-content-center p-3">
                                <i class="fas fa-link fa-2x mb-2"></i>
                                <span>Dokumente</span>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Dokumentenübersicht -->
    <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-success">
                        <i class="fas fa-file-alt me-2"></i>Dokumentenübersicht
                    </h6>
                    <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-success btn-sm">
                        <i class="fas fa-external-link-alt me-1"></i>Dokumentenverwaltung
                    </a>
                </div>
                <div class="card-body">
                    {% if dokumente_uebersicht %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Dokument</th>
                                        <th>Kontext</th>
                                        <th>Verknüpft mit</th>
                                        <th>Aktionen</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for dokument in dokumente_uebersicht %}
                                    <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.verpachtung_id %}
                                                <span class="badge bg-info">Verpachtung</span>
                                            {% elif dokument.land_id %}
                                                <span class="badge bg-success">Länderei</span>
                                            {% elif dokument.paechter_id %}
                                                <span class="badge bg-primary">Pächter</span>
                                            {% elif dokument.destinataer_id %}
                                                <span class="badge bg-warning">Destinatär</span>
                                            {% elif dokument.foerderung_id %}
                                                <span class="badge bg-secondary">Förderung</span>
                                            {% else %}
                                                <span class="text-muted">Keine Verknüpfung</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="{% url 'stiftung:dokument_detail' dokument.pk %}" class="btn btn-sm btn-outline-info" title="Details">
                                                    <i class="fas fa-eye"></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>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        <div class="text-center mt-3">
                            <a href="{% url 'stiftung:dokument_list' %}" class="btn btn-success">
                                Alle Dokumente anzeigen
                            </a>
                        </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 Ihren Entitäten.</p>
                            <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-success">
                                <i class="fas fa-external-link-alt me-2"></i>Dokumentenverwaltung öffnen
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Verfügbare Paperless-Dokumente -->
    {% if available_paperless_docs %}
    <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-info">
                        <i class="fas fa-plus-circle me-2"></i>Verfügbare Paperless-Dokumente
                    </h6>
                    <span class="badge bg-info">{{ available_paperless_docs|length }} verfügbar</span>
                </div>
                <div class="card-body">
                    <div class="row">
                        {% for doc in available_paperless_docs|slice:":6" %}
                        <div class="col-md-6 col-lg-4 mb-3">
                            <div class="card h-100 border-info">
                                <div class="card-body">
                                    <h6 class="card-title">{{ doc.title }}</h6>
                                    <div class="mb-2">
                                        {% for tag in doc.tags %}
                                            {% if tag == 'Stiftung_Destinatäre' or tag == 'Stiftung_Land_und_Pächter' or tag == 'Stiftung_Administration' %}
                                                <span class="badge bg-primary me-1">{{ tag }}</span>
                                            {% else %}
                                                <span class="badge bg-light text-dark me-1">{{ tag }}</span>
                                            {% endif %}
                                        {% endfor %}
                                    </div>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <a href="{{ doc.document_url }}" target="_blank" class="btn btn-sm btn-outline-info">
                                            <i class="fas fa-external-link-alt me-1"></i>In Paperless öffnen
                                        </a>
                                        <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-sm btn-success">
                                            <i class="fas fa-link me-1"></i>Verknüpfen
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                    <div class="text-center mt-3">
                        <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-info">
                            <i class="fas fa-external-link-alt me-2"></i>Alle verfügbaren Dokumente anzeigen
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- Recent Activities -->
    <div class="row">
        <!-- Recent Lands -->
        <div class="col-lg-6 mb-4">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-map me-2"></i>Neueste Ländereien
                    </h6>
                </div>
                <div class="card-body">
                    {% if recent_lands %}
                        <div class="list-group list-group-flush">
                            {% for land in recent_lands %}
                            <div class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <h6 class="mb-1">{{ land.gemeinde }} - {{ land.gemarkung }}</h6>
                                    <small class="text-muted">
                                        Flur {{ land.flur }}, Flurstück {{ land.flurstueck }} 
                                        ({{ land.groesse_qm|floatformat:0 }} qm)
                                    </small>
                                </div>
                                <a href="{% url 'stiftung:land_detail' land.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </div>
                            {% endfor %}
                        </div>
                        <div class="text-center mt-3">
                            <a href="{% url 'stiftung:land_list' %}" class="btn btn-sm btn-primary">
                                Alle Ländereien anzeigen
                            </a>
                        </div>
                    {% else %}
                        <p class="text-muted text-center">Noch keine Ländereien vorhanden.</p>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Recent Verpachtungen -->
        <div class="col-lg-6 mb-4">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-handshake me-2"></i>Neueste Verpachtungen
                    </h6>
                </div>
                <div class="card-body">
                    {% if recent_verpachtungen %}
                        <div class="list-group list-group-flush">
                            {% for verpachtung in recent_verpachtungen %}
                            <div class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <h6 class="mb-1">{{ verpachtung.land.gemeinde }}</h6>
                                    <small class="text-muted">
                                        {{ verpachtung.paechter.get_full_name }} - 
                                        €{{ verpachtung.pachtzins_jaehrlich|floatformat:0 }}/Jahr
                                    </small>
                                </div>
                                <a href="{% url 'stiftung:land_verpachtung_detail' verpachtung.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </div>
                            {% endfor %}
                        </div>
                        <div class="text-center mt-3">
                            <a href="{% url 'stiftung:land_list' %}" class="btn btn-sm btn-primary">
                                Ländereien verwalten
                            </a>
                        </div>
                    {% else %}
                        <p class="text-muted text-center">Noch keine Verpachtungen vorhanden.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Financial Overview -->
    <div class="row">
        <div class="col-12">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-chart-pie me-2"></i>Finanzübersicht
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 text-center">
                            <div class="border rounded p-3">
                                <h4 class="text-success">€{{ total_pachtzins|floatformat:0 }}</h4>
                                <p class="text-muted mb-0">Jährlicher Pachtzins</p>
                            </div>
                        </div>
                        <div class="col-md-4 text-center">
                            <div class="border rounded p-3">
                                <h4 class="text-info">€{{ total_foerderungen|floatformat:0 }}</h4>
                                <p class="text-muted mb-0">Gesamtförderungen</p>
                            </div>
                        </div>
                        <div class="col-md-4 text-center">
                            <div class="border rounded p-3">
                                <h4 class="text-warning">{{ total_verpachtet|floatformat:0 }} qm</h4>
                                <p class="text-muted mb-0">Verpachtete Fläche</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block extra_js %}
<script>
    // Auto-refresh dashboard every 5 minutes
    setTimeout(function() {
        location.reload();
    }, 300000);
</script>
{% endblock %} 
