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

{% block title %}Verpachtungen - 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-handshake text-primary me-2"></i>Verpachtungen
                </h1>
                <a href="{% url 'stiftung:verpachtung_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>Neue Verpachtung
                </a>
            </div>

            <!-- Statistics Cards -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="card bg-primary text-white">
                        <div class="card-body">
                            <h5 class="card-title">Aktive Verpachtungen</h5>
                            <h3 class="card-text">{{ aktive_verpachtungen|default:"0" }}</h3>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-success text-white">
                        <div class="card-body">
                            <h5 class="card-title">Gesamtfläche</h5>
                            <h3 class="card-text">{{ gesamt_flaeche|default:"0"|floatformat:0 }} qm</h3>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-info text-white">
                        <div class="card-body">
                            <h5 class="card-title">Jährlicher Pachtzins</h5>
                            <h3 class="card-text">€{{ jaehrlicher_pachtzins|default:"0"|floatformat:0 }}</h3>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-warning text-white">
                        <div class="card-body">
                            <h5 class="card-title">Anzahl</h5>
                            <h3 class="card-text">{{ page_obj.paginator.count|default:"0" }}</h3>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Filters -->
            <div class="card mb-4">
                <div class="card-header">
                    <h6 class="mb-0">
                        <i class="fas fa-filter me-2"></i>Filter
                    </h6>
                </div>
                <div class="card-body">
                    <form method="get" class="row g-3">
                        <div class="col-md-3">
                            <label for="search" class="form-label">Suche</label>
                            <input type="text" name="search" id="search" class="form-control" value="{{ search_query }}" placeholder="Vertragsnummer, Gemeinde, Pächter">
                        </div>
                        <div class="col-md-2">
                            <label for="status" class="form-label">Status</label>
                            <select name="status" id="status" class="form-select">
                                <option value="">Alle Status</option>
                                {% for status in status_choices %}
                                <option value="{{ status.0 }}" {% if status_filter == status.0 %}selected{% endif %}>{{ status.1 }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label for="gemeinde" class="form-label">Gemeinde</label>
                            <select name="gemeinde" id="gemeinde" class="form-select">
                                <option value="">Alle Gemeinden</option>
                                {% for gemeinde in gemeinden %}
                                <option value="{{ gemeinde }}" {% if gemeinde_filter == gemeinde %}selected{% endif %}>{{ gemeinde }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-4 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary me-2">
                                <i class="fas fa-search me-2"></i>Filtern
                            </button>
                            <a href="{% url 'stiftung:verpachtung_list' %}" class="btn btn-outline-secondary">
                                <i class="fas fa-times me-2"></i>Zurücksetzen
                            </a>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Verpachtungen Table -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0">
                        <i class="fas fa-list me-2"></i>Verpachtungen
                    </h6>
                </div>
                <div class="card-body">
                    {% if page_obj %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>
                                        <a href="?sort=vertragsnummer&dir={% if sort == 'vertragsnummer' and dir != 'desc' %}desc{% else %}asc{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}">Vertragsnummer</a>
                                    </th>
                                    <th>
                                        <a href="?sort=land&dir={% if sort == 'land' and dir != 'desc' %}desc{% else %}asc{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}">Länderei</a>
                                    </th>
                                    <th>
                                        <a href="?sort=paechter&dir={% if sort == 'paechter' and dir != 'desc' %}desc{% else %}asc{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}">Pächter</a>
                                    </th>
                                    <th>
                                        <a href="?sort=beginn&dir={% if sort == 'beginn' and dir != 'desc' %}desc{% else %}asc{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}">Beginn</a>/<a href="?sort=ende&dir={% if sort == 'ende' and dir != 'desc' %}desc{% else %}asc{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}">Ende</a>
                                    </th>
                                    <th>
                                        <a href="?sort=flaeche&dir={% if sort == 'flaeche' and dir != 'desc' %}desc{% else %}asc{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}">Fläche</a>
                                    </th>
                                    <th>
                                        <a href="?sort=pachtzins&dir={% if sort == 'pachtzins' and dir != 'desc' %}desc{% else %}asc{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}">Pachtzins</a>
                                    </th>
                                    <th>
                                        <a href="?sort=status&dir={% if sort == 'status' and dir != 'desc' %}desc{% else %}asc{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}">Status</a>
                                    </th>
                                    <th>Aktionen</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for verpachtung in page_obj %}
                                <tr>
                                    <td>
                                        <strong>{{ verpachtung.vertragsnummer }}</strong>
                                    </td>
                                    <td>
                                        <a href="{% url 'stiftung:land_detail' verpachtung.land.pk %}">
                                            {{ verpachtung.land.gemeinde }}
                                        </a>
                                    </td>
                                    <td>
                                        <a href="{% url 'stiftung:paechter_detail' verpachtung.paechter.pk %}">
                                            {{ verpachtung.paechter.get_full_name }}
                                        </a>
                                    </td>
                                    <td>
                                        {{ verpachtung.pachtbeginn|date:"d.m.Y" }} - {{ verpachtung.pachtende|date:"d.m.Y" }}
                                        {% if verpachtung.verlaengerung %}
                                        <br><small class="text-muted">Verlängert bis: {{ verpachtung.verlaengerung|date:"d.m.Y" }}</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {{ verpachtung.verpachtete_flaeche|floatformat:2 }} qm
                                        <br>
                                        <small class="text-muted">({{ verpachtung.verpachtete_flaeche_hektar|floatformat:2 }} ha)</small>
                                    </td>
                                    <td>€{{ verpachtung.pachtzins_jaehrlich|floatformat:2 }}/Jahr</td>
                                    <td>
                                        <span class="badge bg-{% if verpachtung.status == 'aktiv' %}success{% elif verpachtung.status == 'beendet' %}secondary{% elif verpachtung.status == 'gekuendigt' %}danger{% else %}warning{% endif %}">
                                            {{ verpachtung.get_status_display }}
                                        </span>
                                    </td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{% url 'stiftung:verpachtung_detail' verpachtung.pk %}" class="btn btn-sm btn-outline-primary">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{% url 'stiftung:verpachtung_update' verpachtung.pk %}" class="btn btn-sm btn-outline-warning">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="{% url 'stiftung:verpachtung_delete' verpachtung.pk %}" class="btn btn-sm btn-outline-danger">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    {% if page_obj.has_other_pages %}
                    <nav aria-label="Verpachtungen Pagination">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}{% if sort %}&sort={{ sort }}&dir={{ dir }}{% endif %}">
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                            {% if page_obj.number == num %}
                            <li class="page-item active">
                                <span class="page-link">{{ num }}</span>
                            </li>
                            {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ num }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}{% if sort %}&sort={{ sort }}&dir={{ dir }}{% endif %}">{{ num }}</a>
                            </li>
                            {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if gemeinde_filter %}&gemeinde={{ gemeinde_filter }}{% endif %}{% if sort %}&sort={{ sort }}&dir={{ dir }}{% endif %}">
                                    <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}

                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-handshake fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">Keine Verpachtungen gefunden</h5>
                        <p class="text-muted">Erstellen Sie Ihre erste Verpachtung oder passen Sie die Filter an.</p>
                        <a href="{% url 'stiftung:verpachtung_create' %}" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i>Neue Verpachtung erstellen
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

{% endblock %} 
