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

{% block title %}Commandes - VitiCore{% endblock %}
{% block section_label %}Commerce{% endblock %}
{% block page_title %}Commandes{% endblock %}

{% block topbar_actions %}
    <a class="admin-button" href="{{ path('admin_order_stats') }}">Statistiques</a>
{% endblock %}

{% block content %}
    {% set per_page_options = [10, 25, 50, 100] %}
    {% set per_page = app.request.query.getInt('per_page', 25) %}
    {% if per_page not in per_page_options %}
        {% set per_page = 25 %}
    {% endif %}
    {% set total_items = items|length %}
    {% set page_count = total_items > 0 ? (total_items / per_page)|round(0, 'ceil') : 1 %}
    {% set current_page = app.request.query.getInt('page', 1) %}
    {% if current_page < 1 %}
        {% set current_page = 1 %}
    {% endif %}
    {% if current_page > page_count %}
        {% set current_page = page_count %}
    {% endif %}
    {% set offset = (current_page - 1) * per_page %}
    {% set paginated_items = items|slice(offset, per_page) %}
    {% set first_item = total_items > 0 ? offset + 1 : 0 %}
    {% set last_item = offset + paginated_items|length %}
    {% set sort_query = filters.query_parameters|merge({ per_page: per_page }) %}
    {% set page_query = sort_query|merge({ sort: sort, direction: direction }) %}
    {% set detail_return_query = {} %}
    {% if filters.search_input %}{% set detail_return_query = detail_return_query|merge({ return_search: filters.search_input }) %}{% endif %}
    {% if filters.status %}{% set detail_return_query = detail_return_query|merge({ return_status: filters.status }) %}{% endif %}
    {% if filters.payment_method_id %}{% set detail_return_query = detail_return_query|merge({ return_payment_method_id: filters.payment_method_id }) %}{% endif %}
    {% if filters.created_from_input %}{% set detail_return_query = detail_return_query|merge({ return_created_from: filters.created_from_input }) %}{% endif %}
    {% if filters.created_to_input %}{% set detail_return_query = detail_return_query|merge({ return_created_to: filters.created_to_input }) %}{% endif %}
    {% set detail_return_query = detail_return_query|merge({
        return_per_page: per_page,
        return_sort: sort,
        return_direction: direction,
        return_page: current_page
    }) %}

    <section class="admin-list-page">
        <form class="admin-filter-panel" method="get" action="{{ path('admin_order') }}">
            <input type="hidden" name="per_page" value="{{ per_page }}">
            <input type="hidden" name="sort" value="{{ sort }}">
            <input type="hidden" name="direction" value="{{ direction }}">
            <input class="admin-filter-toggle-input" type="checkbox" id="admin-order-filters-toggle">
            <label class="admin-filter-toggle" for="admin-order-filters-toggle">
                <span>Filtres</span>
                <strong>Afficher les filtres</strong>
            </label>

            <div class="admin-filter-body">
                <div class="admin-filter-header">
                    <div>
                        <p>Filtres</p>
                        <h2>Suivi des commandes</h2>
                    </div>
                    <div class="admin-filter-actions">
                        <a class="admin-button" href="{{ path('admin_order') }}">Reinitialiser</a>
                        <button class="admin-button admin-button-primary" type="submit">Filtrer</button>
                    </div>
                </div>

                <div class="admin-filter-grid">
                    <label>
                        <span>Recherche</span>
                        <input
                            type="search"
                            name="search"
                            value="{{ filters.search_input }}"
                            placeholder="Email, paiement, facture, client, reference..."
                        >
                    </label>

                    <label>
                        <span>Statut</span>
                        <select name="status">
                            <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 abandonnes</option>
                            <option value="failed" {% if filters.status == 'failed' %}selected{% endif %}>Paiement echoue</option>
                            <option value="canceled" {% if filters.status == 'canceled' %}selected{% endif %}>Paiement annule</option>
                            <option value="paid" {% if filters.status == 'paid' %}selected{% endif %}>Payees</option>
                            <option value="delivered" {% if filters.status == 'delivered' %}selected{% endif %}>Livrees</option>
                            <option value="paid_not_delivered" {% if filters.status == 'paid_not_delivered' %}selected{% endif %}>Payees non livrees</option>
                        </select>
                    </label>

                    <label>
                        <span>Mode de paiement</span>
                        <select name="payment_method_id">
                            <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>
                    </label>

                    <label>
                        <span>Du</span>
                        <input type="date" name="created_from" value="{{ filters.created_from_input }}">
                    </label>

                    <label>
                        <span>Au</span>
                        <input type="date" name="created_to" value="{{ filters.created_to_input }}">
                    </label>
                </div>
            </div>
        </form>

        <section class="admin-table-panel">
            <div class="admin-table-actionbar">
                <a class="admin-button admin-button-primary" href="{{ path('admin_order_export_excel', filters.query_parameters) }}">
                    Export Excel
                </a>
                <a class="admin-button" href="{{ path('admin_order_purchase_orders_download', filters.query_parameters) }}">
                    {% if invoice_feature_enabled %}Telecharger les documents PDF{% else %}Telecharger les bons de commande{% endif %}
                </a>
            </div>

            <div class="admin-table-heading">
                <div>
                    <p>Liste</p>
                    <h2>{{ total_items }} commande{{ total_items > 1 ? 's' : '' }}</h2>
                </div>
                <div class="admin-table-tools">
                    <span>{{ first_item }}-{{ last_item }} sur {{ total_items }}</span>
                    <form class="admin-per-page-form" method="get" action="{{ path('admin_order') }}">
                        {% for key, value in filters.query_parameters %}
                            <input type="hidden" name="{{ key }}" value="{{ value }}">
                        {% endfor %}
                        <input type="hidden" name="sort" value="{{ sort }}">
                        <input type="hidden" name="direction" value="{{ direction }}">
                        <label>
                            <span>Lignes</span>
                            <select name="per_page" onchange="this.form.submit()">
                                {% for option in per_page_options %}
                                    <option value="{{ option }}" {% if per_page == option %}selected{% endif %}>{{ option }}</option>
                                {% endfor %}
                            </select>
                        </label>
                    </form>
                </div>
            </div>

            <div class="admin-table-scroll">
                <table class="admin-data-table">
                    <thead>
                    <tr>
                        <th>
                            {% set number_direction = sort == 'number' and direction == 'asc' ? 'desc' : 'asc' %}
                            <a class="admin-sort-link {{ sort == 'number' ? 'is-active' : '' }}" href="{{ path('admin_order', sort_query|merge({ sort: 'number', direction: number_direction })) }}">
                                #
                                {% if sort == 'number' and direction == 'asc' %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m5 12 7-7 7 7"></path><path d="M12 19V5"></path></svg>
                                {% elseif sort == 'number' %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14"></path><path d="m19 12-7 7-7-7"></path></svg>
                                {% else %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m21 16-4 4-4-4"></path><path d="M17 20V4"></path><path d="m3 8 4-4 4 4"></path><path d="M7 4v16"></path></svg>
                                {% endif %}
                            </a>
                        </th>
                        <th>Reference</th>
                        <th>Statut</th>
                        <th>
                            {% set date_direction = sort == 'date' and direction == 'asc' ? 'desc' : 'asc' %}
                            <a class="admin-sort-link {{ sort == 'date' ? 'is-active' : '' }}" href="{{ path('admin_order', sort_query|merge({ sort: 'date', direction: date_direction })) }}">
                                Date
                                {% if sort == 'date' and direction == 'asc' %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m5 12 7-7 7 7"></path><path d="M12 19V5"></path></svg>
                                {% elseif sort == 'date' %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14"></path><path d="m19 12-7 7-7-7"></path></svg>
                                {% else %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m21 16-4 4-4-4"></path><path d="M17 20V4"></path><path d="m3 8 4-4 4 4"></path><path d="M7 4v16"></path></svg>
                                {% endif %}
                            </a>
                        </th>
                        <th>Client</th>
                        <th>Email</th>
                        <th>Code partenaire</th>
                        <th>
                            {% set total_direction = sort == 'total' and direction == 'asc' ? 'desc' : 'asc' %}
                            <a class="admin-sort-link {{ sort == 'total' ? 'is-active' : '' }}" href="{{ path('admin_order', sort_query|merge({ sort: 'total', direction: total_direction })) }}">
                                Total
                                {% if sort == 'total' and direction == 'asc' %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m5 12 7-7 7 7"></path><path d="M12 19V5"></path></svg>
                                {% elseif sort == 'total' %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14"></path><path d="m19 12-7 7-7-7"></path></svg>
                                {% else %}
                                    <svg class="admin-sort-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m21 16-4 4-4-4"></path><path d="M17 20V4"></path><path d="m3 8 4-4 4 4"></path><path d="M7 4v16"></path></svg>
                                {% endif %}
                            </a>
                        </th>
                        <th>Paiement</th>
                        <th>Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for item in paginated_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 has_credit = invoice and invoice.credit %}

                        <tr>
                            <td>{{ order_numbers[item.id.toString]|default(offset + loop.index) }}</td>
                            <td>
                                <strong>{{ item.token ?: '-' }}</strong>
                            </td>
                            <td>
                                {% if checkout_status == 'paid' %}
                                    <span class="admin-status-stack">
                                        <span class="admin-status admin-status-success">Payee</span>
                                        {% if has_credit %}
                                            <span class="admin-status admin-status-warning">Avoir</span>
                                        {% endif %}
                                        {% if item.isDelivery %}
                                            <span class="admin-status admin-status-info">Expediee</span>
                                        {% endif %}
                                    </span>
                                    {% if cart_checkout and cart_checkout.payedAt %}
                                        <small>{{ cart_checkout.payedAt|date('d/m/Y H:i') }}</small>
                                    {% endif %}
                                {% elseif checkout_status == 'initiated' %}
                                    <span class="admin-status admin-status-info">Paiement initie</span>
                                    {% if cart_checkout and cart_checkout.initiatedAt %}
                                        <small>{{ cart_checkout.initiatedAt|date('d/m/Y H:i') }}</small>
                                    {% endif %}
                                {% elseif checkout_status == 'failed' %}
                                    <span class="admin-status admin-status-danger">Paiement echoue</span>
                                    {% if payment_failure_code %}
                                        <small>Code : {{ payment_failure_code }}</small>
                                    {% elseif payment_failure_message %}
                                        <small>{{ payment_failure_message }}</small>
                                    {% endif %}
                                {% elseif checkout_status == 'canceled' %}
                                    <span class="admin-status admin-status-muted">Paiement annule</span>
                                    {% if payment_failure_code %}
                                        <small>Code : {{ payment_failure_code }}</small>
                                    {% elseif payment_failure_message %}
                                        <small>{{ payment_failure_message }}</small>
                                    {% endif %}
                                {% elseif checkout_status == 'abandoned' %}
                                    <span class="admin-status admin-status-warning">Panier abandonne</span>
                                    {% if item.abandonedAt %}
                                        <small>{{ item.abandonedAt|date('d/m/Y H:i') }}</small>
                                    {% endif %}
                                {% elseif checkout_status == 'ordered' %}
                                    <span class="admin-status admin-status-warning">En attente</span>
                                    {% if cart_checkout and cart_checkout.orderedAt %}
                                        <small>{{ cart_checkout.orderedAt|date('d/m/Y H:i') }}</small>
                                    {% endif %}
                                {% else %}
                                    <span class="admin-status admin-status-warning">En cours</span>
                                {% endif %}
                            </td>
                            <td>
                                {{ item.createdAt|date('d/m/Y') }}<br>
                                <small>{{ item.createdAt|date('H:i') }}</small>
                            </td>
                            <td>{{ customer_name ?: '-' }}</td>
                            <td>{{ item.getEmail() ?: '-' }}</td>
                            <td>{{ item.groupCode ?: '-' }}</td>
                            <td>
                                {% if item.total is not null %}
                                    {{ (item.total / 100)|number_format(2, ',', ' ') }} €
                                {% else %}
                                    -
                                {% endif %}
                            </td>
                            <td>
                                {% if payment_method %}
                                    {{ payment_method.method|trans }}
                                {% else %}
                                    -
                                {% endif %}
                            </td>
                            <td>
                                <a class="admin-row-action" href="{{ path('admin_order_order', { id: item.id }|merge(detail_return_query)) }}">
                                    Details
                                </a>
                            </td>
                        </tr>
                    {% else %}
                        <tr>
                            <td colspan="10" class="admin-empty-cell">Aucune commande trouvee.</td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>

            {% if page_count > 1 %}
                {% set previous_page = current_page - 1 %}
                {% set next_page = current_page + 1 %}
                {% set first_page_link = current_page - 2 < 2 ? 2 : current_page - 2 %}
                {% set last_page_link = current_page + 2 > page_count - 1 ? page_count - 1 : current_page + 2 %}

                <nav class="admin-pagination" aria-label="Pagination des commandes">
                    {% if current_page > 1 %}
                        <a href="{{ path('admin_order', page_query|merge({ page: previous_page })) }}">Precedent</a>
                    {% else %}
                        <span>Precedent</span>
                    {% endif %}

                    <div>
                        {% if current_page == 1 %}
                            <strong>1</strong>
                        {% else %}
                            <a href="{{ path('admin_order', page_query|merge({ page: 1 })) }}">1</a>
                        {% endif %}

                        {% if first_page_link > 2 %}
                            <span>...</span>
                        {% endif %}

                        {% if first_page_link <= last_page_link %}
                            {% for page in first_page_link..last_page_link %}
                                {% if page == current_page %}
                                    <strong>{{ page }}</strong>
                                {% else %}
                                    <a href="{{ path('admin_order', page_query|merge({ page: page })) }}">{{ page }}</a>
                                {% endif %}
                            {% endfor %}
                        {% endif %}

                        {% if last_page_link < page_count - 1 %}
                            <span>...</span>
                        {% endif %}

                        {% if current_page == page_count %}
                            <strong>{{ page_count }}</strong>
                        {% else %}
                            <a href="{{ path('admin_order', page_query|merge({ page: page_count })) }}">{{ page_count }}</a>
                        {% endif %}
                    </div>

                    {% if current_page < page_count %}
                        <a href="{{ path('admin_order', page_query|merge({ page: next_page })) }}">Suivant</a>
                    {% else %}
                        <span>Suivant</span>
                    {% endif %}
                </nav>
            {% endif %}
        </section>
    </section>
{% endblock %}
