{% import "macros.j2" as macros %}
{% macro teamEntries(event, work_type) -%}
    {% for entry in event.team | selectattr("work_type", "equalto", work_type)%}
    <div class="d-inline-flex align-items-center bg-light border rounded-pill px-3 py-1 m-1">
    <span class="me-2">{{ entry.person_name }}</span>
    {% if user -%}
    <button type="button" class="btn btn-sm p-0 border-0 bg-transparent text-muted">
        <a href="/event/{{event.id}}/register_team/{{entry.id}}/delete">
            <i class="bi bi-trash"></i>
        </a>
    </button>
    {% endif -%}
    </div>
    {% endfor %}
{%- endmacro %}

{% extends "base.html" %}
{% block content %}
<p class="h1">{{ event.title }}</p>
<p class="text-muted">{{ event.event_time.strftime('%A, %d.%m.%Y %H:%M Uhr') }}</p>
{% if event.organizer_name %}<p>Organisiert von {{ event.organizer_name }}</p>{% endif %}
<p>{{ event.description }}</p>
<hr class="hr"/>
{% if message %}
<div class="alert alert-info alert-dismissible fade show" role="alert">
    <i class="bi bi-info-circle me-2"></i>
    {{ message }}
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
<div class="container">

    <p class="h3">Anmeldungen</p>
    {% if not user %}
    <div class="alert alert-warning m-2">
        <i class="bi bi-info-circle me-2"></i>
        Das Löschen von Anmeldungen und Dienstanmeldungen ist nur möglich, wenn du in der Allmende-Cloud eingeloggt bist. <br/> Geht dazu auf <a href="https://cloud.allmende-gufi.de" target="_blank">allmende.cloud</a> und melde dich an.
    </div>
    {% endif %}
    <div class="row m-2">
        <div class="col-md-4 d-flex justify-content-center align-items-center flex-column">
            <p class="text-muted w-100 mb-2">Anmeldung schließt {{ event.registration_deadline.strftime('%A, %d.%m.%Y, %H:%M Uhr') }}</p>
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary mb-2 w-100" {% if not (event.registration_open or (user and user.admin)) %}disabled{% endif%} data-bs-toggle="modal" data-bs-target="#registration">
                Anmeldung hinzufügen
            </button>
            <button type="button" class="btn btn-primary mb-2 w-100" {% if event.all_teams_max() or event.in_the_past %}disabled{% endif %} data-bs-toggle="modal" data-bs-target="#teamRegistration">
                Dienst übernehmen
            </button>
            {% if event.recipe_link %}
            <a href="{{ event.recipe_link }}" class="btn btn-outline-primary mb-2 w-100" target="_blank">
                <i class="bi bi-book"></i> Original Rezept ansehen
            </a>
            {% endif %}
            <div class="row w-100">
                <div class="col-6 p-1">
                    <a href="/event/{{event.id}}/pdf" class="btn btn-secondary w-100" target="_blank">
                        <i class="bi bi-printer m-2"></i> Druckansicht
                    </a>
                </div>
                <div class="col-6 p-1">
                    {% if user -%}
                    <a href="/event/{{event.id}}/edit" class="btn btn-secondary w-100">
                        <i class="bi bi-pen m-2"></i> Event bearbeiten
                    </a>
                    {% else -%}
                    <button type="button" class="btn btn-secondary w-100" data-bs-toggle="modal" data-bs-target="#loginInfo">
                        <i class="bi bi-pen m-2"></i> Event bearbeiten
                    </button>
                    {% endif -%}
                </div>
            </div>
            {% if user and user.admin %}
            <div class="row w-100">
                <div class="col-6 p-1">
                    <button type="button" class="btn btn-danger w-100" data-bs-toggle="modal" data-bs-target="#deleteEvent">
                        Event Löschen
                    </button>
                </div>
                <div class="col-6 p-1">
                    <a href="/event/{{event.id}}/sync_with_grist" class="btn btn-secondary w-100 {% if event.exclude_from_billing %}disabled{% endif %}">
                        <i class="bi bi-cash-coin m-2"></i> Abrechnen
                    </a>
                </div>
            </div>


            {% endif %}
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Dienste</h5>
                    <div class="row">
                        <div class="col-md-4">
                            Kochen: {% if event.team_min_reached("cooking") %}✅{% endif %}
                        </div>
                        <div class="col-md-8">
                            {{ teamEntries(event, "cooking") }}
                        </div>
                    </div>
                    <hr/>
                    <div class="row">
                        <div class="col-md-4">
                            Spülen: {% if event.team_min_reached("dishes") %}✅{% endif %}
                        </div>
                        <div class="col-md-8">
                            {{ teamEntries(event, "dishes") }}
                        </div>
                    </div>
                    <hr/>
                    <div class="row">
                        <div class="col-md-4">
                            Tische vorbereiten: {% if event.team_min_reached("tables") %}✅{% endif %}
                        </div>
                        <div class="col-md-8">
                            {{ teamEntries(event, "tables") }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Zusammenfassung</h5>
                    <table class="table table-sm">
                        <tr>
                            <td>Erwachsene:</td>
                            <td>{{ event.registrations | sum(attribute="num_adult_meals") }}</td>
                        </tr>
                        <tr>
                            <td>Kinder:</td>
                            <td>{{ event.registrations | sum(attribute="num_children_meals") }}</td>
                        </tr>
                        <tr>
                            <td>Kleinkinder:</td>
                            <td>{{ event.registrations | sum(attribute="num_small_children_meals") }}</td>
                        </tr>
                        <tr>
                            <th>Gesamt:</th>
                            <th>{{ event.registrations | sum(attribute="num_adult_meals") + event.registrations | sum(attribute="num_children_meals") +event.registrations | sum(attribute="num_small_children_meals")}} </th>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Desktop table view -->
    <div class="d-none d-md-block">
        <table class="table">
            <thead>
            <tr>
                <th scope="col">Haushalt</th>
                <th scope="col">Erwachsene</th>
                <th scope="col">Kinder</th>
                <th scope="col">Kleinkinder</th>
                <th scope="col">Kommentar</th>
                {% if user %}<th scope="col">Löschen</th>{% endif %}
            </tr>
            </thead>
            <tbody>
            {% for reg in event.registrations %}
            <tr>
                <td>{{ reg.household.name }}</td>
                <td>{{ reg.num_adult_meals }}</td>
                <td>{{ reg.num_children_meals }}</td>
                <td>{{ reg.num_small_children_meals }}</td>
                <td>{% if reg.comment %}{{ reg.comment }}{% endif %}</td>
                {% if user %}<td><a href="/event/{{event.id}}/registration/{{reg.household_id}}/delete"><i class="bi bi-trash"></i></a></td>{% endif %}
            </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Mobile card view -->
    <div class="d-md-none">
        {% for reg in event.registrations %}
        <div class="card mb-3">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-2">
                    <h5 class="card-title mb-0">{{ reg.household.name }}</h5>
                    {% if user %}<a href="/event/{{event.id}}/registration/{{reg.household_id}}/delete" class="text-danger">
                        <i class="bi bi-trash"></i>
                    </a>{% endif -%}
                </div>
                <div class="row mt-3">
                    <div class="col-3 text-center">
                        <div class="text-muted small">Erwachsene</div>
                        <div class="fw-bold">{{ reg.num_adult_meals }}</div>
                    </div>
                    <div class="col-3 text-center">
                        <div class="text-muted small">Kinder</div>
                        <div class="fw-bold">{{ reg.num_children_meals }}</div>
                    </div>
                    <div class="col-3 text-center">
                        <div class="text-muted small">Kleinkinder</div>
                        <div class="fw-bold">{{ reg.num_small_children_meals }}</div>
                    </div>
                    <div class="col-3 text-center">
                        <div class="text-muted small">Kommentar</div>
                        <div class="small">{{ reg.comment }}</div>
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
    </div>
<!-- Modal -->
<div class="modal fade" id="registration" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
     aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="staticBackdropLabel">Anmeldung hinzufügen</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="/event/{{event.id}}/register" method="POST">
                <div class="modal-body">
                    <p>Wenn dein Haushalt nicht auswählbar ist, existiert schon eine Anmeldung. Wenn du die Anmeldung ändern willst, lösche die bestehende Anmeldung und lege eine neue an.</p>
                    <div class="mb-3">
                        <select name="household" class="form-select" aria-label="Multiple select example" required>
                            <option value="" disabled selected hidden>Wer?</option>
                            {% for household in households %}
                            <option value="{{household.id}}">{{household.name}}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="row">
                        <div class="col">
                            <label for="InputAdults" class="form-label">Anzahl Erwachsene</label>
                            <input name="numAdults" id="InputAdults" type="number" class="form-control"
                                   aria-label="Anzahl Erwachsene" min="0" step="1" inputmode="numeric">
                        </div>
                        <div class="col">
                            <label for="InputKids" class="form-label">Anzahl Kinder >7 </label>
                            <input name="numKids" id="InputKids" type="number" class="form-control"
                                   aria-label="Anzahl Kinder >7" min="0" step="1" inputmode="numeric">
                        </div>
                        <div class="col">
                            <label for="InputSmallKids" class="form-label">Anzahl Kinder <7 </label>
                            <input name="numSmallKids" id="InputSmallKids" type="number" class="form-control"
                                   aria-label="Anzahl Kinder <7" min="0" step="1" inputmode="numeric">
                        </div>
                        <div class="mb-3 mt-3">
                            <label for="InputComment" class="form-label">Kommentar</label>
                            <input name="comment" id="InputComment" class="form-control"
                                  aria-label="Kommentar">
                    </div>

                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Anmelden</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Team Modal -->
<div class="modal fade" id="teamRegistration" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
     aria-labelledby="teamRegistrationLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="teamRegistrationLabel">Dienst übernehmen</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="/event/{{event.id}}/register_team" method="POST">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <label for="personName" class="form-label">Name</label>
                            <input name="personName" id="personName" type="text" class="form-control"
                                   aria-label="Name" list="people">
                        </div>
                        <datalist id="people">
                            {% for household in households %}
                            {% for person in household.name.split(",") %}
                            <option value="{{ person.strip() }}">
                            {% endfor %}
                            {% endfor %}
                        </datalist>
                        <div class="col-md-6">
                            <label for="workType" class="form-label">Dienst-Art</label>
                            <select id="workType" name="workType" class="form-select" aria-label="Multiple select example">
                                {% if not event.team_max_reached("cooking") %}<option value="cooking">Kochen</option>{% endif %}
                                {% if not event.team_max_reached("dishes") %}<option value="dishes">Spülen</option>{% endif %}
                                {% if not event.team_max_reached("tables") %}<option value="tables">Tische vorbereiten</option>{% endif %}
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Anmelden</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Delete Event Modal -->
<div class="modal fade" id="deleteEvent" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
     aria-labelledby="deleteEventLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h1 class="modal-title fs-5" id="deleteEventLabel">Event endgültig löschen?</h1>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"
                        aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-danger">
                    <i class="bi bi-exclamation-triangle-fill me-2"></i>
                    Diese Aktion kann nicht rückgängig gemacht werden! Alle Anmeldungen und Dienstanmeldungen werden
                    unwiderruflich gelöscht.
                </div>
            </div>
            <div class="modal-footer">
                <a href="/event/{{event.id}}/delete" class="btn btn-danger">Unwiderruflich Löschen</a>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
            </div>
        </div>
    </div>
</div>

{{ macros.login_info_modal() }}

{% endblock %}