{% macro address(address, email) %}
    {% if address %}
        {% set full_name = ((address.firstname ?: '') ~ ' ' ~ (address.lastname ?: ''))|trim %}
        {% if full_name %}<strong>{{ full_name }}</strong><br>{% endif %}
        {% if address.company %}{{ address.company }}<br>{% endif %}
        {% if address.street %}{{ address.street }}<br>{% endif %}
        {% if address.postal or address.city %}{{ address.postal }} {{ address.city }}<br>{% endif %}
        {% if address.country %}{{ address.country|upper }}<br>{% endif %}
        {% if email %}<small>{{ email }}</small><br>{% endif %}
        {% if address.phone %}<small>{{ address.phone }}</small>{% endif %}
    {% else %}
        -
    {% endif %}
{% endmacro %}

{% import _self as customer_order %}

{% set summary = order_insights.summary %}

<div class="row">
    <div class="col-sm-6 col-lg-3">
        <div class="panel panel-primary">
            <div class="panel-heading">{{ 'admin.client.order_insights.order_count'|trans }}</div>
            <div class="panel-body"><strong style="font-size: 24px;">{{ summary.order_count }}</strong></div>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="panel panel-info">
            <div class="panel-heading">{{ 'admin.client.order_insights.average_basket'|trans }}</div>
            <div class="panel-body">
                <strong style="font-size: 24px;">{{ summary.average_basket_ttc is not null ? (summary.average_basket_ttc|price_format ~ ' €') : '-' }}</strong>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="panel panel-success">
            <div class="panel-heading">{{ 'admin.client.order_insights.total_spent'|trans }}</div>
            <div class="panel-body"><strong style="font-size: 24px;">{{ summary.total_spent_ttc|price_format }} €</strong></div>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="panel panel-default">
            <div class="panel-heading">{{ 'admin.client.order_insights.total_quantity'|trans }}</div>
            <div class="panel-body"><strong style="font-size: 24px;">{{ summary.total_quantity }}</strong></div>
        </div>
    </div>
</div>

{% if summary.first_order_at and summary.last_order_at %}
    <p class="text-muted">
        {{ 'admin.client.order_insights.period'|trans({
            '%first%': summary.first_order_at|date('d/m/Y'),
            '%last%': summary.last_order_at|date('d/m/Y')
        }) }}
        {% if summary.priced_order_count < summary.order_count %}
            — {{ 'admin.client.order_insights.partial_pricing'|trans({ '%count%': summary.priced_order_count }) }}
        {% endif %}
    </p>
{% endif %}

