{% extends 'admin/_layout.html.twig' %}

{% block title %}Echeancier - {{ customer.fullName }} - Administration{% endblock %}

{% block admin_content %}
<div class="page-container">
    <div class="flex items-center justify-between mb-6">
        <div>
            <h1 class="text-2xl font-bold heading-page">Echeancier</h1>
            <p class="text-xs text-gray-400 mt-1">{{ echeancier.reference }} - {{ customer.fullName }} - {{ echeancier.description }}</p>
        </div>
        <div class="flex items-center gap-3">
            {% if echeancier.state == 'draft' %}
                <span class="px-3 py-1 bg-yellow-100 text-yellow-800 font-bold uppercase text-xs">Brouillon</span>
            {% elseif echeancier.state == 'send' %}
                <span class="px-3 py-1 bg-blue-500/20 text-blue-700 font-bold uppercase text-xs">Envoye</span>
            {% elseif echeancier.state == 'signed' %}
                <span class="px-3 py-1 bg-purple-500/20 text-purple-700 font-bold uppercase text-xs">Signe</span>
            {% elseif echeancier.state == 'pending_setup' %}
                <span class="px-3 py-1 bg-orange-500/20 text-orange-700 font-bold uppercase text-xs">En attente SEPA</span>
            {% elseif echeancier.state == 'active' %}
                <span class="px-3 py-1 bg-green-500/20 text-green-700 font-bold uppercase text-xs">Actif</span>
            {% elseif echeancier.state == 'completed' %}
                <span class="px-3 py-1 bg-emerald-500/20 text-emerald-700 font-bold uppercase text-xs">Termine</span>
            {% elseif echeancier.state == 'cancelled' %}
                <span class="px-3 py-1 bg-red-500/20 text-red-700 font-bold uppercase text-xs">Annule</span>
            {% elseif echeancier.state == 'default' %}
                <span class="px-3 py-1 bg-red-600 text-white font-bold uppercase text-xs">Defaut</span>
            {% endif %}
            <a href="{{ path('app_admin_clients_show', {id: customer.id, tab: 'echeancier'}) }}" class="px-4 py-2 glass font-bold uppercase text-[10px] tracking-widest hover:bg-gray-900 hover:text-white transition-all">Retour</a>
        </div>
    </div>

    {% for flash in app.flashes('success') %}
        <div class="mb-4 p-3 bg-green-500/20 text-green-700 font-bold text-xs">{{ flash }}</div>
    {% endfor %}
    {% for flash in app.flashes('error') %}
        <div class="mb-4 p-3 bg-red-500/20 text-red-700 font-bold text-xs">{{ flash }}</div>
    {% endfor %}

    {# Resume #}
    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
        <div class="glass p-4">
            <p class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Creance + majoration 5%</p>
            <p class="text-2xl font-bold mt-1">{{ echeancier.totalWithMajoration|number_format(2, ',', ' ') }} &euro;</p>
            <p class="text-[10px] text-gray-400 mt-1">{{ echeancier.totalAmountHt|number_format(2, ',', ' ') }} + <span class="text-red-500 font-bold">{{ echeancier.majoration|number_format(2, ',', ' ') }} &euro;</span></p>
        </div>
        <div class="glass p-4">
            <p class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Mensualite</p>
            <p class="text-2xl font-bold mt-1 text-[#fabf04]">{{ echeancier.monthlyAmount|number_format(2, ',', ' ') }} &euro;</p>
        </div>
        <div class="glass p-4">
            <p class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Progression</p>
            <p class="text-2xl font-bold mt-1">{{ echeancier.nbPaid }}/{{ echeancier.nbLines }}</p>
            <div class="w-full bg-gray-200 h-2 mt-2">
                <div class="bg-green-500 h-2" style="width: {{ echeancier.progress }}%"></div>
            </div>
        </div>
        <div class="glass p-4">
            <p class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Deja paye</p>
            <p class="text-2xl font-bold mt-1 text-green-600">{{ echeancier.totalPaid|number_format(2, ',', ' ') }} &euro;</p>
        </div>
    </div>

    {# Description #}
    <div class="glass p-5 mb-6">
        <h2 class="text-sm font-bold uppercase tracking-wider mb-2">Motif</h2>
        <p class="text-sm text-gray-600">{{ echeancier.description }}</p>
        {% if echeancier.advert %}
            <p class="text-xs text-gray-400 mt-2">Avis lie : <a href="{{ path('app_admin_advert_show', {id: echeancier.advert.id}) }}" class="text-[#fabf04] font-bold">{{ echeancier.advert.orderNumber.numOrder }}</a></p>
        {% endif %}
    </div>

    {# Actions #}
    <div class="flex flex-wrap gap-2 mb-6">
        {% if echeancier.state in ['draft', 'send'] %}
            {% if echeancier.pdfUnsigned %}
                <form method="post" action="{{ path('app_admin_echeancier_generate_pdf', {id: echeancier.id}) }}" data-confirm="Regenerer le PDF ? Le fichier existant sera remplace.">
                    <button type="submit" class="px-4 py-2 bg-yellow-500/20 text-yellow-700 hover:bg-yellow-500 hover:text-white font-bold uppercase text-[10px] tracking-wider transition-all">Regenerer PDF</button>
                </form>
            {% else %}
                <form method="post" action="{{ path('app_admin_echeancier_generate_pdf', {id: echeancier.id}) }}">
                    <button type="submit" class="px-4 py-2 bg-gray-900 text-white font-bold uppercase text-[10px] tracking-wider hover:bg-[#fabf04] hover:text-gray-900 transition-all">Generer PDF</button>
                </form>
            {% endif %}
        {% endif %}
        {% if echeancier.pdfUnsigned and echeancier.state in ['draft', 'send'] %}
            <a href="{{ vich_uploader_asset(echeancier, 'pdfUnsignedFile') }}" target="_blank"
               class="px-4 py-2 bg-gray-900 text-white font-bold uppercase text-[10px] tracking-wider hover:bg-[#fabf04] hover:text-gray-900 transition-all">
                Voir PDF
            </a>
        {% endif %}
        {% if echeancier.state in ['draft', 'send'] and echeancier.pdfUnsigned %}
            <form method="post" action="{{ path('app_admin_echeancier_send_signature', {id: echeancier.id}) }}" data-confirm="{{ echeancier.state == 'send' ? 'Renvoyer le lien de signature au client ?' : 'Envoyer le PDF pour signature au client via DocuSeal ?' }}">
                <button type="submit" class="px-4 py-2 bg-purple-500/20 text-purple-700 hover:bg-purple-500 hover:text-white font-bold uppercase text-[10px] tracking-wider transition-all">{{ echeancier.state == 'send' ? 'Renvoyer signature' : 'Envoyer pour signature' }}</button>
            </form>
        {% endif %}
        {% if echeancier.pdfSigned %}
            <a href="{{ vich_uploader_asset(echeancier, 'pdfSignedFile') }}" target="_blank"
               class="px-4 py-2 bg-green-500/20 text-green-700 font-bold uppercase text-[10px] tracking-wider hover:bg-green-500 hover:text-white transition-all">
                Voir echeancier signe
            </a>
        {% endif %}
        {% if echeancier.pdfAudit %}
            <a href="{{ vich_uploader_asset(echeancier, 'pdfAuditFile') }}" target="_blank"
               class="px-4 py-2 bg-blue-500/20 text-blue-700 font-bold uppercase text-[10px] tracking-wider hover:bg-blue-500 hover:text-white transition-all">
                Audit de signature
            </a>
        {% endif %}
        {% if echeancier.state in ['signed', 'pending_setup'] %}
            <form method="post" action="{{ path('app_admin_echeancier_send_sepa', {id: echeancier.id}) }}" data-confirm="{{ echeancier.state == 'pending_setup' ? 'Renvoyer le lien de configuration SEPA au client ?' : 'Envoyer le lien de configuration SEPA au client ?' }}">
                <button type="submit" class="px-4 py-2 bg-orange-500/20 text-orange-700 hover:bg-orange-500 hover:text-white font-bold uppercase text-[10px] tracking-wider transition-all">{{ echeancier.state == 'pending_setup' ? 'Renvoyer lien SEPA' : 'Envoyer lien SEPA' }}</button>
            </form>
        {% endif %}
        {% if echeancier.stripePaymentMethodId and echeancier.state in ['active', 'pending_setup'] %}
            <form method="post" action="{{ path('app_admin_echeancier_reset_sepa', {id: echeancier.id}) }}" data-confirm="Reinitialiser le moyen de paiement SEPA ? Le client devra reconfigurer son IBAN.">
                <button type="submit" class="px-4 py-2 bg-yellow-500/20 text-yellow-700 hover:bg-yellow-500 hover:text-white font-bold uppercase text-[10px] tracking-wider transition-all">Reinitialiser SEPA</button>
            </form>
        {% endif %}
        {% if echeancier.state not in ['draft'] %}
            <form method="post" action="{{ path('app_admin_echeancier_send_attestation', {id: echeancier.id}) }}" data-confirm="Envoyer une attestation d'etat de l'echeancier au client ?">
                <button type="submit" class="px-4 py-2 bg-blue-500/20 text-blue-700 hover:bg-blue-500 hover:text-white font-bold uppercase text-[10px] tracking-wider transition-all">Envoyer attestation</button>
            </form>
        {% endif %}
        {% if echeancier.state in ['draft', 'send', 'signed', 'pending_setup', 'active'] %}
            <form method="post" action="{{ path('app_admin_echeancier_cancel', {id: echeancier.id}) }}" data-confirm="Annuler cet echeancier ? La subscription Stripe sera annulee.">
                <button type="submit" class="px-4 py-2 bg-red-500/20 text-red-700 hover:bg-red-500 hover:text-white font-bold uppercase text-[10px] tracking-wider transition-all">Annuler</button>
            </form>
        {% endif %}
    </div>

    {# Echeances #}
    <h2 class="text-lg font-bold uppercase mb-4">Echeances</h2>
    <div class="glass overflow-x-auto overflow-hidden">
        <table class="w-full text-sm">
            <thead>
                <tr class="glass-dark text-white">
                    <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">N</th>
                    <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">Date prevue</th>
                    <th class="px-4 py-3 text-right font-bold uppercase text-xs tracking-widest">Montant</th>
                    <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-widest">Statut</th>
                    <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">Paye le</th>
                    <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-widest">Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for line in echeancier.lines %}
                    <tr class="border-b border-white/20 hover:bg-white/50">
                        <td class="px-4 py-3 font-bold">{{ line.position }}</td>
                        <td class="px-4 py-3 text-xs">{{ line.scheduledAt|date('d/m/Y') }}</td>
                        <td class="px-4 py-3 text-right font-bold text-xs">{{ line.amount|number_format(2, ',', ' ') }} &euro;</td>
                        <td class="px-4 py-3 text-center">
                            {% if line.isPaid %}
                                <span class="px-2 py-0.5 bg-green-500/20 text-green-700 font-bold uppercase text-[10px]">Paye</span>
                            {% elseif line.isFailed %}
                                <span class="px-2 py-0.5 bg-red-500/20 text-red-700 font-bold uppercase text-[10px]">Echoue</span>
                            {% else %}
                                <span class="px-2 py-0.5 bg-yellow-100 text-yellow-800 font-bold uppercase text-[10px]">En attente</span>
                            {% endif %}
                        </td>
                        <td class="px-4 py-3 text-xs text-gray-500">
                            {{ line.paidAt ? line.paidAt|date('d/m/Y H:i') : '—' }}
                            {% if line.failureReason %}
                                <span class="text-red-500 ml-1">{{ line.failureReason }}</span>
                            {% endif %}
                        </td>
                        <td class="px-4 py-3 text-center">
                            {% if line.isPending and echeancier.stripePaymentMethodId and not line.stripePaymentIntentId %}
                                <form method="post" action="{{ path('app_admin_echeancier_force_payment', {id: echeancier.id, lineId: line.id}) }}" data-confirm="Forcer le prelevement de {{ line.amount|number_format(2, ',', ' ') }} EUR pour l'echeance {{ line.position }} ?">
                                    <button type="submit" class="px-2 py-1 bg-orange-500/20 text-orange-700 hover:bg-orange-500 hover:text-white font-bold uppercase text-[9px] tracking-wider transition-all">Forcer</button>
                                </form>
                            {% elseif line.isFailed and echeancier.stripePaymentMethodId %}
                                <form method="post" action="{{ path('app_admin_echeancier_force_payment', {id: echeancier.id, lineId: line.id}) }}" data-confirm="Retenter le prelevement de {{ line.amount|number_format(2, ',', ' ') }} EUR pour l'echeance {{ line.position }} ?">
                                    <button type="submit" class="px-2 py-1 bg-red-500/20 text-red-700 hover:bg-red-500 hover:text-white font-bold uppercase text-[9px] tracking-wider transition-all">Retenter</button>
                                </form>
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    {% if echeancier.stripePaymentMethodId %}
        <div class="glass p-4 mt-4">
            <h3 class="text-[9px] font-bold uppercase tracking-wider text-gray-400 mb-2">Mandat SEPA</h3>
            <div class="grid grid-cols-2 md:grid-cols-4 gap-3 text-xs">
                <div>
                    <p class="text-[9px] font-bold uppercase tracking-wider text-gray-400">IBAN</p>
                    <p class="font-mono font-bold mt-1">**** **** **** {{ echeancier.stripeSepaLast4 ?: '****' }}</p>
                </div>
                {% if echeancier.stripeSepaBankName %}
                    <div>
                        <p class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Code banque</p>
                        <p class="font-bold mt-1">{{ echeancier.stripeSepaBankName }}</p>
                    </div>
                {% endif %}
                {% if echeancier.stripeSepaCountry %}
                    <div>
                        <p class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Pays</p>
                        <p class="font-bold mt-1">{{ echeancier.stripeSepaCountry }}</p>
                    </div>
                {% endif %}
                <div>
                    <p class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Statut</p>
                    <p class="font-bold mt-1 text-green-600">Actif</p>
                </div>
            </div>
            <p class="text-[10px] text-gray-400 font-mono mt-2">{{ echeancier.stripePaymentMethodId }}</p>
        </div>
    {% endif %}
</div>
{% endblock %}
