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

{% block title %}Contrat {{ contrat.reference }} - Association E-Cosplay{% 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">{{ contrat.reference }}</h1>
            <p class="text-xs text-gray-400 mt-1">{{ contrat.typeLabel }} - {{ contrat.raisonSociale }}</p>
        </div>
        <div class="flex items-center gap-3">
            {% if contrat.state == 'signed' %}
                <span class="px-3 py-1 bg-green-500/20 text-green-700 font-bold uppercase text-xs">Signe</span>
            {% elseif contrat.state == 'send' %}
                <span class="px-3 py-1 bg-blue-500/20 text-blue-700 font-bold uppercase text-xs">Envoye</span>
            {% elseif contrat.state == 'cancelled' %}
                <span class="px-3 py-1 bg-red-500/20 text-red-700 font-bold uppercase text-xs">Annule</span>
            {% else %}
                <span class="px-3 py-1 bg-yellow-100 text-yellow-800 font-bold uppercase text-xs">Brouillon</span>
            {% endif %}
            <a href="{{ path('app_admin_contrats_index') }}" class="px-4 py-2 glass font-bold uppercase text-xs tracking-widest hover:bg-gray-900 hover:text-white transition-all">Retour</a>
        </div>
    </div>

    {% for type, messages in app.flashes %}
        {% for message in messages %}
            <div class="mb-4 p-4 glass font-medium text-sm {{ type == 'success' ? 'border-green-300 text-green-800' : 'border-red-300 text-red-800' }}">{{ message }}</div>
        {% endfor %}
    {% endfor %}

    {# Informations #}
    <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
        <div class="glass p-5">
            <h2 class="text-sm font-bold uppercase tracking-wider mb-3">Client</h2>
            <div class="space-y-2 text-sm">
                <p><span class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Raison sociale :</span> <span class="font-bold">{{ contrat.raisonSociale }}</span></p>
                <p><span class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Email :</span> <span class="font-bold">{{ contrat.email }}</span></p>
                {% if contrat.customer %}
                    <p><span class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Compte client :</span> <a href="{{ path('app_admin_clients_show', {id: contrat.customer.id}) }}" class="font-bold" style="color: #fabf04;">{{ contrat.customer.fullName }}</a></p>
                {% endif %}
            </div>
        </div>
        <div class="glass p-5">
            <h2 class="text-sm font-bold uppercase tracking-wider mb-3">Contrat</h2>
            <div class="space-y-2 text-sm">
                <p><span class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Reference :</span> <span class="font-mono font-bold">{{ contrat.reference }}</span></p>
                <p><span class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Type :</span> <span class="font-bold">{{ contrat.typeLabel }}</span></p>
                <p><span class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Cree le :</span> <span class="font-bold">{{ contrat.createdAt|date('d/m/Y H:i') }}</span></p>
                {% if contrat.signedAt %}
                    <p><span class="text-[9px] font-bold uppercase tracking-wider text-gray-400">Signe le :</span> <span class="font-bold text-green-600">{{ contrat.signedAt|date('d/m/Y H:i') }}</span></p>
                {% endif %}
            </div>
        </div>
    </div>

    {# Services #}
    {% if contrat.services|length > 0 %}
        <div class="glass p-5 mb-6">
            <h2 class="text-sm font-bold uppercase tracking-wider mb-3">Services inclus</h2>
            <div class="glass overflow-hidden">
                <table class="w-full text-sm">
                    <thead>
                        <tr class="glass-dark text-white">
                            <th class="px-4 py-2 text-left font-bold uppercase text-[10px] tracking-widest">Service</th>
                            <th class="px-4 py-2 text-center font-bold uppercase text-[10px] tracking-widest">Quantite</th>
                            <th class="px-4 py-2 text-right font-bold uppercase text-[10px] tracking-widest">Prix unitaire HT</th>
                            <th class="px-4 py-2 text-right font-bold uppercase text-[10px] tracking-widest">Sous-total HT</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% set catalogLabels = {
                            'esite_vitrine': 'E-Site Vitrine',
                            'esite_ecommerce': 'E-Site E-Commerce',
                            'esite_hors_cms': 'Site hors CMS Esy-Web',
                            'email_3go': 'E-Mail 3 Go',
                            'email_50go': 'E-Mail 50 Go',
                            'ndd_gestion': 'Nom de domaine - Gestion',
                            'ndd_renouvellement': 'Nom de domaine - Renouvellement',
                            'eprotect': 'E-Protect Pro',
                            'ecalendar': 'E-Calendar',
                            'echat': 'E-Chat',
                            'emailer': 'E-Mailer'
                        } %}
                        {% for s in contrat.services %}
                            <tr class="border-b border-white/20 {{ loop.index is odd ? 'bg-white/30' : '' }}">
                                <td class="px-4 py-2 font-bold text-xs">{{ catalogLabels[s.service] ?? s.service }}</td>
                                <td class="px-4 py-2 text-center text-xs">{{ s.quantity }}</td>
                                <td class="px-4 py-2 text-right text-xs font-mono">{{ s.priceHt|number_format(2, ',', ' ') }} &euro;</td>
                                <td class="px-4 py-2 text-right text-xs font-mono font-bold">{{ (s.priceHt * s.quantity)|number_format(2, ',', ' ') }} &euro;</td>
                            </tr>
                        {% endfor %}
                    </tbody>
                    <tfoot>
                        <tr class="glass-dark text-white">
                            <td colspan="3" class="px-4 py-2 text-right font-bold uppercase text-[10px] tracking-widest">Total HT</td>
                            <td class="px-4 py-2 text-right font-mono font-bold">{{ contrat.totalHt|number_format(2, ',', ' ') }} &euro;</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    {% endif %}

    {# Actions #}
    <div class="flex flex-wrap gap-2 mb-6">
        {% if contrat.state == 'draft' %}
            {% if contrat.pdfUnsigned %}
                <form method="post" action="{{ path('app_admin_contrats_generate_pdf', {id: contrat.id}) }}" data-confirm="Regenerer le PDF ?">
                    <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_contrats_generate_pdf', {id: contrat.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 contrat.pdfUnsigned and contrat.state in ['draft', 'send'] %}
            <a href="{{ vich_uploader_asset(contrat, '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 contrat.state == 'draft' and contrat.pdfUnsigned %}
            <form method="post" action="{{ path('app_admin_contrats_send_signature', {id: contrat.id}) }}" data-confirm="Envoyer le contrat pour signature au client {{ contrat.raisonSociale }} ({{ contrat.email }}) ?">
                <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">Envoyer pour signature</button>
            </form>
        {% elseif contrat.state == 'send' and contrat.pdfUnsigned %}
            <form method="post" action="{{ path('app_admin_contrats_send_signature', {id: contrat.id}) }}" data-confirm="Renvoyer le contrat pour signature ?">
                <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">Renvoyer signature</button>
            </form>
        {% endif %}
        {% if contrat.pdfSigned %}
            <a href="{{ vich_uploader_asset(contrat, '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">
                Contrat signe
            </a>
        {% endif %}
        {% if contrat.pdfAudit %}
            <a href="{{ vich_uploader_asset(contrat, '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 signature
            </a>
        {% endif %}
        {% if contrat.state in ['draft', 'send'] %}
            <form method="post" action="{{ path('app_admin_contrats_cancel', {id: contrat.id}) }}" data-confirm="Annuler ce contrat ?">
                <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>
</div>
{% endblock %}
