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

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

            <!-- Filters -->
            <div class="card shadow mb-4">
                <div class="card-header">
                    <h6 class="mb-0">
                        <i class="fas fa-filter me-2"></i>Filter und Suche
                    </h6>
                </div>
                <div class="card-body">
                    <form method="get" class="row g-3">
                        <div class="col-md-4">
                            <label for="search" class="form-label">Suche</label>
                            <input type="text" class="form-control" id="search" name="search" 
                                   value="{{ search_query }}" 
                                   placeholder="Vertragsnummer, Land, Pächter...">
                        </div>
                        <div class="col-md-3">
                            <label for="status" class="form-label">Status</label>
                            <select class="form-select" id="status" name="status">
                                <option value="">Alle Status</option>
                                {% for value, label in status_choices %}
                                    <option value="{{ value }}" {% if status_filter == value %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label for="year" class="form-label">Jahr</label>
                            <input type="number" class="form-control" id="year" name="year" 
                                   value="{{ year_filter }}" 
                                   placeholder="2024" min="1900" max="2100">
                        </div>
                        <div class="col-md-3 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 shadow">
                <div class="card-header">
                    <h6 class="mb-0">
                        <i class="fas fa-list me-2"></i>Verpachtungen ({{ page_obj.paginator.count }})
                    </h6>
                </div>
                <div class="card-body">
                    {% if page_obj %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>Vertragsnummer</th>
                                    <th>Land</th>
                                    <th>Pächter</th>
                                    <th>Pachtzeit</th>
                                    <th>Fläche</th>
                                    <th>Pachtzins</th>
                                    <th>Status</th>
                                    <th width="120">Aktionen</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for verpachtung in page_obj %}
                                <tr>
                                    <td>
                                        <a href="{% url 'stiftung:verpachtung_detail' verpachtung.pk %}" 
                                           class="text-decoration-none fw-bold">
                                            {{ verpachtung.vertragsnummer }}
                                        </a>
                                    </td>
                                    <td>
                                        <a href="{% url 'stiftung:land_detail' verpachtung.land.pk %}" 
                                           class="text-decoration-none">
                                            {{ verpachtung.land.bezeichnung }}
                                        </a>
                                        {% if verpachtung.land.flur %}
                                        <br><small class="text-muted">Flur {{ verpachtung.land.flur }}</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <a href="{% url 'stiftung:paechter_detail' verpachtung.paechter.pk %}" 
                                           class="text-decoration-none">
                                            {{ verpachtung.paechter.get_full_name }}
                                        </a>
                                        {% if verpachtung.paechter.ort %}
                                        <br><small class="text-muted">{{ verpachtung.paechter.ort }}</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {{ verpachtung.pachtbeginn|date:"d.m.Y" }}
                                        {% if verpachtung.pachtende %}
                                        <br><small class="text-muted">bis {{ verpachtung.pachtende|date:"d.m.Y" }}</small>
                                        {% else %}
                                        <br><small class="text-success">Unbefristet</small>
                                        {% endif %}
                                        {% if verpachtung.verlaengerung_klausel %}
                                        <br><small class="badge bg-info">Auto-Verlängerung</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {{ verpachtung.verpachtete_flaeche|floatformat:0 }} qm
                                        <br><small class="text-muted">({{ verpachtung.verpachtete_flaeche_hektar|floatformat:2 }} ha)</small>
                                    </td>
                                    <td>
                                        <strong class="text-success">€{{ verpachtung.pachtzins_pauschal|floatformat:2 }}</strong>
                                        {% if verpachtung.pachtzins_pro_ha %}
                                        <br><small class="text-muted">€{{ verpachtung.pachtzins_pro_ha|floatformat:2 }}/ha</small>
                                        {% endif %}
                                        {% if verpachtung.ust_option %}
                                        <br><small class="badge bg-warning">+ {{ verpachtung.ust_satz }}% USt</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if verpachtung.status == 'aktiv' %}
                                            {% if verpachtung.is_aktiv %}
                                                <span class="badge bg-success">Aktiv</span>
                                            {% else %}
                                                <span class="badge bg-warning">Inaktiv</span>
                                            {% endif %}
                                        {% elif verpachtung.status == 'beendet' %}
                                            <span class="badge bg-secondary">Beendet</span>
                                        {% elif verpachtung.status == 'gekuendigt' %}
                                            <span class="badge bg-danger">Gekündigt</span>
                                        {% elif verpachtung.status == 'verlaengert' %}
                                            <span class="badge bg-info">Verlängert</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{{ verpachtung.get_status_display }}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{% url 'stiftung:verpachtung_detail' verpachtung.pk %}" 
                                               class="btn btn-sm btn-outline-primary" title="Details">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{% url 'stiftung:verpachtung_update' verpachtung.pk %}" 
                                               class="btn btn-sm btn-outline-warning" title="Bearbeiten">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="{% url 'stiftung:verpachtung_delete' verpachtung.pk %}" 
                                               class="btn btn-sm btn-outline-danger" title="Löschen">
                                                <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=1{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if year_filter %}&year={{ year_filter }}{% endif %}">
                                    <i class="fas fa-angle-double-left"></i>
                                </a>
                            </li>
                            <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 year_filter %}&year={{ year_filter }}{% endif %}">
                                    <i class="fas fa-angle-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 year_filter %}&year={{ year_filter }}{% 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 year_filter %}&year={{ year_filter }}{% endif %}">
                                    <i class="fas fa-angle-right"></i>
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if year_filter %}&year={{ year_filter }}{% endif %}">
                                    <i class="fas fa-angle-double-right"></i>
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}

                    {% else %}
                    <div class="text-center py-4">
                        <i class="fas fa-handshake fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">Keine Verpachtungen gefunden</h5>
                        <p class="text-muted">
                            {% if search_query or status_filter or year_filter %}
                                Keine Verpachtungen entsprechen den angegebenen Filterkriterien.
                            {% else %}
                                Es sind noch keine Verpachtungen erfasst.
                            {% endif %}
                        </p>
                        <a href="{% url 'stiftung:verpachtung_create' %}" class="btn btn-success">
                            <i class="fas fa-plus me-2"></i>Erste Verpachtung erstellen
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

{% endblock %}