<?php
$stColors = [
    'pending'   => ['#fff4ee','#f97316'],
    'confirmed' => ['#eff6ff','#3b82f6'],
    'cooking'   => ['#fef2f2','#ef4444'],
    'ready'     => ['#f0fdf4','#16a34a'],
    'picked_up' => ['#f5f3ff','#7c3aed'],
    'on_the_way'=> ['#fffbeb','#d97706'],
    'delivered' => ['#f0fdf4','#16a34a'],
    'cancelled' => ['#f9fafb','#9ca3af'],
];
$stLabels = [
    'pending'=>'Pending','confirmed'=>'Confirmed','cooking'=>'Cooking',
    'ready'=>'Ready','picked_up'=>'Picked Up','on_the_way'=>'On the Way',
    'delivered'=>'Delivered','cancelled'=>'Cancelled',
];
?>

<?php
$_cid2  = \Core\Session::get('customer_id');
$_del2  = (int)\Core\Database::getInstance()->fetchValue(
    "SELECT COUNT(*) FROM orders WHERE customer_id=? AND status='delivered'", [$_cid2]);
$_t2    = customer_tier($_del2);
$_tc2   = ['Platinum'=>'#006064','Gold'=>'#f57f17','Silver'=>'#424242','Bronze'=>'#bf360c','New'=>'#6b7280'];
$_tClr2 = $_tc2[$_t2['label']] ?? '#6b7280';
?>
<div style="display:flex;align-items:center;justify-content:space-between;padding:12px 14px 0;">
    <span style="font-size:.95rem;font-weight:700;">My Orders</span>
    <a href="<?= url('shop/account') ?>"
       style="display:inline-flex;align-items:center;gap:5px;font-size:.75rem;font-weight:700;
              padding:4px 12px;border-radius:20px;text-decoration:none;color:<?= $_tClr2 ?>;
              background:<?= $_t2['label'] === 'Platinum' ? '#e0f7fa' : ($_t2['label'] === 'Gold' ? '#fff8e1' : ($_t2['label'] === 'Silver' ? '#f5f5f5' : ($_t2['label'] === 'Bronze' ? '#fbe9e7' : '#f3f4f6'))) ?>;">
        <?= $_t2['icon'] ?> <?= $_t2['label'] ?>
    </a>
</div>
<div style="padding:0 14px;">
    <a href="<?= url('shop/menu') ?>" style="font-size:.75rem;color:var(--accent);font-weight:600;text-decoration:none;">
        <i class="bi bi-plus me-1"></i>New Order
    </a>
</div>

<?php if (empty($orders)): ?>
<div class="empty-state" style="margin:0 14px;">
    <div class="empty-state-icon">📦</div>
    <h4>No orders yet</h4>
    <p>You haven't placed any orders. Start ordering now!</p>
    <a href="<?= url('shop') ?>" class="btn-main" style="width:auto;display:inline-flex;">Browse Menu</a>
</div>
<?php else: ?>
<div class="order-list">
    <?php foreach ($orders as $o):
        [$bg, $clr] = $stColors[$o['status']] ?? ['#f9fafb','#6b7280'];
    ?>
    <a href="<?= url('shop/orders/'.$o['id']) ?>" class="order-row">
        <div class="order-icon">
            <i class="bi bi-receipt-cutoff" style="font-size:1.1rem;"></i>
        </div>
        <div style="flex:1;min-width:0;">
            <div class="order-no"><?= e($o['order_number']) ?></div>
            <div class="order-date"><?= date('d M Y, h:i A', strtotime($o['placed_at'])) ?></div>
        </div>
        <div style="text-align:right;flex-shrink:0;">
            <div class="order-amount"><?= money((float)$o['total_amount']) ?></div>
            <span class="order-status-pill" style="background:<?= $bg ?>;color:<?= $clr ?>;">
                <?= $stLabels[$o['status']] ?? ucfirst($o['status']) ?>
            </span>
        </div>
        <i class="bi bi-chevron-right" style="color:#bbb;font-size:.85rem;flex-shrink:0;"></i>
    </a>
    <?php endforeach; ?>
</div>
<?php endif; ?>
