{% extends "base.html" %}
{% block title %}{{ 'Spende bearbeiten' if spende else 'Neue Spende' }}{{ " - " + app_name() }}{% endblock %}

{% block content %}
<div class="page-header">
    <h1>{% if spende %}Spende bearbeiten{% else %}Neue Spende{% endif %}</h1>
    <div class="btn-group">
        <a href="#" role="button" class="btn-sm outline" onclick="event.preventDefault(); window.history.back();">Abbrechen</a>
        <button type="submit" form="spende-form" class="btn-sm">Speichern</button>
    </div>
</div>

<form id="spende-form" method="post" action="{% if spende %}/spenden/{{ spende.id }}/bearbeiten{% else %}/spenden/neu{% endif %}">
    <input type="hidden" name="next" value="{{ request.headers.get('referer', '') }}">
    <div class="card">
        <div class="form-grid">
            <div>
                <label for="kontakt_id">Kontakt *</label>
                <select id="kontakt_id" name="kontakt_id" required>
                    <option value="">Bitte wählen</option>
                    {% for k in kontakte %}
                    <option value="{{ k.id }}" {% if (spende and spende.kontakt_id == k.id) or (kontakt_id is defined and kontakt_id == k.id) %}selected{% endif %}>{{ k.anzeigename }}</option>
                    {% endfor %}
                </select>
            </div>
            <div>
                <label for="betrag">Betrag (EUR) *</label>
                <input type="number" id="betrag" name="betrag" step="0.01" value="{{ spende.betrag if spende else '' }}" required>
            </div>
            <div>
                <label for="datum">Datum *</label>
                <input type="date" id="datum" name="datum" value="{{ spende.datum.strftime('%Y-%m-%d') if spende and spende.datum else '' }}" required>
            </div>
            <div>
                <label for="typ">Typ</label>
                <select id="typ" name="typ">
                    {% for opt in kat_optionen("spende_typ") %}
                    <option value="{{ opt.wert }}" {% if spende and spende.typ == opt.wert %}selected{% endif %}>{{ opt.label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div>
                <label for="zweck">Zweck</label>
                <input type="text" id="zweck" name="zweck" value="{{ spende.zweck if spende else '' }}">
            </div>
            <div>
                <label for="projekt">Projekt</label>
                <input type="text" id="projekt" name="projekt" value="{{ spende.projekt if spende else '' }}">
            </div>
            {% if alle_kampagnen is defined and alle_kampagnen %}
            <div>
                <label for="kampagne_id">Kampagne</label>
                <select id="kampagne_id" name="kampagne_id">
                    <option value="">– Keine Kampagne –</option>
                    {% for k in alle_kampagnen %}
                    <option value="{{ k.id }}" {% if spende and spende.kampagne_id == k.id %}selected{% endif %}>{{ k.titel }}</option>
                    {% endfor %}
                </select>
            </div>
            {% endif %}
            <div>
                <label for="zahlungsart">Zahlungsart</label>
                <select id="zahlungsart" name="zahlungsart">
                    {% for opt in kat_optionen("spende_zahlungsart") %}
                    <option value="{{ opt.wert }}" {% if spende and spende.zahlungsart == opt.wert %}selected{% endif %}>{{ opt.label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div>
                <label for="quittung">Quittung</label>
                <select id="quittung" name="quittung">
                    {% for opt in kat_optionen("spende_quittung") %}
                    <option value="{{ opt.wert }}" {% if spende and spende.quittung == opt.wert %}selected{% endif %}>{{ opt.label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="full-width">
                <label for="bemerkung">Bemerkung</label>
                <textarea id="bemerkung" name="bemerkung" rows="3">{{ spende.bemerkung if spende else '' }}</textarea>
            </div>
        </div>
    </div>
    <div class="btn-group" style="margin-top: 1rem;">
        <button type="submit">💾 Speichern</button>
        <a href="{{ request.headers.get('referer', '/spenden') }}" role="button" class="outline">Abbrechen</a>
    </div>
</form>
{% endblock %}
