<!doctype html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <title>Statistiques commandes</title>
    <style>
        body {
            margin: 0;
            color: #0f172a;
            font-family: Arial, sans-serif;
            font-size: 11px;
            line-height: 1.35;
            background: #fff;
        }

        .page {
            padding: 18px;
        }

        h1, h2, h3, p {
            margin: 0;
        }

        .header {
            margin-bottom: 18px;
        }

        .title {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 6px;
        }

        .meta {
            color: #475569;
            font-size: 11px;
        }

        .kpis {
            width: 100%;
            border-collapse: separate;
            border-spacing: 8px;
            margin: 0 -8px 18px;
        }

        .kpi {
            width: 25%;
            border: 1px solid #dbe2ea;
            border-radius: 10px;
            padding: 12px;
            vertical-align: top;
            background: #f8fafc;
        }

        .kpi-label {
            color: #64748b;
            font-size: 10px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.08em;
        }

        .kpi-value {
            margin-top: 8px;
            font-size: 20px;
            font-weight: 700;
            white-space: nowrap;
        }

        .kpi-subtle {
            margin-top: 8px;
            color: #475569;
            font-size: 10px;
        }

        .section {
            margin-top: 18px;
            page-break-inside: avoid;
        }

        .section-title {
            margin-bottom: 10px;
            font-size: 16px;
            font-weight: 700;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            border: 1px solid #dbe2ea;
            padding: 7px 8px;
            vertical-align: top;
        }

        th {
            background: #f8fafc;
            text-align: left;
        }

        .text-right {
            text-align: right;
            white-space: nowrap;
        }

        .subtle {
            color: #64748b;
            font-size: 10px;
        }

        .page-break {
            page-break-before: always;
        }
    </style>
