<?php
$statusColors = [
    'pending'=>'warning','confirmed'=>'info','cooking'=>'danger','ready'=>'success',
    'picked_up'=>'primary','on_the_way'=>'warning','delivered'=>'success','cancelled'=>'secondary',
];
$statusLabels = [
    'pending'=>'Pending','confirmed'=>'Confirmed','cooking'=>'Cooking','ready'=>'Ready',
    'picked_up'=>'Picked Up','on_the_way'=>'On the Way','delivered'=>'Delivered','cancelled'=>'Cancelled',
];
?>

<div class="page-header">
    <div>
        <h1><i class="bi bi-person-circle me-2"></i><?= e($customer['name'] ?? 'Customer') ?></h1>
        <p><?= e($customer['email'] ?? '') ?> <?= $customer['phone'] ? '· '.$customer['phone'] : '' ?></p>
    </div>
    <a href="<?= url('reports/customers') ?>" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-1"></i> Back
    </a>
</div>

<?php if (empty($orders)): ?>
<div class="card">
    <div class="card-body text-center py-5 text-secondary">
        <i class="bi bi-receipt fs-1 d-block mb-3 opacity-25"></i>
        <p>No orders found for this customer.</p>
    </div>
</div>
<?php else: ?>

<!-- Quick stats -->
<?php
$totalSpent    = array_sum(array_map(fn($o) => $o['status'] !== 'cancelled' ? $o['total_amount'] : 0, $orders));
$totalOrders   = count($orders);
$cancelledOrds = count(array_filter($orders, fn($o) => $o['status'] === 'cancelled'));
?>
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:rgba(249,115,22,.12);color:var(--accent);"><i class="bi bi-receipt"></i></div>
            <div class="stat-body"><div class="stat-value"><?= $totalOrders ?></div><div class="stat-label">Total Orders</div></div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:rgba(34,197,94,.12);color:#22c55e;"><i class="bi bi-wallet2"></i></div>
            <div class="stat-body"><div class="stat-value"><?= money((float)$totalSpent) ?></div><div class="stat-label">Total Spent</div></div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:rgba(59,130,246,.12);color:#3b82f6;"><i class="bi bi-graph-up-arrow"></i></div>
            <div class="stat-body"><div class="stat-value"><?= $totalOrders > 0 ? money((float)($totalSpent / $totalOrders)) : money(0) ?></div><div class="stat-label">Avg Order</div></div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:rgba(239,68,68,.12);color:#ef4444;"><i class="bi bi-x-circle"></i></div>
            <div class="stat-body"><div class="stat-value"><?= $cancelledOrds ?></div><div class="stat-label">Cancelled</div></div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="bi bi-clock-history text-accent"></i>
        <h6 class="card-title">Order History</h6>
        <span class="ms-auto text-secondary" style="font-size:.8rem;"><?= $totalOrders ?> orders</span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table mb-0" style="font-size:.85rem;">
                <thead>
                    <tr>
                        <th>Order #</th>
                        <th class="text-center">Status</th>
                        <th>Payment</th>
                        <th class="text-end">Total</th>
                        <th>Date</th>
                        <th class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orders as $o): ?>
                    <tr>
                        <td>
                            <a href="<?= url('orders/'.$o['id']) ?>" class="fw-700 text-accent text-decoration-none">
                                <?= e($o['order_number']) ?>
                            </a>
                        </td>
                        <td class="text-center">
                            <span class="badge bg-<?= $statusColors[$o['status']] ?? 'secondary' ?>" style="font-size:.7rem;">
                                <?= $statusLabels[$o['status']] ?? ucfirst($o['status']) ?>
                            </span>
                        </td>
                        <td><?= ucfirst($o['payment_method']) ?></td>
                        <td class="text-end fw-700 <?= $o['status'] === 'cancelled' ? 'text-secondary text-decoration-line-through' : 'text-accent' ?>">
                            <?= money((float)$o['total_amount']) ?>
                        </td>
                        <td class="text-secondary" style="white-space:nowrap;">
                            <?= date('d M Y H:i', strtotime($o['placed_at'])) ?>
                        </td>
                        <td class="text-center">
                            <div class="d-flex gap-1 justify-content-center">
                                <a href="<?= url('orders/'.$o['id']) ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="<?= url('orders/'.$o['id'].'/invoice') ?>"
                                   class="btn btn-sm btn-outline-secondary" target="_blank">
                                    <i class="bi bi-printer"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php endif; ?>
