{% trans_default_domain 'admin' %}

{% extends "@FerienpassAdmin/layout/edit.html.twig" %}

{% set translationPrefix = app.request.get('_route')|split('_')[1] %}

{% block edit_page_header %}

    <twig:Admin:Offer:Header offer="{{ item }}" detailed="{{ true }}"/>

    {% if isCopy|default %}
        <twig:Admin:Alert type="info" class="mt-5">
            {{ 'offers.save_before_continuing'|trans(domain: 'admin') }}
        </twig:Admin:Alert>
    {% endif %}

    {% if is_granted('edit', item) and item.id and item.isVariant|default %}
        <twig:Admin:Alert type="info" icon="ph:git-branch" class="mt-5">
            Sie bearbeiten eine Variante von <a class="underline hover:no-underline"
                                                href="{{ path('admin_offers_edit', { uuid: item.variantBase.uuid.toBase58}) }}">
                Angebot ID {{ item.variantBase.id }}</a> und können deswegen nicht alle Felder
            bearbeiten.
        </twig:Admin:Alert>
    {% endif %}

    {% if isReadonly|default %}
        <twig:Admin:Alert type="info" icon="ph:lock" class="mt-5">
            Dieses Angebot kann nicht mehr bearbeitet werden.
        </twig:Admin:Alert>
    {% endif %}

{% endblock %}

{% block edit_page_header_actions %}
{% endblock %}

{% block edit_page_header_info %}
    {% for status in item.status|keys %}
        <twig:Admin:Badge offer="{{ status }}" size="xs" class="-ml-1"/>
    {% endfor %}

    <p>zuletzt geändert
        <time datetime="{{ item.modifiedAt|date('c') }}">{{ item.modifiedAt|ago }}</time>
    </p>
{% endblock %}

{% block edit_aside_nav_label %}

     {# Status icon sits to the LEFT of the section label (icon → label). #}
     {% if isReadonly|default %}
         <twig:ux:icon name="ph:lock" class="h-4 w-4 flex-none text-gray-500" />
     {% elseif formScreen.hasError(v) %}
         <twig:ux:icon name="ph:x-circle" class="w-4 h-4 flex-none text-red-600" />
     {% elseif formScreen.hasWarning(v) %}
         <twig:ux:icon name="ph:warning-circle" class="w-4 h-4 flex-none text-yellow-600" />
     {% else %}
         <twig:ux:icon name="ph:check-circle" class="text-green-500 w-4 h-4 flex-none" />
     {% endif %}

     {{ parent() }}

{% endblock %}

{% block edit_form %}

    {% if component_class is defined %}
        {% set component_name = component_class|split('\\')|last %}
        {{ component('Admin:Offer:' ~ component_name, { offer: item, sections: formScreen.sections, section: section, isReadonly: isReadonly|default(false) }) }}
    {% else %}
        <twig:Admin:Edit:Offer
                class="border-t border-gray-200 dark:border-gray-800 mt-6 pt-6"
                item="{{ item }}"
                form="{{ form }}"
                formOptions="{{ formOptions }}"
                isCopy="{{ isCopy|default(false) }}"
                sections="{{ formScreen.sections }}"
                section="{{ section }}"
        />
    {% endif %}

{% endblock %}

{% block edit_header %}

    {{ parent() }}

    {% set warnings = formScreen.warningMessages(section) %}
    {% if warnings is not empty %}
        <twig:Admin:Alert type="warning" class="mt-2 shadow border border-yellow-700/30">
            {% for message in warnings %}
                <span>{{ message }}</span>
            {% endfor %}
        </twig:Admin:Alert>
    {% endif %}

{% endblock %}

{% block success_stream %}

    <twig:Turbo:Stream:Prepend target="#notifications">
        <twig:Admin:Notification type="success" title="Daten gespeichert">
            Das Angebot <span class="font-medium">{{ item.name }}</span> wurde gespeichert.
        </twig:Admin:Notification>
    </twig:Turbo:Stream:Prepend>

    {% if reorder|default %}
        <twig:Turbo:Stream:Update target="#modal">
            <div>
                <div class="mx-auto flex size-12 items-center justify-center rounded-full bg-green-100 dark:bg-green-500/10">
                    <twig:ux:icon name="ph:check" aria-hidden="true" class="size-6 text-green-600 dark:text-green-400" />
                </div>
                <div class="mt-3 text-center sm:mt-5">
                    <h3 id="dialog-title" class="text-base font-semibold text-gray-900 dark:text-white">Warteliste
                        auffüllen?</h3>
                    <div class="mt-2">
                        <p class="text-sm text-gray-500 dark:text-gray-400">Es sind Plätze frei geworden. Sollen
                            Teilnehmer von der Warteliste automatisch nachrücken?</p>
                    </div>
                </div>
            </div>
            <div class="mt-5 sm:mt-6 sm:grid sm:grid-flow-row-dense sm:grid-cols-2 sm:gap-3">
                <form action="{{ path('admin_offers_reorder', {uuid: item.uuid.toBase58}) }}" method="post" data-turbo-frame="modal" class="sm:col-start-2">
                    <twig:Admin:Button variant="primary" type="submit" class="w-full justify-center">Teilnahmeliste auffüllen</twig:Admin:Button>
                </form>
                <twig:Admin:Button command="close" commandfor="admin-modal-dialog" class="mt-3 inline-flex w-full justify-center sm:col-start-1 sm:mt-0">Abbrechen</twig:Admin:Button>
            </div>
        </twig:Turbo:Stream:Update>
    {% endif %}

    {% if overbooked|default %}
        <twig:Turbo:Stream:Prepend target="#notifications">
            <twig:Admin:Notification type="error" title="Zu viele Teilnehmer" dismissAfter="0"
                                     primaryActionTitle="Plätze zuteilen"
                                     primaryActionLink="{{ path('admin_offer_assign', {uuid: item.uuid.toBase58}) }}">
                Es sind mehr Teilnehmer bestätigt, als Plätze vorhanden sind.
            </twig:Admin:Notification>
        </twig:Turbo:Stream:Prepend>
    {% endif %}

{% endblock %}

{% block reorder_success_stream %}
    <twig:Turbo:Stream:Update target="#modal"/>
    <twig:Turbo:Stream:Prepend target="#notifications">
        <twig:Admin:Notification type="mail_sent" title="{{ 'Teilnehmer:innen wurden benachrichtigt' }}"
                                 message="{{ 'Die Teilnehmer:innen wurden über die Änderung des Teilnahmestatus informiert' }}"
                                 dismissAfter="0"
                                 primaryActionLink="{{ path('admin_outbox', { group: notificationUuid }) }}"
                                 primaryActionTitle="{{ 'Nachrichten anzeigen' }}"/>
    </twig:Turbo:Stream:Prepend>
{% endblock %}