</head>
<body>
<div class="page">
    <div class="header">
        <div class="title">Statistiques commandes</div>
        <div class="meta">
            Periode : {{ filters.paid_from_input }} au {{ filters.paid_to_input }}
            · Regroupement :
            {% if filters.group_by == 'day' %}
                Jour
            {% elseif filters.group_by == 'year' %}
                Annee
            {% else %}
                Mois
            {% endif %}
            · Top clients : {{ filters.top_clients_limit }}
        </div>
    </div>

    <table class="kpis">
        <tr>
            <td class="kpi">
                <div class="kpi-label">Chiffre d'affaire net</div>
                <div class="kpi-value">{{ (stats.summary.net_revenue_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                <div class="kpi-subtle">Brut - avoirs - frais de transaction</div>
            </td>
            <td class="kpi">
                <div class="kpi-label">Chiffre d'affaire brut</div>
                <div class="kpi-value">{{ (stats.summary.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                <div class="kpi-subtle">Produits : {{ (stats.summary.products_ttc / 100)|number_format(2, ',', ' ') }} €<br>Port : {{ (stats.summary.shipping_ttc / 100)|number_format(2, ',', ' ') }} €</div>
            </td>
            <td class="kpi">
                <div class="kpi-label">Commandes payees</div>
                <div class="kpi-value">{{ stats.summary.order_count }}</div>
                <div class="kpi-subtle">Clients distincts : {{ stats.summary.unique_customer_count }}<br>Clients recurrents : {{ stats.summary.repeat_customer_count }}</div>
            </td>
            <td class="kpi">
                <div class="kpi-label">Panier moyen TTC</div>
                <div class="kpi-value">{{ (stats.summary.average_basket_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                <div class="kpi-subtle">Port moyen : {{ (stats.summary.average_shipping_ttc / 100)|number_format(2, ',', ' ') }} €<br>Equivalence moyenne : {{ stats.summary.average_equivalence|number_format(2, ',', ' ') }}</div>
            </td>
        </tr>
    </table>

    <div class="section">
        <div class="section-title">Detail du chiffre d'affaire</div>
        <table>
            <thead>
            <tr>
                <th>Libelle</th>
                <th>Contexte</th>
                <th class="text-right">Montant</th>
            </tr>
            </thead>
            <tbody>
            {% for item in stats.revenue_breakdown %}
                <tr>
                    <td>{{ item.label }}</td>
                    <td class="subtle">{{ item.context }}</td>
                    <td class="text-right">
                        {% if item.tone == 'negative' %}-{% endif %}{{ (item.amount_ttc / 100)|number_format(2, ',', ' ') }} €
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <div class="section">
        <div class="section-title">Top clients</div>
        <p class="subtle" style="margin-bottom: 8px;">Total du top {{ filters.top_clients_limit }} : {{ (stats.top_clients_total_ttc / 100)|number_format(2, ',', ' ') }} €</p>
        <table>
            <thead>
            <tr>
                <th>Email</th>
                <th>Nom</th>
                <th>Prenom</th>
                <th class="text-right">Commandes</th>
                <th class="text-right">Panier moyen TTC</th>
                <th class="text-right">CA TTC</th>
                <th>Derniere commande</th>
            </tr>
            </thead>
            <tbody>
            {% for client in stats.top_clients %}
                <tr>
                    <td>{{ client.email }}</td>
                    <td>{{ client.lastname }}</td>
                    <td>{{ client.firstname }}</td>
                    <td class="text-right">{{ client.order_count }}</td>
                    <td class="text-right">{{ (client.average_basket_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ (client.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td>{% if client.last_order_at %}{{ client.last_order_at|date('d/m/Y H:i') }}{% else %}-{% endif %}</td>
                </tr>
            {% else %}
                <tr><td colspan="7">Aucun client sur la periode selectionnee.</td></tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <div class="section page-break">
        <div class="section-title">Produits vendus</div>
        <table>
            <thead>
            <tr>
                <th>Produit</th>
                <th>Type</th>
                <th>Unite</th>
                <th class="text-right">Quantite</th>
                <th class="text-right">Commandes</th>
                <th class="text-right">Prix moyen TTC</th>
                <th class="text-right">CA net TTC</th>
                <th class="text-right">Part CA</th>
            </tr>
            </thead>
            <tbody>
            {% for product in stats.sold_products %}
                <tr>
                    <td>{{ product.product_name }}</td>
                    <td>{{ product.type_name }}</td>
                    <td>{{ product.unit_label }}</td>
                    <td class="text-right">{{ product.quantity }}</td>
                    <td class="text-right">{{ product.order_count }}</td>
                    <td class="text-right">{{ (product.average_price_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ (product.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ product.share_revenue_percent|number_format(2, ',', ' ') }} %</td>
                </tr>
            {% else %}
                <tr><td colspan="8">Aucune ligne produit.</td></tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    {% if stats.option_summary.products_with_options_count > 0 %}
        <div class="section">
            <div class="section-title">Options produits</div>
            <p class="subtle" style="margin-bottom: 8px;">
                CA options : {{ (stats.option_summary.option_revenue_ttc / 100)|number_format(2, ',', ' ') }} €
                · Quantites avec option : {{ stats.option_summary.option_quantity }}
                · Commandes avec option : {{ stats.option_summary.option_order_count }}
                · Taux d'equipement : {{ stats.option_summary.attach_rate_percent|number_format(2, ',', ' ') }} %
            </p>
            <table>
                <thead>
                <tr>
                    <th>Produit</th>
                    <th class="text-right">Ventes produit</th>
                    <th class="text-right">Quantites avec option</th>
                    <th class="text-right">Taux d'equipement</th>
                    <th class="text-right">CA options TTC</th>
                    <th>Detail</th>
                </tr>
                </thead>
                <tbody>
                {% for product in stats.option_products %}
                    <tr>
                        <td>{{ product.product_name }}<br><span class="subtle">{{ product.type_name }}{% if product.unit_label != '-' %} · {{ product.unit_label }}{% endif %}</span></td>
                        <td class="text-right">{{ product.quantity }}</td>
                        <td class="text-right">{{ product.option_quantity }}</td>
                        <td class="text-right">{{ product.option_attach_rate_percent|number_format(2, ',', ' ') }} %</td>
                        <td class="text-right">{{ (product.option_revenue_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                        <td>
                            {% if product.options is not empty %}
                                {% for option in product.options %}
                                    <div>{{ option.name }} : {{ option.quantity }} vente(s), {{ (option.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                                {% endfor %}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}

    <div class="section">
        <div class="section-title">Chiffre d'affaire par type de produit</div>
        <table>
            <thead>
            <tr>
                <th>Type</th>
                <th class="text-right">Quantite</th>
                <th class="text-right">Commandes</th>
                <th class="text-right">Prix moyen TTC</th>
                <th class="text-right">CA net TTC</th>
                <th class="text-right">Part CA</th>
            </tr>
            </thead>
            <tbody>
            {% for type in stats.product_types %}
                <tr>
                    <td>{{ type.name }}</td>
                    <td class="text-right">{{ type.quantity }}</td>
                    <td class="text-right">{{ type.order_count }}</td>
                    <td class="text-right">{{ (type.average_price_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ (type.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ type.share_revenue_percent|number_format(2, ',', ' ') }} %</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <div class="section">
        <div class="section-title">Evolution</div>
        <table>
            <thead>
            <tr>
                <th>Periode</th>
                <th class="text-right">Commandes</th>
                <th class="text-right">CA TTC</th>
                <th class="text-right">Panier moyen TTC</th>
            </tr>
            </thead>
            <tbody>
            {% for label in stats.chart.categories %}
                <tr>
                    <td>{{ label }}</td>
                    <td class="text-right">{{ stats.chart.orders[loop.index0] }}</td>
                    <td class="text-right">{{ stats.chart.revenue[loop.index0]|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ stats.chart.average_basket[loop.index0]|number_format(2, ',', ' ') }} €</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <div class="section">
        <div class="section-title">Paiement et livraison</div>
        <table style="margin-bottom: 12px;">
            <thead>
            <tr>
                <th>Mode de paiement</th>
                <th class="text-right">Commandes</th>
                <th class="text-right">CA TTC</th>
                <th class="text-right">Frais transaction</th>
            </tr>
            </thead>
            <tbody>
            {% for method in stats.payment_methods %}
                <tr>
                    <td>{{ method.name ?: '-' }}</td>
                    <td class="text-right">{{ method.order_count }}</td>
                    <td class="text-right">{{ (method.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ (method.transaction_fees_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>

        <table>
            <thead>
            <tr>
                <th>Mode d'expedition / retrait</th>
                <th>Type</th>
                <th class="text-right">Commandes</th>
                <th class="text-right">Produits TTC</th>
                <th class="text-right">Port TTC</th>
                <th class="text-right">Total TTC</th>
            </tr>
            </thead>
            <tbody>
            {% for method in stats.delivery_methods %}
                <tr>
                    <td>{{ method.name ?: '-' }}</td>
                    <td>{{ method.type }}</td>
                    <td class="text-right">{{ method.order_count }}</td>
                    <td class="text-right">{{ (method.products_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ (method.shipping_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                    <td class="text-right">{{ (method.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