<div class="panel panel-default">
    <div class="panel-heading">{{ 'admin.client.order_insights.cuvees_title'|trans }}</div>
    <div class="table-responsive">
        <table class="table table-bordered table-striped" width="100%" cellspacing="0">
            <thead>
            <tr>
                <th>{{ 'admin.client.order_insights.cuvee'|trans }}</th>
                <th>{{ 'admin.client.order_insights.type_format'|trans }}</th>
                <th>{{ 'admin.client.order_insights.quantity'|trans }}</th>
                <th>{{ 'admin.client.order_insights.orders'|trans }}</th>
                <th>{{ 'admin.client.order_insights.amount'|trans }}</th>
            </tr>
            </thead>
            <tbody>
            {% for cuvee in order_insights.cuvees %}
                <tr>
                    <td>{{ cuvee.name ?: ('admin.client.order_insights.unknown_product'|trans) }}</td>
                    <td>
                        {{ cuvee.type_name ?: '-' }}
                        {% if cuvee.unit_label %}<br><small class="text-muted">{{ cuvee.unit_label }}</small>{% endif %}
                    </td>
                    <td>{{ cuvee.quantity }}</td>
                    <td>{{ cuvee.order_count }}</td>
                    <td>{{ cuvee.total_ttc|price_format }} €</td>
                </tr>
            {% else %}
                <tr>
                    <td colspan="5" class="text-center text-muted">{{ 'admin.client.order_insights.no_cuvee'|trans }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">{{ 'admin.client.order_insights.delivery_methods_title'|trans }}</div>
    <div class="table-responsive">
        <table class="table table-bordered table-striped" width="100%" cellspacing="0">
            <thead>
            <tr>
                <th>{{ 'admin.client.order_insights.delivery_method'|trans }}</th>
                <th>{{ 'admin.client.order_insights.delivery_kind'|trans }}</th>
                <th>{{ 'admin.client.order_insights.orders'|trans }}</th>
                <th>{{ 'admin.client.order_insights.amount'|trans }}</th>
            </tr>
            </thead>
            <tbody>
            {% for delivery_method in order_insights.delivery_methods %}
                <tr>
                    <td>{{ delivery_method.name ?: ('admin.client.order_insights.unknown_delivery_method'|trans) }}</td>
                    <td>
                        <span class="label {{ delivery_method.kind == 'address' ? 'label-info' : (delivery_method.kind == 'pickup' ? 'label-warning' : 'label-default') }}">
                            {{ ('admin.client.order_insights.delivery_kind_' ~ delivery_method.kind)|trans }}
                        </span>
                    </td>
                    <td>{{ delivery_method.order_count }}</td>
                    <td>{{ delivery_method.total_ttc|price_format }} €</td>
                </tr>
            {% else %}
                <tr>
                    <td colspan="4" class="text-center text-muted">{{ 'admin.client.order_insights.no_delivery_method'|trans }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">{{ 'admin.client.order_insights.orders_title'|trans }}</div>
    <div class="table-responsive">
        <table class="table table-bordered" width="100%" cellspacing="0">
            <thead>
            <tr>
                <th>#</th>
                <th>{{ 'admin.client.order_insights.date'|trans }}</th>
                <th>{{ 'admin.client.order_insights.reference'|trans }}</th>
                <th>{{ 'admin.client.order_insights.total'|trans }}</th>
                <th>{{ 'admin.client.order_insights.payment'|trans }}</th>
                <th>{{ 'admin.client.order_insights.status_label'|trans }}</th>
                <th>{{ 'admin.client.order_insights.invoice_address'|trans }}</th>
                <th>{{ 'admin.client.order_insights.delivery_address'|trans }}</th>
                <th>{{ 'admin.client.order_insights.actions'|trans }}</th>
            </tr>
            </thead>
            <tbody>
            {% for order in order_insights.orders %}
                {% set checkout = order.cartCheckout %}
                {% set order_date = checkout and checkout.orderedAt ? checkout.orderedAt : (checkout and checkout.payedAt ? checkout.payedAt : order.createdAt) %}
                {% set invoice_address = order.invoiceAddress ?: order.deliveryAddress %}
                {% set delivery_address = order.deliveryAddress ?: order.invoiceAddress %}
                <tr>
                    <td>{{ loop.index }}</td>
                    <td>{{ order_date|date('d/m/Y H:i') }}</td>
                    <td>{{ order.token ?: '-' }}</td>
                    <td>{{ order.total is not null ? (order.total|price_format ~ ' €') : '-' }}</td>
                    <td>{{ order.paymentMethod ? order.paymentMethod.method|trans : '-' }}</td>
                    <td>
                        {% if order.isCheckoutPayed %}
                            <span class="label label-success">{{ 'admin.client.order_insights.status.paid'|trans }}</span>
                        {% else %}
                            <span class="label label-info">{{ 'admin.client.order_insights.status.confirmed'|trans }}</span>
                        {% endif %}
                    </td>
                    <td>{{ customer_order.address(invoice_address, invoice_address ? invoice_address.email : order.email) }}</td>
                    <td>{{ customer_order.address(delivery_address, delivery_address ? delivery_address.email : order.email) }}</td>
                    <td>
                        <a href="{{ path('admin_order_order', { id: order.id }) }}" class="btn btn-info btn-sm">
                            <i class="fas fa-search"></i> {{ 'admin.client.order_insights.order_details'|trans }}
                        </a>
                    </td>
                </tr>
            {% else %}
                <tr>
                    <td colspan="9" class="text-center text-muted">{{ 'admin.client.order_insights.no_order'|trans }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>
