<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Recapitulatif {{ '%02d'|format(month) }}/{{ year }}</title>
    <style>
        @page { size: A4 landscape; margin: 20px; }
        body { font-family: Helvetica, Arial, sans-serif; font-size: 10px; color: #111; margin: 0; }
        .header { background: #111827; color: #fff; padding: 16px 24px; margin-bottom: 16px; }
        .header h1 { font-size: 18px; margin: 0 0 4px; }
        .header p { font-size: 10px; margin: 0; opacity: 0.7; }
        .stats { margin-bottom: 16px; }
        .stats td { padding: 8px 16px; border: 2px solid #111827; text-align: center; font-weight: bold; }
        .stats .label { font-size: 8px; text-transform: uppercase; letter-spacing: 1px; color: #666; }
        .stats .value { font-size: 16px; }
        table.orders { width: 100%; border-collapse: collapse; }
        table.orders th { background: #111827; color: #fff; padding: 6px 8px; font-size: 8px; text-transform: uppercase; letter-spacing: 1px; text-align: left; }
        table.orders td { padding: 5px 8px; border-bottom: 1px solid #eee; font-size: 9px; }
        table.orders tr:nth-child(even) { background: #f9fafb; }
        .footer { margin-top: 16px; font-size: 8px; color: #999; text-align: center; }
    </style>
</head>
<body>
    <div class="header">
        <h1>Recapitulatif mensuel — {{ '%02d'|format(month) }}/{{ year }}</h1>
        <p>{% if organizer %}{{ organizer.companyName ?? (organizer.firstName ~ ' ' ~ organizer.lastName) }} — {% endif %}E-Ticket by E-Cosplay</p>
    </div>

    <table class="stats">
        <tr>
            <td>
                <div class="label">Commandes</div>
                <div class="value">{{ stats.orders|length }}</div>
            </td>
            <td>
                <div class="label">Total HT</div>
                <div class="value" style="color: #4f46e5;">{{ stats.totalHT|number_format(2, ',', ' ') }} &euro;</div>
            </td>
            <td>
                <div class="label">Commission E-Ticket</div>
                <div class="value" style="color: #dc2626;">{{ stats.commissionEticket|number_format(2, ',', ' ') }} &euro;</div>
            </td>
            <td>
                <div class="label">Commission Stripe</div>
                <div class="value" style="color: #dc2626;">{{ stats.commissionStripe|number_format(2, ',', ' ') }} &euro;</div>
            </td>
            {% if not isAdmin %}
            <td>
                <div class="label">Net percu</div>
                <div class="value" style="color: #16a34a;">{{ stats.netOrga|number_format(2, ',', ' ') }} &euro;</div>
            </td>
            {% else %}
            <td>
                <div class="label">Net E-Ticket</div>
                <div class="value" style="color: #16a34a;">{{ stats.commissionEticket|number_format(2, ',', ' ') }} &euro;</div>
            </td>
            {% endif %}
        </tr>
    </table>

    <table class="orders">
        <thead>
            <tr>
                <th>Commande</th>
                <th>Date</th>
                {% if isAdmin %}<th>Organisateur</th>{% endif %}
                <th>Evenement</th>
                <th>Acheteur</th>
                <th>Email</th>
                <th>Billets</th>
                <th style="text-align: right;">Total HT</th>
                <th style="text-align: right;">Com E-Ticket</th>
                <th style="text-align: right;">Com Stripe</th>
                {% if not isAdmin %}<th style="text-align: right;">Net percu</th>{% endif %}
            </tr>
        </thead>
        <tbody>
            {% for order in stats.orders %}
            {% set ht = order.totalHTDecimal %}
            {% set rate = order.event.account.commissionRate ?? 3 %}
            {% set eticketFee = ht * rate / 100 %}
            {% set stripeFee = ht * 0.015 + 0.25 %}
            {% set net = ht - eticketFee - stripeFee %}
            <tr>
                <td style="font-weight: bold;">{{ order.orderNumber }}</td>
                <td>{{ order.paidAt ? order.paidAt|date('d/m/Y') : '—' }}</td>
                {% if isAdmin %}<td>{{ order.event.account.companyName ?? order.event.account.firstName }}</td>{% endif %}
                <td>{{ order.event.title }}</td>
                <td>{{ order.firstName }} {{ order.lastName }}</td>
                <td>{{ order.email }}</td>
                <td>{% for item in order.items %}{{ item.billetName }} x{{ item.quantity }}{% if not loop.last %}, {% endif %}{% endfor %}</td>
                <td style="text-align: right; font-weight: bold;">{{ ht|number_format(2, ',', ' ') }} &euro;</td>
                <td style="text-align: right; color: #dc2626;">{{ eticketFee|number_format(2, ',', ' ') }} &euro;</td>
                <td style="text-align: right; color: #dc2626;">{{ stripeFee|number_format(2, ',', ' ') }} &euro;</td>
                {% if not isAdmin %}<td style="text-align: right; color: #16a34a; font-weight: bold;">{{ net|number_format(2, ',', ' ') }} &euro;</td>{% endif %}
            </tr>
            {% else %}
            <tr><td colspan="{{ isAdmin ? 10 : 10 }}" style="text-align: center; padding: 20px; color: #999;">Aucune commande ce mois-ci.</td></tr>
            {% endfor %}
        </tbody>
    </table>

    <div class="footer">
        Genere le {{ "now"|date('d/m/Y H:i') }} — E-Ticket by E-Cosplay — contact@e-cosplay.fr
    </div>
</body>
</html>
