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

{% block title %}{{ title }} - Stiftungsverwaltung{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3">
                    <i class="fas fa-calendar-alt text-primary me-2"></i>
                    {{ title }}
                </h1>
                <a href="{% url 'stiftung:veranstaltung_list' %}" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-2"></i>Zurück zur Liste
                </a>
            </div>
        </div>
    </div>

    <form method="post" novalidate>
        {% csrf_token %}

        {% if form.non_field_errors %}
        <div class="alert alert-danger">
            <ul class="mb-0">
                {% for error in form.non_field_errors %}
                <li>{{ error }}</li>
                {% endfor %}
            </ul>
        </div>
        {% endif %}

        <div class="row g-4">
            <div class="col-lg-8">
                <!-- Grunddaten -->
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-dark text-white">
                        <i class="fas fa-info-circle me-2"></i>Grunddaten
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12 mb-3">
                                <label for="{{ form.titel.id_for_label }}" class="form-label">{{ form.titel.label }} *</label>
                                {{ form.titel }}
                                {% if form.titel.errors %}<div class="invalid-feedback d-block">{{ form.titel.errors.0 }}</div>{% endif %}
                            </div>
                            <div class="col-md-4 mb-3">
                                <label for="{{ form.datum.id_for_label }}" class="form-label">{{ form.datum.label }} *</label>
                                {{ form.datum }}
                                {% if form.datum.errors %}<div class="invalid-feedback d-block">{{ form.datum.errors.0 }}</div>{% endif %}
                            </div>
                            <div class="col-md-4 mb-3">
                                <label for="{{ form.uhrzeit.id_for_label }}" class="form-label">{{ form.uhrzeit.label }}</label>
                                {{ form.uhrzeit }}
                                {% if form.uhrzeit.errors %}<div class="invalid-feedback d-block">{{ form.uhrzeit.errors.0 }}</div>{% endif %}
                            </div>
                            <div class="col-md-4 mb-3">
                                <label for="{{ form.status.id_for_label }}" class="form-label">{{ form.status.label }}</label>
                                {{ form.status }}
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.ort.id_for_label }}" class="form-label">{{ form.ort.label }} *</label>
                                {{ form.ort }}
                                {% if form.ort.errors %}<div class="invalid-feedback d-block">{{ form.ort.errors.0 }}</div>{% endif %}
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.budget_pro_person.id_for_label }}" class="form-label">{{ form.budget_pro_person.label }}</label>
                                {{ form.budget_pro_person }}
                            </div>
                            <div class="col-12 mb-3">
                                <label for="{{ form.adresse.id_for_label }}" class="form-label">{{ form.adresse.label }}</label>
                                {{ form.adresse }}
                            </div>
                            <div class="col-12 mb-3">
                                <label for="{{ form.beschreibung.id_for_label }}" class="form-label">{{ form.beschreibung.label }}</label>
                                {{ form.beschreibung }}
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Serienbrief-Vorlage -->
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-dark text-white">
                        <i class="fas fa-envelope-open-text me-2"></i>Serienbrief – Vorlage
                    </div>
                    <div class="card-body">
                        <div class="col-12 mb-3">
                            <label for="{{ form.betreff.id_for_label }}" class="form-label">{{ form.betreff.label }}</label>
                            {{ form.betreff }}
                            <div class="form-text">{{ form.betreff.help_text }}</div>
                        </div>
                        <div class="col-12 mb-3">
                            <label for="{{ form.briefvorlage.id_for_label }}" class="form-label">{{ form.briefvorlage.label }}</label>
                            {{ form.briefvorlage }}
                            {% if form.briefvorlage.errors %}<div class="invalid-feedback d-block">{{ form.briefvorlage.errors.0 }}</div>{% endif %}
                            <div class="form-text">{{ form.briefvorlage.help_text }}</div>
                        </div>
                    </div>
                </div>

                <!-- Unterschriften -->
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-dark text-white">
                        <i class="fas fa-signature me-2"></i>Serienbrief – Unterschriften
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.unterschrift_1_name.id_for_label }}" class="form-label">{{ form.unterschrift_1_name.label }}</label>
                                {{ form.unterschrift_1_name }}
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.unterschrift_1_titel.id_for_label }}" class="form-label">{{ form.unterschrift_1_titel.label }}</label>
                                {{ form.unterschrift_1_titel }}
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.unterschrift_2_name.id_for_label }}" class="form-label">{{ form.unterschrift_2_name.label }}</label>
                                {{ form.unterschrift_2_name }}
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.unterschrift_2_titel.id_for_label }}" class="form-label">{{ form.unterschrift_2_titel.label }}</label>
                                {{ form.unterschrift_2_titel }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="col-lg-4">
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-dark text-white">
                        <i class="fas fa-question-circle me-2"></i>Platzhalter-Hilfe
                    </div>
                    <div class="card-body">
                        <p class="small text-muted mb-2">Verfügbare Platzhalter für die Briefvorlage:</p>
                        <table class="table table-sm small">
                            <tbody>
                                <tr><td class="font-monospace text-danger">{{ anrede }}</td><td>Herr / Frau</td></tr>
                                <tr><td class="font-monospace text-danger">{{ vorname }}</td><td>Vorname</td></tr>
                                <tr><td class="font-monospace text-danger">{{ nachname }}</td><td>Nachname</td></tr>
                                <tr><td class="font-monospace text-danger">{{ strasse }}</td><td>Straße + Nr.</td></tr>
                                <tr><td class="font-monospace text-danger">{{ plz }}</td><td>PLZ</td></tr>
                                <tr><td class="font-monospace text-danger">{{ ort }}</td><td>Wohnort</td></tr>
                                <tr><td class="font-monospace text-danger">{{ datum }}</td><td>Veranstaltungsdatum</td></tr>
                                <tr><td class="font-monospace text-danger">{{ uhrzeit }}</td><td>Uhrzeit</td></tr>
                                <tr><td class="font-monospace text-danger">{{ veranstaltungsort }}</td><td>Gasthaus / Ort</td></tr>
                                <tr><td class="font-monospace text-danger">{{ gasthaus_adresse }}</td><td>Adresse Gasthaus</td></tr>
                            </tbody>
                        </table>
                        <p class="small text-muted mb-0">Platzhalter werden beim PDF-Export automatisch befüllt.</p>
                    </div>
                </div>

                {% if veranstaltung %}
                <div class="card shadow-sm">
                    <div class="card-header bg-dark text-white">
                        <i class="fas fa-tools me-2"></i>Aktionen
                    </div>
                    <div class="card-body d-flex flex-column gap-2">
                        <a href="{% url 'stiftung:veranstaltung_serienbrief_vorschau' veranstaltung.pk %}"
                           class="btn btn-outline-primary w-100" target="_blank">
                            <i class="fas fa-eye me-2"></i>Serienbrief-Vorschau
                        </a>
                        <a href="{% url 'stiftung:veranstaltung_serienbrief_pdf' veranstaltung.pk %}"
                           class="btn btn-outline-success w-100">
                            <i class="fas fa-file-pdf me-2"></i>Serienbrief-PDF
                        </a>
                        <hr>
                        <a href="{% url 'stiftung:veranstaltung_delete' veranstaltung.pk %}"
                           class="btn btn-outline-danger w-100">
                            <i class="fas fa-trash me-2"></i>Veranstaltung löschen
                        </a>
                    </div>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- Submit -->
        <div class="row mt-3">
            <div class="col-lg-8">
                <hr>
                <div class="d-flex justify-content-between">
                    <a href="{% url 'stiftung:veranstaltung_list' %}" class="btn btn-secondary">
                        <i class="fas fa-times me-2"></i>Abbrechen
                    </a>
                    <button type="submit" class="btn btn-success">
                        <i class="fas fa-save me-2"></i>
                        {% if veranstaltung %}Aktualisieren{% else %}Erstellen{% endif %}
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>
{% endblock %}
