<div class="page-header">
    <div>
        <h1><i class="bi bi-people me-2"></i>Customer History</h1>
        <p>Spending and order history per customer.</p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= url('reports/sales') ?>" class="btn btn-outline-secondary btn-sm">Item Sales</a>
        <a href="<?= url('reports/invoices') ?>" class="btn btn-outline-secondary btn-sm">Order Report</a>
    </div>
</div>

<?php include __DIR__ . '/_summary_cards.php'; ?>

<?php $formAction = url('reports/customers'); ?>
<?php include __DIR__ . '/_filters.php'; ?>

<div class="card">
    <div class="card-header">
        <i class="bi bi-person-lines-fill text-accent"></i>
        <h6 class="card-title">Customers</h6>
        <span class="ms-auto text-secondary" style="font-size:.8rem;"><?= count($rows) ?> customers</span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($rows)): ?>
        <div class="text-center py-5 text-secondary">
            <i class="bi bi-people fs-1 d-block mb-3 opacity-25"></i>
            <p>No customer data for this period.</p>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table mb-0" style="font-size:.85rem;">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Customer</th>
                        <th>Phone</th>
                        <th class="text-center">Orders</th>
                        <th class="text-center">Cancelled</th>
                        <th class="text-end">Total Spent</th>
                        <th>Last Order</th>
                        <th class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($rows as $i => $r): ?>
                    <tr>
                        <td class="text-secondary" style="font-size:.78rem;"><?= $i + 1 ?></td>
                        <td>
                            <div class="fw-600"><?= e($r['name']) ?></div>
                            <?php if ($r['email']): ?>
                            <div class="text-secondary" style="font-size:.75rem;"><?= e($r['email']) ?></div>
                            <?php endif; ?>
                        </td>
                        <td class="text-secondary"><?= e($r['phone'] ?? '—') ?></td>
                        <td class="text-center fw-700"><?= number_format((int)$r['total_orders']) ?></td>
                        <td class="text-center">
                            <?php if ($r['cancelled_orders'] > 0): ?>
                            <span class="badge bg-danger"><?= $r['cancelled_orders'] ?></span>
                            <?php else: ?>
                            <span class="text-secondary">0</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end fw-700 text-accent"><?= money((float)$r['total_spent']) ?></td>
                        <td class="text-secondary" style="font-size:.78rem;">
                            <?= $r['last_order_at'] ? date('d M Y', strtotime($r['last_order_at'])) : '—' ?>
                        </td>
                        <td class="text-center">
                            <a href="<?= url('reports/customers/'.$r['user_id']) ?>"
                               class="btn btn-sm btn-outline-primary" title="View Orders">
                                <i class="bi bi-eye"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr class="table-light fw-700">
                        <td colspan="3" class="text-end">Total</td>
                        <td class="text-center"><?= number_format(array_sum(array_column($rows,'total_orders'))) ?></td>
                        <td class="text-center"><?= number_format(array_sum(array_column($rows,'cancelled_orders'))) ?></td>
                        <td class="text-end text-accent"><?= money((float)array_sum(array_column($rows,'total_spent'))) ?></td>
                        <td colspan="2"></td>
                    </tr>
                </tfoot>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>
