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

{% block content %}
    {% set pricing = cart.getPricing %}
    {% set cart_checkout = cart.cartCheckout %}
    {% set invoice_address = cart.invoiceAddress %}
    {% set delivery_address = cart.deliveryAddress %}
    {% set billing_address = invoice_address ?: delivery_address %}
    {% set use_shipping_address = cart.deliveryMethod is null or cart.deliveryMethod.category is null or not cart.deliveryMethod.category.disabledShippingAddress %}

    <ol class="breadcrumb">
        <li><a href="{{ path('admin_order') }}">Commandes</a></li>
        <li class="active">Détail</li>
    </ol>

    <div class="page-header" style="margin-top: 0;">
        <div class="row">
            <div class="col-sm-8">
                <h1 style="margin-top: 0;">
                    Commande {{ cart.token ?: 'sans référence' }}
                </h1>
                <p class="text-muted" style="margin-bottom: 0;">
                    {% if cart.firstname or cart.lastname %}
                        {{ cart.firstname }} {{ cart.lastname }}
                    {% else %}
                        Client non renseigné
                    {% endif %}
                </p>
            </div>
            <div class="col-sm-4 text-right">
                <a href="{{ path('admin_order') }}" class="btn btn-default">
                    Retour à la liste
                </a>
                <a href="{{ path('admin_order_purchase_order_pdf', { id: cart.id }) }}" class="btn btn-info" target="_blank">
                    {{ download_document_label|trans({}, 'messages', 'fr') }}
                </a>
                {% if not cart.isCheckoutPayed %}
                    <a href="{{ path('admin_order_redirect_payment', { id: cart.id }) }}" class="btn btn-info" target="_blank">
                        Page de paiement
                    </a>
                    <a
                        href="{{ path('admin_order_delete', { id: cart.id }) }}"
                        class="confirm-deletable btn btn-danger"
                        data-toggle="tooltip"
                        title="Supprimer la commande {{ cart.token ?: 'sans référence' }}"
                        data-title="Supprimer la commande {{ cart.token ?: 'sans référence' }} ?"
                    >
                        Supprimer
                    </a>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-9">
            {% if cart.isCheckoutPayed %}
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <strong>Mise en expédition</strong>
                    </div>
                    <div class="panel-body">
                        <p class="text-muted">
                            Marquez la commande comme expédiée pour prévenir le client par email que sa commande a été remise au transporteur.
                            Si le suivi peut être effectué, veuillez renseigner le code de suivi et sélectionner un transporteur pour générer automatiquement l'URL de suivi.
                        </p>

                        <form method="post" action="{{ path('admin_order_delivery', { id: cart.id }) }}">
                            <input type="hidden" name="_token" value="{{ csrf_token('mark-delivery-' ~ cart.id) }}">

                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="tracking_carrier">Transporteur</label>
                                        <select name="tracking_carrier" id="tracking_carrier" class="form-control">
                                            <option value="">Aucun</option>
                                            {% for carrier_key, carrier in tracking_carriers %}
                                                <option value="{{ carrier_key }}" {% if selected_tracking_carrier == carrier_key %}selected{% endif %}>
                                                    {{ carrier.label }}
                                                </option>
                                            {% endfor %}
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="code_carrier">Code de suivi</label>
                                        <input
                                            type="text"
                                            name="code_carrier"
                                            id="code_carrier"
                                            class="form-control"
                                            value="{{ cart.codeCarrier }}"
                                            placeholder="Ex. 8X01234567890"
                                        >
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label>Lien de suivi actuel</label>
                                        <p class="form-control-static" style="word-break: break-all;">
                                            {% if cart.urlDeSuiviCarrier %}
                                                <a href="{{ cart.urlDeSuiviCarrier }}" target="_blank" rel="noopener noreferrer">
                                                    {{ cart.urlDeSuiviCarrier }}
                                                </a>
                                            {% else %}
                                                Aucun lien généré pour le moment.
                                            {% endif %}
                                        </p>
                                    </div>
                                </div>
                            </div>

                            <button type="submit" class="btn btn-primary">
                                {% if cart.isDelivery %}
                                    Mettre à jour l'expédition
                                {% else %}
                                    Marquer la commande comme expédiée
                                {% endif %}
                            </button>
                        </form>
                    </div>
                </div>
            {% endif %}

            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Panier</strong>
                </div>
                <div class="panel-body">
                    {% if pricing and pricing.lines is not empty %}
                        {% for line in pricing.lines %}
                            {% set line_previous_ttc = line.isPromoGift ? line.totalTTC : (line.degressiveTTC > 0 ? line.netTTC + line.degressiveTTC : null) %}
                            <div class="row" style="{% if not loop.last %}border-bottom: 1px solid #eee; margin-bottom: 20px; padding-bottom: 20px;{% endif %}">
                                <div class="col-sm-2">
                                    <div style="border: 1px solid #eee; background: #fff; padding: 8px; text-align: center;">
                                        {% if line.imagePath %}
                                            <img
                                                src="{{ asset(line.imagePath)|imagine_filter('relative_heighten_293_filter') }}"
                                                alt="{{ line.productName ? line.productName|upper : 'Produit' }}"
                                                style="max-width: 100%; max-height: 110px; object-fit: contain;"
                                            >
                                        {% else %}
                                            <span class="text-muted">Produit</span>
                                        {% endif %}
                                    </div>
                                </div>
                                <div class="col-sm-7">
                                    <div style="font-size: 16px; font-weight: 600; text-transform: uppercase;">
                                        {{ line.productName ? line.productName|upper : '-' }}
                                    </div>
                                    {% if line.typeName %}
                                        <div class="text-muted" style="margin-top: 6px;">
                                            {{ line.typeName }}{% if line.unitLabel %} ({{ line.unitLabel }}){% endif %}
                                        </div>
                                    {% elseif line.unitLabel %}
                                        <div class="text-muted" style="margin-top: 6px;">
                                            {{ line.unitLabel }}
                                        </div>
                                    {% endif %}
                                    {% if line.isPromoGift %}
                                        <div style="margin-top: 10px;">
                                            <span class="label label-warning">Offert</span>
                                        </div>
                                    {% endif %}
                                    {% if line.degressiveTTC > 0 %}
                                        <div class="text-muted" style="margin-top: 8px;">
                                            Dégressif : -{{ line.degressiveTTC|price_format }} €
                                        </div>
                                    {% endif %}
                                </div>
                                <div class="col-sm-3 text-right">
                                    <div class="text-muted">x{{ line.quantity }}</div>
                                    <div style="margin-top: 8px; font-size: 16px; font-weight: 600;">
                                        {% if line_previous_ttc is not null and line_previous_ttc > line.netTTC %}
                                            <span class="text-muted" style="text-decoration: line-through; margin-right: 6px;">
                                                {{ line_previous_ttc|price_format }} €
                                            </span>
                                        {% endif %}
                                        {% if line.isPromoGift %}
                                            Offert
                                        {% else %}
                                            {{ line.netTTC|price_format }} €
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        {% endfor %}
                    {% else %}
                        <p class="text-muted" style="margin-bottom: 0;">Ce panier est vide.</p>
                    {% endif %}
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Totaux</strong>
                </div>
                <div class="panel-body">
                    <table class="table" style="margin-bottom: 0;">
                        <tbody>
                        {% if pricing and pricing.degressiveTTC > 0 %}
                            <tr>
                                <th>{{ pricing.degressiveIsBottleOffered ? 'Bouteille offerte' : 'Dégressif' }}</th>
                                <td class="text-right">-{{ pricing.degressiveTTC|price_format }} €</td>
                            </tr>
                        {% endif %}
                        <tr>
                            <th>Sous-total</th>
                            <td class="text-right">
                                {% if pricing %}{{ pricing.subtotalTTC|price_format }} €{% else %}-{% endif %}
                            </td>
                        </tr>
                        {% for promo in cart.promos %}
                            {% if promo.discountItemsTTC > 0 %}
                                <tr>
                                    <th>Remise{% if promo.code %} - {{ promo.code }}{% endif %}</th>
                                    <td class="text-right">-{{ promo.discountItemsTTC|price_format }} €</td>
                                </tr>
                            {% endif %}
                        {% endfor %}
                        <tr>
                            <th>Livraison</th>
                            <td class="text-right">
                                {% if cart.cartShipping is not null %}
                                    {{ cart.cartShipping.amountTTC|price_format }} €
                                    {% if cart.cartShipping.name %}
                                        <br/><small class="text-muted">{{ cart.cartShipping.name }}</small>
                                    {% endif %}
                                {% else %}
                                    À définir
                                {% endif %}
                            </td>
                        </tr>
                        {% for promo in cart.promos %}
                            {% if promo.discountShippingTTC > 0 %}
                                <tr>
                                    <th>Remise livraison{% if promo.code %} - {{ promo.code }}{% endif %}</th>
                                    <td class="text-right">-{{ promo.discountShippingTTC|price_format }} €</td>
                                </tr>
                            {% endif %}
                        {% endfor %}
                        <tr>
                            <th style="font-size: 16px;">{{ 'shop.total_ttc'|trans }}</th>
                            <td class="text-right" style="font-size: 18px; font-weight: 700;">
                                {% if pricing %}{{ pricing.totalTTC|price_format }} €{% else %}-{% endif %}
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Paiement</strong>
                </div>
                <div class="panel-body">
                    <p>
                        <strong>Méthode :</strong><br>
                        {% if cart.paymentMethod %}
                            {{ cart.paymentMethod.method|trans }}
                        {% else %}
                            -
                        {% endif %}
                    </p>
                    <p><strong>Checkout :</strong><br>{% if cart_checkout %}Initialisé{% else %}Non initialisé{% endif %}</p>
                    <p><strong>Document :</strong><br>{{ document_label|trans({}, 'messages', 'fr') }}</p>

                    {% if (not cart.isCheckoutPayed and document_label == 'checkout_design.document_proforma') or (cart.isCheckoutPayed and document_label == 'checkout_design.document_invoice') %}
                        <form method="post" action="{{ path('admin_order_send_document_email', { id: cart.id }) }}" style="margin-bottom: 20px;">
                            <input type="hidden" name="_token" value="{{ csrf_token('send-document-email-' ~ cart.id) }}">

                            <button type="submit" class="btn btn-primary">
                                {% if cart.isCheckoutPayed %}
                                    Renvoyer la facture au client
                                {% else %}
                                    Envoyer la pro forma au client
                                {% endif %}
                            </button>
                        </form>
                    {% endif %}

                    {% if not cart.isCheckoutPayed %}
                        <hr>
                        <p class="text-muted">
                            Tant que la commande n'est pas payée, vous pouvez laisser un bon de commande{% if invoice_feature_enabled %} ou basculer manuellement en facture pro forma{% endif %}. {% if invoice_feature_enabled %}Si la facturation est activée sur la boutique, le paiement générera ensuite automatiquement la facture finale.{% endif %}
                        </p>

                        <form method="post" action="{{ path('admin_order_document_type', { id: cart.id }) }}" style="margin-bottom: 20px;">
                            <input type="hidden" name="_token" value="{{ csrf_token('document-type-' ~ cart.id) }}">

                            <div class="form-group">
                                <label for="document_type">Type de document</label>
                                <select name="document_type" id="document_type" class="form-control">
                                    <option value="order" {% if cart.documentType == 'order' %}selected{% endif %}>
                                        {{ 'checkout_design.document_order'|trans({}, 'messages', 'fr') }}
                                    </option>
                                    {% if invoice_feature_enabled %}
                                        <option value="invoice" {% if cart.documentType == 'invoice' %}selected{% endif %}>
                                            {{ 'checkout_design.document_proforma'|trans({}, 'messages', 'fr') }}
                                        </option>
                                    {% endif %}
                                </select>
                            </div>

                            <button type="submit" class="btn btn-default">
                                Enregistrer le type de document
                            </button>
                        </form>

                        <hr>
                        <p class="text-muted">
                            Marquer manuellement cette commande comme payée.
                        </p>
                        {% if checkout_completion_issue %}
                            <div class="alert alert-warning">
                                {{ checkout_completion_issue }}
                            </div>
                        {% endif %}

                        <form method="post" action="{{ path('admin_order_mark_paid', { id: cart.id }) }}">
                            <input type="hidden" name="_token" value="{{ csrf_token('mark-paid-' ~ cart.id) }}">

                            <div class="form-group">
                                <label for="payment_method_id">Méthode de paiement</label>
                                <select name="payment_method_id" id="payment_method_id" class="form-control" required>
                                    <option value="">Choisir</option>
                                    {% for payment_method in payment_methods %}
                                        <option
                                            value="{{ payment_method.id }}"
                                            {% if cart.paymentMethod and cart.paymentMethod.id == payment_method.id %}selected{% endif %}
                                        >
                                            {% if payment_method.configuration['name'] is defined and payment_method.configuration['name'] %}
                                                {{ payment_method.configuration['name'] }}
                                            {% else %}
                                                {{ payment_method.method|trans }}
                                            {% endif %}
                                        </option>
                                    {% endfor %}
                                </select>
                            </div>

                            <div class="form-group">
                                <label for="payed_at">Date de paiement</label>
                                <input
                                    type="datetime-local"
                                    name="payed_at"
                                    id="payed_at"
                                    class="form-control"
                                    value="{{ (cart_checkout and cart_checkout.payedAt ? cart_checkout.payedAt : date())|date('Y-m-d\\TH:i') }}"
                                    required
                                >
                            </div>

                            <button type="submit" class="btn btn-success" {% if checkout_completion_issue %}disabled="disabled"{% endif %}>
                                Passer la commande en payé
                            </button>
                        </form>
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>État de la commande</strong>
                </div>
                <div class="panel-body">
                    {% if cart.isCheckoutPayed %}
                        <span class="label label-success">Payée</span>
                    {% else %}
                        <span class="label label-warning">En cours</span>
                    {% endif %}
                    <hr>
                    <p><strong>Créée le :</strong><br>{{ cart.createdAt|date('d/m/Y H:i') }}</p>
                    {% if cart_checkout and cart_checkout.payedAt %}
                        <p><strong>Payée le :</strong><br>{{ cart_checkout.payedAt|date('d/m/Y H:i') }}</p>
                    {% endif %}
                    <p><strong>Référence :</strong><br>{{ cart.token ?: '-' }}</p>
                    <p><strong>Code groupe :</strong><br>{{ cart.groupCode ?: '-' }}</p>
                    <p>
                        <strong>Quantité :</strong><br>
                        {{ cart.quantity }}{% if pricing and pricing.degressiveIsBottleOffered %} + cadeau{% endif %}
                    </p>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Livraison</strong>
                </div>
                <div class="panel-body">
                    <p>
                        <strong>Statut :</strong><br>
                        {% if cart.isDelivery %}
                            <span class="label label-info">Expédiée</span>
                        {% else %}
                            <span class="label label-default">Non expédiée</span>
                        {% endif %}
                    </p>
                    <p>
                        <strong>Méthode :</strong><br>
                        {% if cart.deliveryMethod %}
                            {{ cart.deliveryMethod.name }}
                        {% elseif cart.cartShipping and cart.cartShipping.name %}
                            {{ cart.cartShipping.name }}
                        {% else %}
                            Livraison standard
                        {% endif %}
                    </p>
                    <p>
                        <strong>Code de suivi :</strong><br>
                        {{ cart.codeCarrier ?: '-' }}
                    </p>
                    <p>
                        <strong>Lien de suivi :</strong><br>
                        {% if cart.urlDeSuiviCarrier %}
                            <a href="{{ cart.urlDeSuiviCarrier }}" target="_blank" rel="noopener noreferrer">
                                Ouvrir le suivi
                            </a>
                        {% else %}
                            -
                        {% endif %}
                    </p>
                    {% if use_shipping_address and delivery_address %}
                        {% if delivery_address.company %}<p><strong>{{ delivery_address.company }}</strong></p>{% endif %}
                        <p>{{ delivery_address.firstname }} {{ delivery_address.lastname }}</p>
                        <p>{{ delivery_address.email ?: '-' }}</p>
                        <p>{{ delivery_address.phone ?: '-' }}</p>
                        <p>
                            {{ delivery_address.street }}<br>
                            {{ delivery_address.postal }} {{ delivery_address.city }}<br>
                            {{ delivery_address.country|country_name }}
                        </p>
                    {% elseif cart.deliveryMethod %}
                        <p>
                            <strong>{{ cart.deliveryMethod.category ? cart.deliveryMethod.category.name ~ ' : ' : '' }}{{ cart.deliveryMethod.name }}</strong>
                        </p>
                        {% if cart.deliveryMethod.description %}
                            <div class="text-muted">{{ cart.deliveryMethod.description|raw }}</div>
                        {% endif %}
                    {% else %}
                        <p class="text-muted" style="margin-bottom: 0;">Aucune adresse de livraison.</p>
                    {% endif %}
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Client</strong>
                </div>
                <div class="panel-body">
                    <p>
                        <strong>Compte site :</strong><br>
                        {% if siteUser %}
                            <span class="label label-success">Enregistré</span>
                        {% else %}
                            <span class="label label-default">Non enregistré</span>
                        {% endif %}
                    </p>
                    {% if cart.firstname or cart.lastname or cart.email or cart.phone or cart.company %}
                        {% if cart.company %}<p><strong>{{ cart.company }}</strong></p>{% endif %}
                        <p>{{ cart.firstname ?: '-' }} {{ cart.lastname ?: '' }}</p>
                        <p>{{ cart.email ?: '-' }}</p>
                        <p>{{ cart.phone ?: '-' }}</p>
                        {% if invoice_address and invoice_address.vatId %}
                            <p><strong>TVA :</strong> {{ invoice_address.vatId }}</p>
                        {% endif %}
                    {% else %}
                        <p>{{ cart.getEmail() ?: '-' }}</p>
                        <p class="text-muted" style="margin-bottom: 0;">Client non renseigné.</p>
                    {% endif %}
                </div>
            </div>

            {% if billing_address %}
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <strong>Adresse de facturation</strong>
                    </div>
                    <div class="panel-body">
                        {% if billing_address.company %}<p><strong>{{ billing_address.company }}</strong></p>{% endif %}
                        <p>
                            {{ billing_address.street }}<br>
                            {{ billing_address.postal }} {{ billing_address.city }}<br>
                            {{ billing_address.country|country_name }}
                        </p>
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
{% endblock %}
