{% extends "base.html" %}
{% block title %}Spenden{{ " - " + app_name() }}{% endblock %}

{% block content %}
<div class="page-header">
    <h1>Spenden <span style="font-size:0.9rem; color:var(--kw-text-muted); font-weight:normal;">({{ gesamt }})</span></h1>
    <div class="btn-group">
        <a href="/spenden/neu" role="button" class="btn-sm">+ Neue Spende</a>
        <a href="/spenden/bericht" role="button" class="btn-sm outline">Bericht</a>
    </div>
</div>

<form method="get" action="/spenden">
    <table class="data-table">
        <thead>
            <tr>
                <th>Datum</th>
                <th>Kontakt</th>
                <th style="text-align:right;">Betrag</th>
                <th>Kampagne</th>
                <th>Zweck</th>
                <th>Quittung</th>
                <th></th>
            </tr>
            <tr class="tbl-filter-row">
                <td>
                    <input type="number" name="jahr" value="{{ jahr_filter or '' }}" placeholder="Jahr" min="2000" max="2099">
                </td>
                <td>
                    <input type="text" name="kontakt_q" value="{{ kontakt_q or '' }}" placeholder="Kontakt suchen…">
                </td>
                <td></td>
                <td>
                    {% if alle_kampagnen %}
                    <select name="kampagne_id" onchange="this.form.submit()">
                        <option value="">Alle Kampagnen</option>
                        {% for k in alle_kampagnen %}
                        <option value="{{ k.id }}" {% if kampagne_filter == k.id %}selected{% endif %}>{{ k.titel }}</option>
                        {% endfor %}
                    </select>
                    {% endif %}
                </td>
                <td>
                    <input type="text" name="q" value="{{ q or '' }}" placeholder="Zweck suchen…">
                </td>
                <td>
                    <select name="quittung" onchange="this.form.submit()">
                        <option value="">Alle</option>
                        {% for opt in kat_optionen("spende_quittung") %}
                        <option value="{{ opt.wert }}" {% if quittung_filter == opt.wert %}selected{% endif %}>{{ opt.label }}</option>
                        {% endfor %}
                    </select>
                </td>
                <td>
                    {% if jahr_filter or quittung_filter or kampagne_filter or q or kontakt_q %}
                    <a href="/spenden" class="btn-sm outline" style="margin:0; font-size:0.7rem; padding:0.15rem 0.4rem; display:inline-block;" title="Zurücksetzen">✕</a>
                    {% endif %}
                </td>
            </tr>
        </thead>
        <tbody>
            {% for s in spenden %}
            <tr>
                <td style="white-space:nowrap;">{{ s.datum.strftime('%d.%m.%Y') if s.datum else '-' }}</td>
                <td><a href="/kontakte/{{ s.kontakt_id }}">{{ s.kontakt.anzeigename if s.kontakt else '-' }}</a></td>
                <td style="font-weight:600; text-align:right;">{{ '%.2f'|format(s.betrag) }} €</td>
                <td style="font-size:0.85rem;">
                    {% if s.kampagne %}<a href="/kampagnen/{{ s.kampagne_id }}">{{ s.kampagne.titel }}</a>
                    {% else %}–{% endif %}
                </td>
                <td style="font-size:0.85rem;">{{ s.zweck or '-' }}</td>
                <td>
                    {% if s.quittung == 'versendet' %}<span class="tag-chip" style="background:var(--kw-success); color:white;">versendet</span>
                    {% elif s.quittung == 'erstellt' %}<span class="tag-chip" style="background:#dd6b20; color:white;">erstellt</span>
                    {% else %}<span style="font-size:0.8rem; color:var(--kw-text-muted);">keine</span>{% endif %}
                </td>
                <td style="white-space:nowrap;">
                    <a href="/spenden/{{ s.id }}/bearbeiten" title="Bearbeiten" style="font-size:0.8rem;">✏️</a>
                    <a href="#" onclick="event.preventDefault(); if(confirm('Spende löschen?')) window.location='/spenden/{{ s.id }}/loeschen'" title="Löschen" style="font-size:0.8rem; margin-left:0.3rem;">🗑️</a>
                </td>
            </tr>
            {% else %}
            <tr><td colspan="7" style="text-align:center; padding:2rem; color:var(--kw-text-muted);">Keine Spenden {% if jahr_filter or quittung_filter or q or kontakt_q %}für diese Filter {% endif %}erfasst.</td></tr>
            {% endfor %}
        </tbody>
    </table>
</form>

{% set extra = '' %}
{% if jahr_filter %}{% set extra = extra + '&jahr=' + jahr_filter|string %}{% endif %}
{% if quittung_filter %}{% set extra = extra + '&quittung=' + quittung_filter %}{% endif %}
{% if q %}{% set extra = extra + '&q=' + q %}{% endif %}
{% if kontakt_q %}{% set extra = extra + '&kontakt_q=' + kontakt_q %}{% endif %}
{% if kampagne_filter %}{% set extra = extra + '&kampagne_id=' + kampagne_filter|string %}{% endif %}
{% with base_url='/spenden', extra_params=extra %}
{% include "components/pagination.html" %}
{% endwith %}
{% endblock %}
