{% extends "admin_base" %}
{% block title %}{% if admin_dashboard_title %}{{ admin_dashboard_title }}{% endif %}{% endblock %}
{% block breadcrumb %}
    <span class="admin-topbar__sep">/</span>
    <span>{% if admin_dashboard_title %}{{ admin_dashboard_title }}{% endif %}</span>
{% endblock %}
{% block content %}
{% block dashboard_header %}
<div class="admin-page__header">
    <div>
        <h1 class="admin-page__title">{% if admin_dashboard_title %}{{ admin_dashboard_title }}{% endif %}</h1>
        <p class="admin-page__subtitle">{% if admin_dashboard_subtitle %}{{ admin_dashboard_subtitle }}{% endif %}</p>
    </div>
    {% if admin_has_session_override %}
    <a href="{{ admin_prefix }}/toggle-template" class="btn btn-secondary btn-sm">← Template configuré</a>
    {% endif %}
</div>
{% endblock %}
{% block dashboard_stats %}
<div class="admin-stats__grid">
    {% for resource in resources %}
    <a href="{{ admin_prefix }}/{{ resource.key }}/list" class="admin-stats__card">
        <div class="admin-stats__label">{{ resource.title }}</div>
        <div class="admin-stats__value">{{ resource_counts[resource.key] | default(value="—") }}</div>
    </a>
    {% endfor %}
    {% if not resources %}
    <div class="admin-card admin-card--full-width">
        <div class="admin-card__body">
            <div class="admin-empty-state">
                <svg class="admin-empty-state__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
                    <path d="M9 17H7A5 5 0 0 1 7 7h2"/>
                    <path d="M15 7h2a5 5 0 1 1 0 10h-2"/>
                    <line x1="8" y1="12" x2="16" y2="12"/>
                </svg>
                <p class="admin-empty-state__title">{% if admin_dashboard_empty_title %}{{ admin_dashboard_empty_title }}{% endif %}</p>
                <p class="admin-empty-state__desc">{% if admin_dashboard_empty_desc %}{{ admin_dashboard_empty_desc }}{% endif %}</p>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
{% block dashboard_table %}
{% if resources %}
<div class="admin-card">
    <div class="admin-card__header">
        <h2 class="admin-card__title">{% if admin_dashboard_card_resources %}{{ admin_dashboard_card_resources }}{% endif %}</h2>
    </div>
    <div class="admin-table__wrapper">
        <table class="admin-table admin-table--resources">
            <thead>
                <tr>
                    <th>{% if admin_dashboard_th_resource %}{{ admin_dashboard_th_resource }}{% endif %}</th>
                    <th class="admin-table__col-secondary">{% if admin_dashboard_th_key %}{{ admin_dashboard_th_key }}{% endif %}</th>
                    <th class="admin-table__col-secondary">{% if admin_dashboard_th_permissions %}{{ admin_dashboard_th_permissions }}{% endif %}</th>
                    <th>{% if admin_dashboard_th_actions %}{{ admin_dashboard_th_actions }}{% endif %}</th>
                </tr>
            </thead>
            <tbody>
            {% for resource in resources %}
            <tr>
                <td><strong>{{ resource.title }}</strong></td>
                <td class="admin-table__col-secondary"><span class="admin-badge--id">{{ resource.key }}</span></td>
                <td class="admin-table__col-secondary">
                    {% if resource_groups[resource.key] %}
                        {% for grp in resource_groups[resource.key] %}
                        <span class="admin-badge admin-badge--blue">{{ grp }}</span>
                        {% endfor %}
                    {% endif %}
                </td>
                <td>
                    <div class="admin-table__actions">
                        <a href="{{ admin_prefix }}/{{ resource.key }}/list" class="btn btn-secondary btn-sm">
                            {{ admin_dashboard_btn_list }}
                        </a>

                        {% set_global can_create_resource = false %}
                        {% if current_user %}
                            {% if current_user.is_superuser %}
                                {% set_global can_create_resource = true %}
                            {% else %}
                                {% for groupe in current_user.groupes %}
                                    {% for perm in groupe.permissions %}
                                        {% if perm.resource_key == resource.key and perm.can_create %}
                                            {% set_global can_create_resource = true %}
                                        {% endif %}
                                    {% endfor %}
                                {% endfor %}
                            {% endif %}
                        {% endif %}

                        {% if can_create_resource %}
                        <a href="{{ admin_prefix }}/{{ resource.key }}/create" class="btn btn-primary btn-sm">
                            {{ admin_dashboard_btn_create }}
                        </a>
                        {% endif %}
                    </div>
                </td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endif %}
{% endblock %}
{% if current_page %}{% set current_page = current_page | default(value="list") %}{% endif %}
{% endblock %}
