{% extends '@CpCreationVitiCore/admin/base.html.twig' %}

{% block content %}
    {% set invoice_feature_enabled = invoiceFeatureEnabled() %}
    {% set email_type_labels = {
        account_activation: 'Activation de compte',
        password_reset: 'Réinitialisation du mot de passe',
        offline_order_customer: 'Bon de commande client',
        offline_order_admin: 'Bon de commande boutique',
        paid_order_customer: invoice_feature_enabled ? 'Facture client' : 'Bon de commande client',
        paid_order_admin: invoice_feature_enabled ? 'Facture boutique' : 'Bon de commande boutique',
        order_resend_customer: 'Renvoi bon de commande client',
        document_customer: 'Renvoi document client',
        delivery_customer: 'Avis d’expédition',
        credit_customer: 'Avoir client'
    } %}
    {% set email_status_labels = {
        queued: { label: 'En file', class: 'label-info' },
        retrying: { label: 'Nouvelle tentative', class: 'label-warning' },
        sent: { label: 'Envoyé', class: 'label-success' },
        failed: { label: 'Échec', class: 'label-danger' }
    } %}

    <div class="row">
        <div class="col-md-12">
            <div class="page-header" style="margin-top: 0;">
                <h1>Détail client</h1>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Informations client
                </div>
                <table class="table table-striped">
                    <tbody>
                    <tr>
                        <th style="width: 220px;">Identifiant</th>
                        <td>{{ item.username ?: '-' }}</td>
                    </tr>
                    <tr>
                        <th>Email</th>
                        <td>{{ item.email ?: '-' }}</td>
                    </tr>
                    <tr>
                        <th>Compte vérifié</th>
                        <td>
                            {% if item.verified %}
                                <span class="label label-success">Oui</span>
                            {% else %}
                                <span class="label label-default">Non</span>
                            {% endif %}
                        </td>
                    </tr>
                    <tr>
                        <th>Date de création</th>
                        <td>{{ item.createdAt|date('d/m/Y H:i:s') }}</td>
                    </tr>
                    <tr>
                        <th>Dernière connexion</th>
                        <td>
                            {% if item.lastLoginAt %}
                                {{ item.lastLoginAt|date('d/m/Y H:i:s') }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                    </tr>
                    <tr>
                        <th>Adresse IP</th>
                        <td>{{ item.lastLoginIp ?: '-' }}</td>
                    </tr>
                    <tr>
                        <th>Opt-in marketing</th>
                        <td>
                            {% if item.marketingOptIn %}
                                <span class="label label-success">Oui</span>
                            {% else %}
                                <span class="label label-default">Non</span>
                            {% endif %}
                        </td>
                    </tr>
                    <tr>
                        <th>Groupe partenaire actuel</th>
                        <td>{{ item.group ? item.group.name : '-' }}</td>
                    </tr>
                    <tr>
                        <th>{{ 'admin.client.order_insights.completed_orders'|trans }}</th>
                        <td>{{ order_insights.summary.order_count }}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Actions
                </div>
                <div class="panel-body">
                    {% if item.verified %}
                        <p class="text-muted">Ce client est déjà vérifié.</p>
                        <button type="button" class="btn btn-success" disabled>
                            <i class="fas fa-check"></i> Client vérifié
                        </button>
                    {% else %}
                        <p class="text-muted">Ce compte n'a pas encore été vérifié. Vous pouvez le valider manuellement depuis l'administration.</p>
                        <form method="post" action="{{ path('admin_client_verify', { id: item.id }) }}">
                            <input type="hidden" name="_token" value="{{ csrf_token('verify-client-' ~ item.id) }}">
                            <button type="submit" class="btn btn-success">
                                <i class="fas fa-check"></i> Vérifier manuellement
                            </button>
                        </form>
                    {% endif %}
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">
                    Modifier le groupe partenaire
                </div>
                <div class="panel-body">
                    {{ form_start(group_form) }}
                    {{ form_row(group_form.group) }}
                    {{ form_widget(group_form.save) }}
                    {{ form_end(group_form) }}
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">
                    Suppression du client
                </div>
                <div class="panel-body">
                    {% if can_delete %}
                        <p class="text-muted">Ce client peut être supprimé car aucune commande enregistrée ne lui est rattachée.</p>
                        <form method="post" action="{{ path('admin_client_delete', { id: item.id }) }}" onsubmit="return confirm('Supprimer définitivement ce client enregistré ?');">
                            <input type="hidden" name="_token" value="{{ csrf_token('delete-client-' ~ item.id) }}">
                            <button type="submit" class="btn btn-danger">
                                <i class="fas fa-trash"></i> Supprimer le client
                            </button>
                        </form>
                    {% else %}
                        <p class="text-muted">La suppression est désactivée car ce client est vérifié ou possède au moins une commande enregistrée.</p>
                        <button type="button" class="btn btn-danger" disabled>
                            <i class="fas fa-trash"></i> Suppression impossible
                        </button>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Adresse de facturation
                </div>
                <div class="panel-body">
                    {% if item.invoiceAddress %}
                        <p><strong>Nom :</strong><br>{{ item.invoiceAddress.firstname }} {{ item.invoiceAddress.lastname }}</p>
                        <p><strong>Société :</strong><br>{{ item.invoiceAddress.company ?: '-' }}</p>
                        <p><strong>Email :</strong><br>{{ item.email ?: '-' }}</p>
                        <p><strong>Téléphone :</strong><br>{{ item.invoiceAddress.phone ?: '-' }}</p>
                        <p><strong>Adresse :</strong><br>{{ item.invoiceAddress.street ?: '-' }}</p>
                        <p><strong>Code postal / Ville :</strong><br>{{ item.invoiceAddress.postal ?: '-' }} {{ item.invoiceAddress.city ?: '' }}</p>
                        <p><strong>Pays :</strong><br>{{ item.invoiceAddress.country ?: '-' }}</p>
                    {% else %}
                        <p class="text-muted">Aucune adresse de facturation renseignée.</p>
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Adresse de livraison
                </div>
                <div class="panel-body">
                    {% if item.deliveryAddress %}
                        <p><strong>Nom :</strong><br>{{ item.deliveryAddress.firstname }} {{ item.deliveryAddress.lastname }}</p>
                        <p><strong>Société :</strong><br>{{ item.deliveryAddress.company ?: '-' }}</p>
                        <p><strong>Email :</strong><br>{{ item.email ?: '-' }}</p>
                        <p><strong>Téléphone :</strong><br>{{ item.deliveryAddress.phone ?: '-' }}</p>
                        <p><strong>Adresse :</strong><br>{{ item.deliveryAddress.street ?: '-' }}</p>
                        <p><strong>Code postal / Ville :</strong><br>{{ item.deliveryAddress.postal ?: '-' }} {{ item.deliveryAddress.city ?: '' }}</p>
                        <p><strong>Pays :</strong><br>{{ item.deliveryAddress.country ?: '-' }}</p>
                    {% else %}
                        <p class="text-muted">Aucune adresse de livraison renseignée.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            Historique des emails
        </div>
        <div class="table-responsive">
            <table class="table table-bordered" width="100%" cellspacing="0">
                <thead>
                <tr>
                    <th>Type</th>
                    <th>Destinataire</th>
                    <th>Statut</th>
                    <th>Date</th>
                    <th>Commande</th>
                </tr>
                </thead>
                <tbody>
                {% for email_log in email_logs %}
                    {% set email_status = email_status_labels[email_log.status]|default({ label: email_log.status, class: 'label-default' }) %}
                    <tr>
                        <td>
                            <strong>{{ email_type_labels[email_log.type]|default(email_log.type) }}</strong>
                            {% if email_log.subject %}
                                <br><small class="text-muted">{{ email_log.subject }}</small>
                            {% endif %}
                            {% if email_log.error %}
                                <br><small class="text-danger">{{ email_log.error }}</small>
                            {% endif %}
                        </td>
                        <td>
                            {{ email_log.recipientName ?: '-' }}
                            <br><small>{{ email_log.recipientEmail }}</small>
                        </td>
                        <td>
                            <span class="label {{ email_status.class }}">{{ email_status.label }}</span>
                        </td>
                        <td>
                            {{ email_log.queuedAt|date('d/m/Y H:i') }}
                            <br><small class="text-muted">{{ email_log.transportLabel }}</small>
                            {% if email_log.sentAt %}
                                <br><small class="text-success">envoyé {{ email_log.sentAt|date('d/m/Y H:i') }}</small>
                            {% elseif email_log.failedAt %}
                                <br><small class="text-danger">échec {{ email_log.failedAt|date('d/m/Y H:i') }}</small>
                            {% endif %}
                        </td>
                        <td>
                            {% if email_log.cart %}
                                <a href="{{ path('admin_order_order', { id: email_log.cart.id }) }}" class="btn btn-default btn-sm">
                                    {{ email_log.cart.token ?: 'Ouvrir' }}
                                </a>
                            {% else %}
                                -
                            {% endif %}
                        </td>
                    </tr>
                {% else %}
                    <tr>
                        <td colspan="5" class="text-center text-muted">Aucun email suivi pour ce client.</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    {% include '@CpCreationVitiCore/admin/client/_order_insights.html.twig' with { order_insights: order_insights } only %}
{% endblock %}
