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

{% block title %}Pächter-Workflow – 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-tractor text-primary me-2"></i>
                Pächter-Workflow
            </h1>
            <div class="d-flex gap-2">
                <a href="{% url 'stiftung:land_abrechnung_create' %}" class="btn btn-outline-primary">
                    <i class="fas fa-plus me-2"></i>Neue Abrechnung
                </a>
                <a href="{% url 'stiftung:paechter_list' %}" class="btn btn-outline-secondary">
                    <i class="fas fa-list me-2"></i>Alle Pächter
                </a>
            </div>
        </div>

        <!-- Pipeline: Vertragsfristen -->
        <div class="card shadow mb-4">
            <div class="card-header bg-dark text-white py-2">
                <h5 class="mb-0 small fw-bold">
                    <i class="fas fa-calendar-alt me-2"></i>Pipeline: Vertragsfristen
                </h5>
            </div>
            <div class="card-body p-0">
                <div class="row g-0">
                    {% for stage in pipeline_stages %}
                    <div class="col-xl col-lg-4 col-md-6 border-end">
                        <div class="p-3">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h6 class="mb-0 text-{{ stage.farbe }} fw-bold small">
                                    <i class="fas {{ stage.icon }} me-1"></i>{{ stage.label }}
                                </h6>
                                <span class="badge bg-{{ stage.farbe }} {% if stage.farbe == 'warning' %}text-dark{% else %}text-white{% endif %}">
                                    {{ stage.count }}
                                </span>
                            </div>
                            {% if stage.verpachtungen %}
                            {% for v in stage.verpachtungen %}
                            <div class="card mb-2 border-start border-3 border-{{ stage.farbe }}" style="font-size:0.78rem;">
                                <div class="card-body py-2 px-2">
                                    <div class="fw-semibold">
                                        <a href="{% url 'stiftung:land_verpachtung_detail' pk=v.pk %}" class="text-decoration-none text-dark">
                                            {{ v.land.lfd_nr }} – {{ v.paechter.get_full_name }}
                                        </a>
                                    </div>
                                    {% if v.pachtende %}
                                    <div class="text-muted mt-1">
                                        Ende: {{ v.pachtende|date:"d.m.Y" }}
                                        {% if v.pachtende >= heute %}
                                        <span class="ms-1">({{ v.pachtende|timeuntil:heute }})</span>
                                        {% else %}
                                        <span class="badge bg-danger ms-1">abgelaufen</span>
                                        {% endif %}
                                    </div>
                                    {% endif %}
                                    <div class="text-muted">
                                        {% if v.verpachtete_flaeche %}{{ v.verpachtete_flaeche|floatformat:0 }} m²{% endif %}
                                        {% if v.pachtzins_pauschal %} · €{{ v.pachtzins_pauschal|floatformat:2 }}/J{% endif %}
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                            {% else %}
                            <div class="text-muted text-center small py-3">
                                <i class="fas fa-check-circle fa-lg d-block mb-1 opacity-25"></i>
                                Keine
                            </div>
                            {% endif %}
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>

        <div class="row g-4">
            <!-- Ausstehende Jahresabrechnungen -->
            <div class="col-lg-6">
                <div class="card shadow h-100">
                    <div class="card-header bg-warning text-dark py-2">
                        <h5 class="mb-0 small fw-bold">
                            <i class="fas fa-file-invoice me-2"></i>
                            Ausstehende Jahresabrechnungen {{ letztes_jahr }}
                        </h5>
                    </div>
                    <div class="card-body p-0">
                        {% if laender_ohne_abrechnung %}
                        <div class="table-responsive">
                            <table class="table table-sm table-hover mb-0">
                                <thead class="table-light">
                                    <tr>
                                        <th>Lfd. Nr.</th>
                                        <th>Gemeinde</th>
                                        <th>Pächter</th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for land in laender_ohne_abrechnung %}
                                    <tr>
                                        <td>{{ land.lfd_nr }}</td>
                                        <td>{{ land.gemeinde|default:"–" }}</td>
                                        <td>{{ land.paechter_name|default:"–" }}</td>
                                        <td>
                                            <a href="{% url 'stiftung:land_abrechnung_create' %}?land={{ land.pk }}&jahr={{ letztes_jahr }}" class="btn btn-xs btn-outline-primary" style="font-size:0.7rem;padding:2px 6px;">
                                                <i class="fas fa-plus"></i>
                                            </a>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="text-muted text-center py-4">
                            <i class="fas fa-check-circle fa-2x mb-2 d-block text-success opacity-50"></i>
                            Alle Jahresabrechnungen {{ letztes_jahr }} vorhanden.
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- Pachtanpassungen fällig (> 5 Jahre) -->
            <div class="col-lg-6">
                <div class="card shadow h-100">
                    <div class="card-header bg-info text-white py-2">
                        <h5 class="mb-0 small fw-bold">
                            <i class="fas fa-chart-line me-2"></i>
                            Pachtanpassung fällig (> 5 Jahre)
                        </h5>
                    </div>
                    <div class="card-body p-0">
                        {% if lang_laufend %}
                        <div class="table-responsive">
                            <table class="table table-sm table-hover mb-0">
                                <thead class="table-light">
                                    <tr>
                                        <th>Land</th>
                                        <th>Pächter</th>
                                        <th>Beginn</th>
                                        <th>Dauer</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for v in lang_laufend %}
                                    <tr>
                                        <td>
                                            <a href="{% url 'stiftung:land_verpachtung_detail' pk=v.pk %}">
                                                {{ v.land.lfd_nr }}
                                            </a>
                                        </td>
                                        <td>{{ v.paechter.get_full_name }}</td>
                                        <td>{{ v.pachtbeginn|date:"d.m.Y" }}</td>
                                        <td>
                                            <span class="badge bg-warning text-dark">
                                                {{ v.pachtbeginn|timesince:heute }}
                                            </span>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="text-muted text-center py-4">
                            <i class="fas fa-check-circle fa-2x mb-2 d-block text-success opacity-50"></i>
                            Keine Pachtanpassungen fällig.
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        <!-- Top-Pächter -->
        <div class="card shadow mt-4">
            <div class="card-header bg-primary text-white py-2">
                <h5 class="mb-0 small fw-bold">
                    <i class="fas fa-crown me-2"></i>Top-Pächter nach Fläche
                </h5>
            </div>
            <div class="card-body p-0">
                {% if top_paechter %}
                <div class="table-responsive">
                    <table class="table table-sm table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>#</th>
                                <th>Pächter</th>
                                <th>Verträge</th>
                                <th>Gesamtfläche (m²)</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for p in top_paechter %}
                            <tr>
                                <td class="text-muted">{{ forloop.counter }}</td>
                                <td>
                                    <a href="{% url 'stiftung:paechter_detail' pk=p.pk %}">
                                        {{ p.get_full_name }}
                                    </a>
                                </td>
                                <td>{{ p.anzahl_vertraege }}</td>
                                <td>{{ p.flaeche|floatformat:0|default:"–" }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <div class="text-muted text-center py-4">Keine Pächter gefunden.</div>
                {% endif %}
            </div>
        </div>

    </div>
</div>
{% endblock %}
