{% extends "@FerienpassAdmin/page/tools/_settings.html.twig" %}
{% trans_default_domain 'admin' %}

{% block main %}

    <div>
        <div class="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between border-b border-gray-200 dark:border-gray-800 pb-5">
            <div class="min-w-0">
                <h2 class="text-base/7 font-semibold text-gray-900 dark:text-white">{{ 'tenants.title'|trans }}</h2>
                <p class="mt-1 text-sm/6 text-gray-500 dark:text-gray-400 max-w-2xl">Jeder Mandant ist ein eigener Datenraum mit eigenen Domains. Ein Host, den kein Mandant beansprucht, antwortet mit 404 — es gibt keinen Auffang-Mandanten.</p>
            </div>
            <div class="shrink-0">
                <twig:Admin:Button href="{{ createUrl }}" variant="primary">
                    <span class="flex items-center space-x-1">
                        <span class="h-5 w-5"><twig:ux:icon name="ph:plus"/></span>
                        <span>Mandant anlegen</span>
                    </span>
                </twig:Admin:Button>
            </div>
        </div>

        {% if withoutRootPage is not empty %}
            <div class="mt-6">
                <twig:Admin:Alert type="warning">
                    <strong>Ohne Startpunkt: {{ withoutRootPage|join(', ') }}.</strong>
                    Jeder Mandant braucht genau einen Webseitenstartpunkt in der Seitenstruktur — alles darunter
                    ist seine Website. Legen Sie ihn im Contao-Backend an und wählen Sie dort den Mandanten aus.
                </twig:Admin:Alert>
            </div>
        {% endif %}

        <table class="min-w-full">
            <thead>
            <tr>
                <th scope="col" class="py-2.5 pr-3 text-left text-xs font-semibold text-gray-500 dark:text-gray-400">Mandant</th>
                <th scope="col" class="px-3 py-2.5 text-left text-xs font-semibold text-gray-500 dark:text-gray-400">{{ 'tenants.th.hosts'|trans }}</th>
                <th scope="col" class="px-3 py-2.5 text-left text-xs font-semibold text-gray-500 dark:text-gray-400">{{ 'tenants.th.admin'|trans }}</th>
                <th scope="col" class="px-3 py-2.5 text-left text-xs font-semibold text-gray-500 dark:text-gray-400">Bestand</th>
                <th scope="col" class="py-2.5 pl-3"><span class="sr-only">Aktionen</span></th>
            </tr>
            </thead>
            <tbody class="divide-y divide-gray-200 dark:divide-gray-800 border-t border-gray-200 dark:border-gray-800">
            {% for tenant in tenants %}
                {% set counts = inventory[tenant.id]|default({}) %}
                {% set isOwn = ownTenant and ownTenant.id == tenant.id %}
                {% set isSwitched = switchedTenant and switchedTenant.id == tenant.id %}
                <tr id="tr-{{ tenant.id }}" {% if tenant.disabled %}class="opacity-60"{% endif %}>
                    <td class="py-4 pr-3 align-top">
                        <div class="flex flex-wrap items-center gap-2">
                            <a href="{{ path('admin_tenants_edit', { uuid: tenant.uuid.toBase58 }) }}" class="text-sm font-semibold text-gray-900 hover:text-primary-600 dark:text-gray-100 dark:hover:text-primary-400">{{ tenant.name }}</a>
                            {% if isOwn %}
                                <twig:Admin:Badge color="gray" content="Eigene"/>
                            {% endif %}
                            {% if isSwitched %}
                                <span class="inline-flex items-center gap-x-1.5 rounded-full bg-gray-900 px-2.5 py-0.5 text-xs font-semibold text-white dark:bg-gray-100 dark:text-gray-900">
                                    <span class="size-1.5 rounded-full bg-amber-400" aria-hidden="true"></span>
                                    Aktiver Datenraum
                                </span>
                            {% endif %}
                            {% if tenant.disabled %}
                                <twig:Admin:Badge color="red" content="Offline" :dot="true" :boxed="true"/>
                            {% endif %}
                        </div>
                        <span class="mt-1 block font-mono text-xs text-gray-400 dark:text-gray-500">{{ tenant.slug }}</span>
                    </td>
                    <td class="px-3 py-4 align-top">
                        {% for host in tenant.frontendHosts %}
                            <div class="font-mono text-xs/6 text-gray-700 dark:text-gray-300">{{ host }}</div>
                        {% else %}
                            <span class="text-sm text-gray-400">{{ 'tenants.hosts.none'|trans }}</span>
                        {% endfor %}
                    </td>
                    {# A tenant without an admin domain of its own is administered under
                       /admin on its public site, which is what the list should read as. #}
                    <td class="px-3 py-4 align-top">
                        {% for host in tenant.hasDedicatedAdminHost ? tenant.adminHosts : tenant.frontendHosts|map(h => h ~ '/admin') %}
                            <div class="font-mono text-xs/6 text-gray-700 dark:text-gray-300">{{ host }}</div>
                        {% else %}
                            <span class="text-sm text-gray-400">{{ 'tenants.hosts.none'|trans }}</span>
                        {% endfor %}
                    </td>
                    <td class="px-3 py-4 align-top whitespace-nowrap">
                        {# Contao's own `format_number` shadows twig/intl-extra's and pads
                           to one decimal by default — these are row counts, so force 0. #}
                        <div class="text-sm text-gray-700 dark:text-gray-300">{{ counts.participants|default(0)|format_number(0) }} Teilnehmende</div>
                        <div class="mt-0.5 text-xs text-gray-400 dark:text-gray-500">{{ counts.offers|default(0)|format_number(0) }} Angebote · {{ counts.editions|default(0)|format_number(0) }} Saisons</div>
                    </td>
                    <td class="py-4 pl-3 align-top text-right whitespace-nowrap">
                        <div class="flex items-center justify-end gap-2">
                            <twig:Admin:Button href="{{ path('admin_tenants_edit', { uuid: tenant.uuid.toBase58 }) }}" variant="secondary" size="sm">Stammdaten</twig:Admin:Button>
                            {% if not isOwn and not isSwitched %}
                                <form action="{{ path('admin_tenants_switch', { uuid: tenant.uuid.toBase58 }) }}" method="post" class="inline-flex">
                                    <twig:Admin:Button type="submit" variant="secondary" size="sm">
                                        <span class="flex items-center gap-x-1 text-primary-600 dark:text-primary-400">
                                            Wechseln
                                            <span class="size-4"><twig:ux:icon name="ph:arrow-right"/></span>
                                        </span>
                                    </twig:Admin:Button>
                                </form>
                            {% endif %}
                        </div>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>

        <div class="mt-6 flex max-w-2xl items-start gap-x-2.5 rounded-lg border border-gray-200 bg-gray-50 px-4 py-3 text-sm text-gray-600 dark:border-gray-800 dark:bg-gray-900 dark:text-gray-400">
            <span class="mt-0.5 size-4 shrink-0 text-gray-400"><twig:ux:icon name="ph:info"/></span>
            <p><strong class="font-semibold text-gray-900 dark:text-gray-100">„Wechseln“ öffnet die Verwaltung im Datenraum dieses Mandanten.</strong> Der gesamte Rahmen färbt sich um und bleibt so, bis Sie den Datenraum wieder verlassen.</p>
        </div>

        <div class="mt-10 border-t border-gray-200 pt-6 dark:border-gray-800">
            <h3 class="text-sm font-semibold text-gray-900 dark:text-gray-100">Mandant löschen</h3>
            <p class="mt-1 max-w-2xl text-sm text-gray-500 dark:text-gray-400">
                Ein Mandant mit Bestand lässt sich nicht löschen. Die Alternative ist, ihn offline zu nehmen:
                Die Domains antworten mit 404, die Daten bleiben unberührt. Beides finden Sie in den Stammdaten des Mandanten.
            </p>
        </div>
    </div>

{% endblock %}

{% block deleted_stream %}
    <turbo-stream action="update" target="modal"></turbo-stream>
    <turbo-stream action="remove" target="tr-{{ id }}"></turbo-stream>
{% endblock %}
