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

{% block content %}
    <div class="panel panel-default">
        <div class="panel-heading">
            Filtres et exports
        </div>
        <div class="panel-body">
            <form method="get" action="{{ path('admin_order') }}">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="search">Recherche texte</label>
                            <input
                                type="search"
                                name="search"
                                id="search"
                                class="form-control"
                                value="{{ filters.search_input }}"
                                placeholder="Email, ref. paiement, facture, client, reference..."
                            >
                        </div>
                    </div>
                    <div class="col-md-6">
                        <p class="help-block" style="margin-top: 30px;">
                            Recherche sur email, client, reference de commande, facture, paiement, code partenaire, suivi, etc.
                        </p>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="status">Statut</label>
                            <select name="status" id="status" class="form-control">
                                <option value="all" {% if filters.status == 'all' %}selected{% endif %}>Toutes</option>
                                <option value="pending" {% if filters.status == 'pending' %}selected{% endif %}>En cours</option>
                                <option value="abandoned" {% if filters.status == 'abandoned' %}selected{% endif %}>Paniers abandonnés</option>
                                <option value="failed" {% if filters.status == 'failed' %}selected{% endif %}>Paiement échoué</option>
                                <option value="canceled" {% if filters.status == 'canceled' %}selected{% endif %}>Paiement annulé</option>
                                <option value="offline_pending" {% if filters.status == 'offline_pending' %}selected{% endif %}>
                                    {{ 'admin.order.filters.status.offline_pending'|trans }}
                                </option>
                                <option value="paid" {% if filters.status == 'paid' %}selected{% endif %}>Payées</option>
                                <option value="delivered" {% if filters.status == 'delivered' %}selected{% endif %}>Livrées</option>
                                <option value="paid_not_delivered" {% if filters.status == 'paid_not_delivered' %}selected{% endif %}>Payées non livrées</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="payment_method_id">Mode de paiement</label>
                            <select name="payment_method_id" id="payment_method_id" class="form-control">
                                <option value="">Tous</option>
                                {% for payment_method in payment_methods %}
                                    <option value="{{ payment_method.id }}" {% if filters.payment_method_id == payment_method.id.toString %}selected{% endif %}>
                                        {{ payment_method.method|trans }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="created_from">Du</label>
                            <input
                                type="date"
                                name="created_from"
                                id="created_from"
                                class="form-control"
                                value="{{ filters.created_from_input }}"
                            >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="created_to">Au</label>
                            <input
                                type="date"
                                name="created_to"
                                id="created_to"
                                class="form-control"
                                value="{{ filters.created_to_input }}"
                            >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <label>&nbsp;</label>
                        <div>
                            <button type="submit" class="btn btn-primary btn-block">
                                Filtrer
                            </button>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12">
                        <a href="{{ path('admin_order') }}" class="btn btn-default">
                            Reinitialiser
                        </a>
                        <a href="{{ path('admin_order_stats') }}" class="btn btn-primary">
                            Statistiques
                        </a>
                        <a href="{{ path('admin_order_export_excel', filters.query_parameters) }}" class="btn btn-success">
                            Export Excel
                        </a>
                        <a href="{{ path('admin_order_purchase_orders_download', filters.query_parameters) }}" class="btn btn-info">
                            {% if invoice_feature_enabled %}Telecharger les documents PDF{% else %}Telecharger les bons de commande{% endif %}
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            Suivi des commandes ({{ items|length }})
        </div>
        <div class="table-responsive">
            <table class="table table-bordered dataTable" width="100%" cellspacing="0" data-order='[[0, "desc"]]'>
                <thead>
                <tr>
                    <th>#</th>
                    <th>Créé le</th>
                    <th>Client</th>
                    <th>Email</th>
                    <th>Référence</th>
                    <th>Code Partenaire</th>
                    <th>Total</th>
                    <th>Paiement</th>
                    <th>Statut</th>
                    <th>Actions</th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th>#</th>
                    <th>Créé le</th>
                    <th>Client</th>
                    <th>Email</th>
                    <th>Référence</th>
                    <th>Code Partenaire</th>
                    <th>Total</th>
                    <th>Paiement</th>
                    <th>Statut</th>
                    <th>Actions</th>
                </tr>
                </tfoot>
                <tbody>
                {% for item in items %}
                    {% set cart_checkout = item.cartCheckout %}
                    {% set invoice = item.invoice %}
                    {% set payment_method = item.paymentMethod ?: (cart_checkout ? cart_checkout.paymentMethod : null) %}
                    {% set customer_name = (item.lastname ~ ' ' ~ item.firstname)|trim %}
                    {% set checkout_status = item.checkoutStatus %}
                    {% set payment_failure_code = item.paymentFailureCode %}
                    {% set payment_failure_message = item.paymentFailureMessage %}
                    {% set is_online_payment = payment_method ? payment_method.checkoutOnlineMethod : true %}
                    {% set has_credit = invoice and invoice.credit %}
                    <tr>
                        <td data-order="{{ loop.revindex }}">{{ loop.revindex }}</td>
                        <td>{{ item.createdAt|date('d-m-Y') }}<br/>{{ item.createdAt|date('H:i:s') }}</td>
                        <td>{{ customer_name ?: '-' }}</td>
                        <td>{{ item.getEmail() ?: '-' }}</td>
                        <td>{{ item.token ?: '-' }}</td>
                        <td>{{ item.groupCode ?: '-' }}</td>
                        <td>
                            {% if item.total is not null %}
                                {{ item.total / 100 }} €
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>
                            {% set code_error = null %}
                            {% if payment_method %}
                                {{ payment_method.method|trans }}

                                {% if cart_checkout and cart_checkout.response is not empty %}
                                    {% if payment_method.method == 'systempay' or payment_method.method == 'sogecommerce' %}
                                        {% if cart_checkout.response.vads_payment_error is defined and cart_checkout.response.vads_result != 00 %}
                                            {% set code_error = cart_checkout.response.vads_payment_error %}
                                        {% endif %}
                                    {% elseif payment_method.method == 'stripe' %}
                                        {% if cart_checkout.response.last_payment_error.code is defined and cart_checkout.response.last_payment_error.code %}
                                            {% set code_error = cart_checkout.response.last_payment_error.code %}
                                        {% elseif cart_checkout.response.stripe_checkout_sync.last_payment_error.code is defined and cart_checkout.response.stripe_checkout_sync.last_payment_error.code %}
                                            {% set code_error = cart_checkout.response.stripe_checkout_sync.last_payment_error.code %}
                                        {% elseif cart_checkout.response.object.payment_intent.last_payment_error.code is defined and cart_checkout.response.object.payment_intent.last_payment_error.code %}
                                            {% set code_error = cart_checkout.response.object.payment_intent.last_payment_error.code %}
                                        {% elseif cart_checkout.response.session.payment_intent.last_payment_error.code is defined and cart_checkout.response.session.payment_intent.last_payment_error.code %}
                                            {% set code_error = cart_checkout.response.session.payment_intent.last_payment_error.code %}
                                        {% endif %}
                                    {% elseif payment_method.method == 'e-transactions' %}
                                        {% if cart_checkout.response.Erreur is defined and cart_checkout.response.Erreur != 000000 %}
                                            {% set code_error = cart_checkout.response.Erreur %}
                                        {% endif %}
                                    {% elseif payment_method.method == 'monetico' %}
                                        {% if cart_checkout.response.vads_result is defined and cart_checkout.response.vads_result != 00 %}
                                            {% set code_error = cart_checkout.response.vads_payment_error %}
                                        {% endif %}
                                    {% elseif payment_method.method == 'sherlock2' or payment_method.method == 'sherlock' %}
                                        {% if cart_checkout.response.Data is defined and cart_checkout.response.Data is not null %}
                                            {% set response_data = {} %}
                                            {% for response_row in cart_checkout.response.Data|split('|')|map((value, key) => value|split('=')) %}
                                                {% set response_data = response_data|merge({ (response_row[0]): (response_row[1] ?? null) }) %}
                                            {% endfor %}
                                            {% if response_data.responseCode is defined and response_data.responseCode != 00 %}
                                                {% set code_error = response_data.responseCode %}
                                            {% endif %}
                                        {% endif %}
                                    {% endif %}
                                {% endif %}

                                {% if code_error is null and payment_failure_code %}
                                    {% set code_error = payment_failure_code %}
                                {% endif %}

                                {% if payment_method.method == 'stripe' %}
                                    {% set stripe_reference = cart_checkout.response.payment_reference|default(
                                        cart_checkout.response.stripe_checkout_sync.payment_reference|default(
                                            cart_checkout.response.metadata.payment_reference|default(
                                                cart_checkout.response.session.metadata.payment_reference|default(
                                                    cart_checkout.response.stripe_checkout_sync.client_reference_id|default(null)
                                                )
                                            )
                                        )
                                    ) %}
                                    {% if stripe_reference %}
                                        <br/>
                                        <small><strong>Réf. paiement :</strong> {{ stripe_reference }}</small>
                                    {% endif %}
                                {% endif %}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>
                            {% if checkout_status == 'paid' %}
                                <span class="label label-success">Payée</span>
                                {% if has_credit %}
                                    <br/>
                                    <span class="label label-warning">Avoir</span>
                                {% endif %}
                                {% if item.isDelivery %}
                                    <br/>
                                    <span class="label label-info">Expédiée</span>
                                {% endif %}
                                <br/>
                                <small>{{ cart_checkout.payedAt|date('d-m-Y H:i') }}</small>
                            {% elseif checkout_status == 'initiated' %}
                                <span class="label label-primary">Paiement initié</span>
                                {% if cart_checkout and cart_checkout.initiatedAt %}
                                    <br/>
                                    <small>{{ cart_checkout.initiatedAt|date('d-m-Y H:i') }}</small>
                                {% endif %}
                            {% elseif checkout_status == 'failed' %}
                                <span class="label label-danger">Paiement échoué</span>
                                {% if code_error %}
                                    <br/>
                                    <small><strong>Code erreur :</strong> {{ code_error }}</small>
                                {% elseif payment_failure_message %}
                                    <br/>
                                    <small>{{ payment_failure_message }}</small>
                                {% endif %}
                            {% elseif checkout_status == 'canceled' %}
                                <span class="label label-default">Paiement annulé</span>
                                {% if code_error %}
                                    <br/>
                                    <small><strong>Code erreur :</strong> {{ code_error }}</small>
                                {% elseif payment_failure_message %}
                                    <br/>
                                    <small>{{ payment_failure_message }}</small>
                                {% endif %}
                            {% elseif checkout_status == 'abandoned' %}
                                <span class="label label-warning">Panier abandonné</span>
                                {% if item.abandonedAt %}
                                    <br/>
                                    <small>{{ item.abandonedAt|date('d-m-Y H:i') }}</small>
                                {% endif %}
                            {% elseif checkout_status == 'ordered' %}
                                <span class="label label-order-validated">Paiement en attente de validation</span>
                                {% if payment_method and not payment_method.checkoutOnlineMethod %}
                                    <br/>
                                    <small>{{ payment_method.method|trans }}</small>
                                {% endif %}
                                {% if cart_checkout and cart_checkout.orderedAt %}
                                    <br/>
                                    <small>{{ cart_checkout.orderedAt|date('d-m-Y H:i') }}</small>
                                {% endif %}
                            {% else %}
                                <span class="label label-warning">En cours</span>
                                {% if not cart_checkout %}
                                    <br/>
                                    <small>Paiement non initialisé</small>
                                {% endif %}
                            {% endif %}
                        </td>
                        <td>
                            <a href="{{ path('admin_order_order', { id: item.id }) }}" class="btn btn-info btn-sm">
                                <i class="fas fa-search"></i> Détails
                            </a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}
