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

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

{% block content %}
    {% set modeLabels = {
        'ESPECES': 'Espèces',
        'ORANGE_MONEY': 'Orange Money',
        'MTN_MOMO': 'MTN MoMo',
        'MOBILE_MONEY': 'Mobile Money',
        'VIREMENT': 'Virement',
        'AUTRE': 'Autre'
    } %}
    <style>
        .payment-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:1rem}
        .payment-actions{display:flex;gap:.75rem;flex-wrap:wrap}
        .payment-filters{display:grid;grid-template-columns:repeat(auto-fit,minmax(170px,1fr));gap:.75rem;align-items:end}
        .payment-filters label{display:block;font-size:.75rem;text-transform:uppercase;color:#64748b;margin-bottom:.3rem}
        .payment-filters input,.payment-filters select{width:100%;border:1px solid rgba(15,92,69,.16);border-radius:12px;padding:.72rem .85rem;background:#fff}
        .payment-table{width:100%;border-collapse:separate;border-spacing:0 .55rem}
        .payment-table th{font-size:.72rem;text-transform:uppercase;color:#64748b;text-align:left;padding:.35rem .75rem}
        .payment-table td{background:#fff;padding:.8rem .75rem;border-top:1px solid rgba(15,92,69,.1);border-bottom:1px solid rgba(15,92,69,.1);vertical-align:middle}
        .payment-table td:first-child{border-left:1px solid rgba(15,92,69,.1);border-radius:12px 0 0 12px}
        .payment-table td:last-child{border-right:1px solid rgba(15,92,69,.1);border-radius:0 12px 12px 0;text-align:right}
        .payment-amount{font-weight:800;color:#102033;white-space:nowrap}
    </style>
    <div class="admin-page-stack">
        <section class="admin-page-header">
            <div>
                <p class="admin-eyebrow">PAIEMENTS</p>
                <h1 class="admin-page-title">Encaissements</h1>
                <p>Suivi des paiements enregistrés pour {{ ecole.nom }}.</p>
            </div>
            <div class="payment-actions">
                <a class="admin-btn admin-btn-light" href="{{ path('school_finance_index') }}">Finances</a>
                <a class="admin-btn admin-btn-primary" href="{{ path('school_paiement_new') }}">Nouveau paiement</a>
            </div>
        </section>

        <section class="payment-grid">
            {% include 'components/admin/stat_card.html.twig' with {icon: 'payments', label: 'Total encaissé', value: stats.totalEncaisse|number_format(0, ',', ' ') ~ ' FCFA', trend: 'Paiements validés', meta: '', accent: 'emerald'} only %}
            {% include 'components/admin/stat_card.html.twig' with {icon: 'receipt_long', label: 'Paiements', value: stats.nombrePaiements, trend: 'Toutes situations', meta: '', accent: 'info'} only %}
            {% include 'components/admin/stat_card.html.twig' with {icon: 'calendar_month', label: 'Ce mois', value: stats.paiementsMois|number_format(0, ',', ' ') ~ ' FCFA', trend: 'Encaissements validés', meta: '', accent: 'gold'} only %}
            {% include 'components/admin/stat_card.html.twig' with {icon: 'block', label: 'Annulés', value: stats.annules, trend: 'Conservés en historique', meta: '', accent: 'neutral'} only %}
        </section>

        <section class="admin-card">
            <form class="payment-filters" method="get">
                <div>
                    <label for="q">Recherche</label>
                    <input id="q" name="q" value="{{ filters.q }}" placeholder="Nom, matricule, reçu">
                </div>
                <div>
                    <label for="annee">Année</label>
                    <select id="annee" name="annee">
                        <option value="">Toutes</option>
                        {% for annee in annees %}
                            <option value="{{ annee.id }}" {{ filters.anneeId == annee.id ? 'selected' : '' }}>{{ annee.libelle }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div>
                    <label for="statut">Statut</label>
                    <select id="statut" name="statut">
                        <option value="">Tous</option>
                        <option value="VALIDE" {{ filters.statut == 'VALIDE' ? 'selected' : '' }}>Validé</option>
                        <option value="ANNULE" {{ filters.statut == 'ANNULE' ? 'selected' : '' }}>Annulé</option>
                    </select>
                </div>
                <div>
                    <label for="modePaiement">Mode</label>
                    <select id="modePaiement" name="modePaiement">
                        <option value="">Tous</option>
                        {% for code,label in modeLabels %}
                            <option value="{{ code }}" {{ filters.modePaiement == code ? 'selected' : '' }}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div>
                    <label for="dateFrom">Du</label>
                    <input id="dateFrom" type="date" name="dateFrom" value="{{ filters.dateFromRaw }}">
                </div>
                <div>
                    <label for="dateTo">Au</label>
                    <input id="dateTo" type="date" name="dateTo" value="{{ filters.dateToRaw }}">
                </div>
                <div class="payment-actions">
                    <button class="admin-btn admin-btn-primary" type="submit">Filtrer</button>
                    <a class="admin-btn admin-btn-light" href="{{ path('school_paiement_index') }}">Réinitialiser</a>
                </div>
            </form>
        </section>

        <section class="admin-card">
            <div class="admin-card-head"><div><h2>Liste des paiements</h2><p>Les paiements annulés restent visibles pour garder la trace administrative.</p></div></div>
            <table class="payment-table">
                <thead><tr><th>Reçu</th><th>Date</th><th>Étudiant</th><th>Matricule</th><th>Montant</th><th>Mode</th><th>Statut</th><th></th></tr></thead>
                <tbody>
                {% for paiement in paiements %}
                    <tr>
                        <td><strong>{{ paiement.numeroRecu }}</strong></td>
                        <td>{{ paiement.datePaiement ? paiement.datePaiement|date('d/m/Y') : '-' }}</td>
                        <td>{{ paiement.etudiant ? paiement.etudiant.nomComplet : '-' }}</td>
                        <td>{{ paiement.etudiant and paiement.etudiant.matricule ? paiement.etudiant.matricule : '-' }}</td>
                        <td class="payment-amount">{{ paiement.montantTotal|number_format(0, ',', ' ') }} FCFA</td>
                        <td>{{ paiement.modePaiement ? modeLabels[paiement.modePaiement]|default(paiement.modePaiement) : '-' }}</td>
                        <td>{% include 'components/admin/status_badge.html.twig' with {label: paiement.statut == 'VALIDE' ? 'Validé' : 'Annulé', tone: paiement.statut == 'VALIDE' ? 'success' : 'danger'} only %}</td>
                        <td><a class="admin-text-link" href="{{ path('school_paiement_show', {id: paiement.id}) }}">Voir</a></td>
                    </tr>
                {% else %}
                    <tr><td colspan="8">Aucun paiement trouvé.</td></tr>
                {% endfor %}
                </tbody>
            </table>
        </section>
    </div>
{% endblock %}
