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

{% block content %}
    <div class="panel panel-default">
        <div class="panel-heading">
            Historique des commandes
        </div>
        <div class="table-responsive">
            <table class="table table-bordered dataTable" width="100%" cellspacing="0">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Date</th>
                    <th>Payé le</th>
                    <th>Total</th>
                    <th>Livraison</th>
                    <th>Référence</th>
                    <th>Paiement</th>
                    <th>Code partenaire</th>
                    <th>Commande</th>
                    <th>Paiement & Livraison</th>
                    <th>Supprimer</th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th>#</th>
                    <th>Date</th>
                    <th>Payé le</th>
                    <th>Total</th>
                    <th>Livraison</th>
                    <th>Référence</th>
                    <th>Paiement</th>
                    <th>Code partenaire</th>
                    <th>Commande</th>
                    <th>Paiement & Livraison</th>
                    <th>Supprimer</th>
                </tr>
                </tfoot>
                <tbody>
                {% for item in items %}
                    <tr>
                        <td>{{ (items | length) - loop.index + 1 }}</td>
                        <td>{{ item.createdAt | date('d-m-Y') }}<br/>{{ item.createdAt | date('H:i:s') }}</td>
                        {% if item.payedAt %}
                            <td>{{ item.payedAt | date('d-m-Y') }}<br/>{{ item.payedAt| date('H:i:s') }}</td>
                        {% else %}
                            <td>-</td>
                        {% endif %}
                        <td>{{ item.cart.total / 100 }} €</td>
                        <td>
                            {% if item.cart.deliveryMethod %}
                                {{ item.cart.deliveryMethod.getName }}
                            {% else %}
                                {{ item.cart.shippingName }}
                            {% endif %}
                        </td>

                        <td><strong>Référence:</strong> {{ item.cart.token }}<br/>
                            {{ item.cart.invoiceAddress.lastname }}<br/>
                            {{ item.cart.invoiceAddress.firstname }}<br/>
                            {% set codeError = null %}
                            {% set paysCard = null %}
                            {% set paysCode = null %}
                            {% set ipCard = null %}
                            {% if item.cart.cartCheckout.response is not empty %}
                                {% if item.cart.paymentMethod.method == "systempay" or item.cart.paymentMethod.method == "sogecommerce" %}
                                    {% if item.cart.cartCheckout.response.vads_card_country is defined and item.cart.cartCheckout.response.vads_card_country is not null %}
                                        {% set paysCard = item.cart.cartCheckout.response.vads_card_country|country_name %}
                                        {% set paysCode = item.cart.cartCheckout.response.vads_card_country %}
                                    {% endif %}
                                    {% if item.cart.cartCheckout.response.vads_pays_ip is defined and item.cart.cartCheckout.response.vads_pays_ip is not null %}
                                        {% set ipCard = item.cart.cartCheckout.response.vads_pays_ip|country_name %}
                                    {% endif %}
                                    {% if item.cart.cartCheckout.response.vads_payment_error is defined and item.cart.cartCheckout.response.vads_result != 00 %}
                                        {% set codeError = item.cart.cartCheckout.response.vads_payment_error %}
                                    {% endif %}
                                {% elseif  item.cart.paymentMethod.method == "e-transactions" %}
                                    {% if item.cart.cartCheckout.response.Pays is defined and item.cart.cartCheckout.response.Pays is not null %}
                                        {% set paysCard = item.cart.cartCheckout.response.Pays|country_name_alpha3 %}
                                        {% set paysCode = item.cart.cartCheckout.response.Pays %}
                                    {% endif %}
                                    {% if item.cart.cartCheckout.response.Ip is defined and item.cart.cartCheckout.response.Ip is not null %}
                                        {% set ipCard = item.cart.cartCheckout.response.Ip|country_name_alpha3 %}
                                    {% endif %}
                                    {% if item.cart.cartCheckout.response.Erreur is defined and item.cart.cartCheckout.response.Erreur != 000000 %}
                                        {% set codeError = item.cart.cartCheckout.response.Erreur %}
                                    {% endif %}
                                {% elseif  item.cart.paymentMethod.method == "monetico" %}
                                    {% if item.cart.cartCheckout.response.originecb is defined and item.cart.cartCheckout.response.originecb != "ZZZ" %}
                                        {% set paysCard = item.cart.cartCheckout.response.originecb|country_name_alpha3 %}
                                        {% set paysCode = item.cart.cartCheckout.response.originecb %}
                                    {% endif %}
                                    {% if item.cart.cartCheckout.response.originetr is defined and item.cart.cartCheckout.response.originetr != "ZZZ" %}
                                        {% set ipCard = item.cart.cartCheckout.response.originetr|country_name_alpha3 %}
                                    {% endif %}
                                    {% if item.cart.cartCheckout.response.vads_result is defined and item.cart.cartCheckout.response.vads_result != 00 %}
                                        {% set codeError = item.cart.cartCheckout.response.vads_payment_error %}
                                    {% endif %}
                                {% elseif  item.cart.paymentMethod.method == "sherlock2" or item.cart.paymentMethod.method == "sherlock" %}
                                    {% if item.cart.cartCheckout.response.Data is defined and item.cart.cartCheckout.response.Data is not null %}
                                        {% set data = [] %}
                                        {% for item in item.cart.cartCheckout.response.Data|split('|')|map((value, key) => value|split('=')) %}
                                            {% set row = { (item[0]): (item[1]) } %}
                                            {% set data = data|merge(row) %}
                                        {% endfor %}
                                        {% if data['issuerCountryCode'] is defined and data['issuerCountryCode'] != 'null' %}
                                            {% set paysCard = (data['issuerCountryCode'])|country_name_alpha3 %}
                                            {% set paysCode = data['issuerCountryCode'] %}
                                        {% endif %}
                                        {% if data['responseCode'] is defined and data['responseCode'] != 00 %}
                                            {% set codeError = data['responseCode'] %}
                                        {% endif %}
                                    {% endif %}
                                {% elseif  item.cart.paymentMethod.method == "axepta" %}

                                {% endif %}
                                {% if paysCard is not null %}<strong>CB:</strong> {{ paysCard }}<br/>{% endif %}

                                {% if ipCard is not null %}<strong>IP:</strong> {{ ipCard }} <br/>{% endif %}
                                {% if paysCard is not null and paysCard != item.cart.invoiceAddress.country|country_name and paysCode not in ['DE','DEU','FR','FRA','BE','BEL','MC','MCO','IT','ITA','GB','GBR','LU','LUX'] %}
                                    <span class="warning" style="color: #DC143C;"><strong><i
                                                    class="fas fa-exclamation"></i> À Vérifier</strong></span>
                                {% endif %}
                                {% if codeError is not null %}
                                    <strong>Code Erreur :</strong> {{ codeError }}
                                {% endif %}
                            {% endif %}
                        </td>
                        <td>{{ item.paymentMethod.method | trans }}</td>
                        <td>{% if item.cart.groupCode %}{{ item.cart.groupCode }}{% endif %}</td>
                        <td>
                            <a href="{{ path('admin_order_order', { id: item.id}) }}" class="btn btn-info"><i
                                        class="fas fa-search"></i> Voir</a>
                            <br/>
                            {{ render(controller("CpCreation\\VitiCore\\Bridge\\Symfony\\Bundle\\Controller\\Admin\\OrderController::showGiftOptions",{ 'cart' : item.cart })) }}
                            <br/>
                            <a href="{{ path('admin_order_pdf', { id: item.id}) }}" target="_blank"
                               class="btn btn-info"><i
                                        class="fas fa-file"></i> PDF</a>

                        </td>
                        <td>
                            {% if item.paymentMethod.method == 'banktransfert' or item.paymentMethod.method == 'check' %}
                                {% if item.payedAt %}
                                    <a href="{{ path('admin_order_payedAt', {id : item.id}) }}"
                                       class="btn btn-success confirm-modification" data-title="Passer la commande de {{ item.cart.company }} {{ item.cart.lastname }} {{ item.cart.firstname }} en non-réglée">Réglé</a>
                                {% else %}
                                    <a href="{{ path('admin_order_payedAt', {id : item.id}) }}" class="btn btn-danger confirm-modification" data-title="Passer la commande de {{ item.cart.company }} {{ item.cart.lastname }} {{ item.cart.firstname }} en réglée">Non
                                        réglé</a>
                                    <br/>
                                    <br/>
                                    <a href="{{ path('admin_order_redirect_payment', {id : item.id}) }}"
                                       class="btn btn-info" target="_blank">Page de paiement</a>
                                {% endif %}
                            {% else %}
                                {% if item.payedAt %}
                                    <span style="cursor: auto;" class="btn btn-success">Réglé</span>
                                {% else %}
                                    <span style="cursor: auto;" class="btn btn-danger">Non réglé</span>
                                    <br/>
                                    <br/>
                                    <a href="{{ path('admin_order_redirect_payment', {id : item.id}) }}"
                                       class="btn btn-info" target="_blank">Page de paiement</a>
                                {% endif %}
                            {% endif %}
                            {% if item.payedAt %}
                                <br/>
                                <br/>
                                {% if item.cart.cartCheckout.delivery == false  and  item.cart.deliveryMethod is null %}
                                    <a href="{{ path('admin_order_delivery', {id : item.id}) }}"
                                       class="btn btn-warning">Avertir
                                        de la livraison ?</a>
                                {% elseif item.cart.deliveryMethod is not null %}
                                    <button class="btn btn-info">Pas de livraison</button>
                                {% else %}
                                    <span class="btn btn-success">Livraison averti par email</span>
                                    <br/>
                                    <small><a href="{{ path('admin_order_delivery_resend', {id : item.id}) }}">Renvoyer
                                            email</a></small>
                                {% endif %}
                            {% endif %}

                        </td>
                        <td>
                            <a href="{{ path('admin_order_disabled', { id: item.id }) }}"
                               class="confirm-deletable btn btn-danger btn-xs"
                               data-toggle="tooltip"
                               title="Supprimer la commande {{ item.cart.invoiceAddress.lastname }} {{ item.cart.invoiceAddress.firstname }}"
                               data-title="Supprimer la commande  {{ item.cart.invoiceAddress.lastname }} {{ item.cart.invoiceAddress.firstname }}">
                                <i class="fa fa-2x fa-trash"></i></a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}
