{% extends 'school/base.html.twig' %}

{% set status_labels = {
    SOUMISE: 'Soumise',
    A_TESTER: 'À tester',
    A_AFFECTER: 'À affecter',
    VALIDEE: 'Validée',
    REJETEE: 'Rejetée',
    ANNULEE: 'Annulée'
} %}

{% block title %}Inscriptions | SALSABIL Madrassas{% endblock %}

{% block content %}
    {% set is_archive_scope = scope == 'archives' %}
    {% set is_all_scope = scope == 'all' %}
    <div class="admin-page-stack">
        <section class="admin-page-header">
            <div>
                <p class="admin-eyebrow">INSCRIPTIONS</p>
                <h1 class="admin-page-title">
                    {% if is_archive_scope %}Inscriptions archivées / anciennes{% elseif is_all_scope %}Toutes les inscriptions{% else %}Inscriptions de l’année en cours{% endif %}
                </h1>
                <p>
                    {% if is_archive_scope %}
                        Historique des anciennes années et dossiers archivés.
                    {% else %}
                        Traitez uniquement les demandes liées à {{ ecole.nom }}{% if currentAnnee %} pour {{ currentAnnee.libelle }}{% endif %}.
                    {% endif %}
                </p>
            </div>
            <a class="admin-btn admin-btn-primary" href="{{ public_registration_url(ecole) }}" target="_blank" rel="noopener">Formulaire public</a>
        </section>

        <section class="school-summary-grid">
            <a class="school-summary-card {{ scope == 'current' ? 'is-active' : '' }}" href="{{ path('school_inscription_index', {scope: 'current'}) }}">
                <span>Traitement courant</span>
                <strong>Année en cours</strong>
            </a>
            <a class="school-summary-card {{ scope == 'archives' ? 'is-active' : '' }}" href="{{ path('school_inscription_index', {scope: 'archives'}) }}">
                <span>Historique</span>
                <strong>Archives</strong>
            </a>
            <a class="school-summary-card {{ scope == 'all' ? 'is-active' : '' }}" href="{{ path('school_inscription_index', {scope: 'all'}) }}">
                <span>Recherche globale</span>
                <strong>Toutes les années</strong>
            </a>
        </section>

        <section class="admin-card">
            <form class="admin-form" method="get">
                <input type="hidden" name="scope" value="{{ scope }}">
                <div class="admin-form-four-cols">
                    <div>
                        <label class="form-label" for="q">Recherche</label>
                        <input class="form-control" id="q" name="q" value="{{ filters.q }}" placeholder="Nom, téléphone, dossier">
                    </div>
                    <div>
                        <label class="form-label" for="annee">Année</label>
                        <select class="form-select" id="annee" name="annee">
                            {% if scope != 'current' %}
                                <option value="">Toutes les années concernées</option>
                            {% endif %}
                            {% for annee in annees %}
                                <option value="{{ annee.id }}" {{ currentAnnee and currentAnnee.id == annee.id ? 'selected' : '' }}>
                                    {{ annee.libelle }}{% if anneeEnCours and annee.id == anneeEnCours.id %} · année en cours{% endif %}
                                </option>
                            {% else %}
                                <option value="">Aucune année active</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div>
                        <label class="form-label" for="statut">Statut</label>
                        <select class="form-select" id="statut" name="statut">
                            <option value="">Tous</option>
                            {% for label, value in statuts %}
                                <option value="{{ value }}" {{ filters.statut == value ? 'selected' : '' }}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div>
                        <label class="form-label" for="section">Section</label>
                        <select class="form-select" id="section" name="section">
                            <option value="">Toutes</option>
                            {% for section in sections %}
                                <option value="{{ section.id }}" {{ filters.section == section.id ? 'selected' : '' }}>{{ section.nom }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div>
                        <label class="form-label" for="type">Type</label>
                        <select class="form-select" id="type" name="type">
                            <option value="">Tous</option>
                            <option value="NOUVEAU" {{ filters.type == 'NOUVEAU' ? 'selected' : '' }}>Nouveau</option>
                            <option value="ANCIEN" {{ filters.type == 'ANCIEN' ? 'selected' : '' }}>Ancien</option>
                        </select>
                    </div>
                </div>
                <div class="admin-form-actions">
                    <button class="admin-btn admin-btn-primary" type="submit">Filtrer</button>
                    <a class="admin-btn admin-btn-light" href="{{ path('school_inscription_index', {scope: scope}) }}">Réinitialiser</a>
                </div>
            </form>
        </section>

        <section class="admin-card admin-table-card">
            <div class="admin-card-head">
                <div><h2>Demandes reçues</h2><p>{{ inscriptions|length }} demande(s) selon les filtres actuels.</p></div>
            </div>
            {% if inscriptions is empty %}
                <div class="admin-empty-state">
                    <span class="material-symbols-outlined">fact_check</span>
                    <strong>Aucune demande trouvée</strong>
                    <p>Les nouvelles demandes publiques apparaîtront ici automatiquement.</p>
                </div>
            {% else %}
                <div class="table-responsive">
                    <table class="table admin-table school-table align-middle mb-0">
                        <thead>
                        <tr>
                            <th>Dossier</th>
                            <th>Candidat</th>
                            <th>Année</th>
                            <th>Type</th>
                            <th>Parcours souhaité</th>
                            <th>Statut</th>
                            <th>Date</th>
                            <th class="text-end">Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for inscription in inscriptions %}
                            <tr>
                                <td>
                                    <div class="school-table-title">
                                        <strong>{{ inscription.numeroDossier }}</strong>
                                        <small>{{ inscription.displayMatricule ?: 'Matricule à finaliser' }}</small>
                                    </div>
                                </td>
                                <td>
                                    <div class="school-table-title">
                                        <strong>{{ inscription.nomComplet }}</strong>
                                        <small>{{ inscription.telephoneWhatsapp ?: inscription.telephonePrincipal ?: 'Contact non renseigné' }}</small>
                                    </div>
                                </td>
                                <td>{{ inscription.anneeScolaire ? inscription.anneeScolaire.libelle : 'Non liée' }}</td>
                                <td>
                                    {% include 'components/admin/status_badge.html.twig' with {label: inscription.typeCandidat == 'ANCIEN' ? 'Ancien' : 'Nouveau', tone: inscription.typeCandidat == 'ANCIEN' ? 'warning' : 'info'} only %}
                                </td>
                                <td>
                                    {% set active_parcours = inscription.parcours|filter(parcours => parcours.actif) %}
                                    {% set principal_parcours = active_parcours|filter(parcours => parcours.principal)|first ?: active_parcours|first %}
                                    {% if principal_parcours %}
                                        {% set principal_section = principal_parcours.sectionSouhaitee ?: principal_parcours.sectionFinale %}
                                        {% set principal_niveau = principal_parcours.niveauSouhaite ?: principal_parcours.niveauFinal %}
                                        <span>{{ principal_section ? principal_section.nom : 'Section non précisée' }}</span>
                                        {% if principal_niveau %}<small class="d-block">{{ principal_niveau.nom }}</small>{% endif %}
                                        {% if active_parcours|length > 1 %}<small class="d-block text-muted">+ {{ active_parcours|length - 1 }} autre{{ active_parcours|length > 2 ? 's' : '' }} parcours</small>{% endif %}
                                    {% else %}
                                        {{ inscription.sectionSouhaitee ? inscription.sectionSouhaitee.nom : 'Section non précisée' }}
                                        {% if inscription.niveauSouhaite %}<small class="d-block">{{ inscription.niveauSouhaite.nom }}</small>{% endif %}
                                    {% endif %}
                                </td>
                                <td>
                                    {% set tone = inscription.statut == 'VALIDEE' ? 'success' : (inscription.statut == 'REJETEE' or inscription.statut == 'ANNULEE' ? 'danger' : (inscription.statut == 'A_TESTER' ? 'warning' : 'info')) %}
                                    {% include 'components/admin/status_badge.html.twig' with {label: status_labels[inscription.statut]|default(inscription.statut), tone: tone} only %}
                                </td>
                                <td>{{ inscription.dateSoumission ? inscription.dateSoumission|date('d/m/Y H:i') : '-' }}</td>
                                <td>
                                    <div class="admin-actions">
                                        <a class="admin-action-button" href="{{ path('school_inscription_show', {id: inscription.id}) }}">Voir</a>
                                        {% if inscription.canBeAssigned %}
                                            <a class="admin-action-button" href="{{ path('school_inscription_affecter', {id: inscription.id}) }}">Affecter</a>
                                        {% endif %}
                                        {% if inscription.canBeDeleted %}
                                            <form method="post" action="{{ path('school_inscription_delete', {id: inscription.id}) }}" onsubmit="return confirm('Supprimer définitivement ce dossier d’inscription ? Cette action ne supprimera pas l’étudiant déjà validé.');">
                                                <input type="hidden" name="_token" value="{{ csrf_token('delete_inscription_' ~ inscription.id) }}">
                                                <button class="admin-action-button admin-action-danger" type="submit">Supprimer</button>
                                            </form>
                                        {% endif %}
                                    </div>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
        </section>
    </div>
{% endblock %}
