<?php
$tabs = [
    'ALL' => 'All', 'PENDING' => 'Pending', 'CONFIRMED' => 'Confirmed', 'PREPARING' => 'Preparing',
    'READY' => 'Ready', 'OUT_FOR_DELIVERY' => 'Out for Delivery', 'COMPLETED' => 'Completed', 'CANCELLED' => 'Cancelled',
];
$statusBadge = [
    'PENDING' => 'badge-gray', 'CONFIRMED' => 'badge-blue', 'PREPARING' => 'badge-gold',
    'READY' => 'badge-gold', 'OUT_FOR_DELIVERY' => 'badge-blue', 'COMPLETED' => 'badge-green', 'CANCELLED' => 'badge-red',
];
$deliveryBadge = [
    'UNASSIGNED' => 'badge-gray', 'ASSIGNED' => 'badge-blue', 'PICKED_UP' => 'badge-gold',
    'DELIVERED' => 'badge-green', 'FAILED' => 'badge-red',
];
?>
<div class="grid-cards">
    <div class="stat-card c-blue">
        <div class="stat-icon"><i class="fa-solid fa-receipt"></i></div>
        <div class="stat-label">Today's Orders</div>
        <div class="stat-value"><?= (int) $todayStats['today_count'] ?></div>
        <div class="stat-sub">Excludes cancelled</div>
    </div>
    <div class="stat-card c-green">
        <div class="stat-icon"><i class="fa-solid fa-arrow-trend-up"></i></div>
        <div class="stat-label">Today's Revenue</div>
        <div class="stat-value"><?= formatMoney((float) $todayStats['today_revenue']) ?></div>
        <div class="stat-sub">Order total value</div>
    </div>
    <div class="stat-card c-purple">
        <div class="stat-icon"><i class="fa-solid fa-hourglass-half"></i></div>
        <div class="stat-label">Pending / Preparing</div>
        <div class="stat-value"><?= (int) ($statusCounts['PENDING'] ?? 0) + (int) ($statusCounts['CONFIRMED'] ?? 0) + (int) ($statusCounts['PREPARING'] ?? 0) ?></div>
        <div class="stat-sub">Needs kitchen attention</div>
    </div>
    <div class="stat-card c-red">
        <div class="stat-icon"><i class="fa-solid fa-credit-card"></i></div>
        <div class="stat-label">Unpaid Today</div>
        <div class="stat-value"><?= (int) $todayStats['unpaid_count'] ?></div>
        <div class="stat-sub">Balance still due</div>
    </div>
</div>

<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;flex-wrap:wrap;gap:10px;">
    <form method="GET" action="<?= Router::url('orders') ?>" style="display:flex;gap:8px;">
        <input type="hidden" name="status" value="<?= e($activeStatus) ?>">
        <input type="text" name="search" class="form-control" style="width:220px;" placeholder="Search order#, phone, name..." value="<?= e($search) ?>">
        <button type="submit" class="btn btn-secondary"><i class="fa-solid fa-magnifying-glass"></i></button>
    </form>
    <?php if (Auth::can('orders', 'add')): ?>
    <a href="<?= Router::url('orders/create') ?>" class="btn btn-primary"><i class="fa-solid fa-plus"></i> New Order</a>
    <?php endif; ?>
</div>

<div style="display:flex;gap:6px;flex-wrap:wrap;margin-bottom:16px;">
    <?php foreach ($tabs as $key => $label): ?>
    <a href="<?= Router::url('orders?status=' . $key . ($search ? '&search=' . urlencode($search) : '')) ?>"
       class="btn <?= $activeStatus === $key ? 'btn-primary' : 'btn-secondary' ?>" style="padding:6px 14px;font-size:12.5px;">
        <?= e($label) ?> <span class="tab-count"><?= (int) ($statusCounts[$key] ?? 0) ?></span>
    </a>
    <?php endforeach; ?>
</div>

<div class="panel" style="padding:0;overflow-x:auto;">
<table class="data-table">
<thead>
<tr><th>Order #</th><th>Received Via</th><th>Customer</th><th>Total</th><th>Payment</th><th>Status</th><th>Delivery</th><th>Placed</th><th></th></tr>
</thead>
<tbody>
<?php
$sourceMeta = [
    'APP'      => ['icon' => 'fa-mobile-screen-button', 'label' => 'App',      'badge' => 'badge-blue'],
    'CALL'     => ['icon' => 'fa-phone',                 'label' => 'Call',     'badge' => 'badge-gold'],
    'WHATSAPP' => ['icon' => 'fa-brands fa-whatsapp',     'label' => 'WhatsApp', 'badge' => 'badge-green'],
    'WALK_IN'  => ['icon' => 'fa-store',                 'label' => 'Walk-in',  'badge' => 'badge-gray'],
];
foreach ($orders as $o):
    $balance = (float) $o['total_amount'] - (float) $o['total_paid'];
    $sm = $sourceMeta[$o['order_source']] ?? null;
?>
<tr>
    <td><strong><?= e($o['order_number']) ?></strong></td>
    <td><?php if ($sm): ?><span class="badge <?= $sm['badge'] ?>"><i class="fa-solid <?= $sm['icon'] ?>"></i> <?= e($sm['label']) ?></span><?php else: ?><span style="color:var(--color-text-muted);">—</span><?php endif; ?></td>
    <td><?= e($o['customer_name'] ?? 'Walk-in') ?><?= $o['customer_phone'] ? ' · ' . e($o['customer_phone']) : '' ?></td>
    <td><?= formatMoney((float) $o['total_amount']) ?></td>
    <td>
        <?php if ($o['status'] === 'CANCELLED'): ?>
        <span class="badge badge-gray">—</span>
        <?php elseif ($balance <= 0): ?>
        <span class="badge badge-green"><i class="fa-solid fa-circle-check"></i> Paid</span>
        <?php elseif ((float) $o['total_paid'] > 0): ?>
        <span class="badge badge-gold"><i class="fa-solid fa-triangle-exclamation"></i> Partial</span>
        <?php else: ?>
        <span class="badge badge-red"><i class="fa-solid fa-xmark"></i> Unpaid</span>
        <?php endif; ?>
    </td>
    <td><span class="badge <?= $statusBadge[$o['status']] ?? 'badge-gray' ?>"><?= e(str_replace('_', ' ', $o['status'])) ?></span></td>
    <td>
        <?php if ($o['delivery_status']): ?>
        <span class="badge <?= $deliveryBadge[$o['delivery_status']] ?? 'badge-gray' ?>"><?= e(str_replace('_', ' ', $o['delivery_status'])) ?></span>
        <?php if ($o['rider_name']): ?><div style="font-size:11px;color:var(--color-text-muted);margin-top:2px;"><i class="fa-solid fa-motorcycle"></i> <?= e($o['rider_name']) ?></div><?php endif; ?>
        <?php else: ?>
        <span style="color:var(--color-text-muted);">—</span>
        <?php endif; ?>
    </td>
    <td><?= date('d M, h:i A', strtotime($o['created_at'])) ?></td>
    <td><a href="<?= Router::url('orders/show?id=' . $o['id']) ?>" class="btn btn-secondary" style="padding:5px 12px;font-size:12px;"><i class="fa-solid fa-eye"></i> View</a></td>
</tr>
<?php endforeach; ?>
<?php if (empty($orders)): ?>
<tr><td colspan="9" style="text-align:center;color:var(--color-text-muted);padding:30px;">No orders found.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>

<style>
.tab-count {
    display: inline-block; margin-left: 4px; font-size: 10.5px; font-weight: 700;
    background: rgba(255,255,255,0.25); padding: 1px 6px; border-radius: 100px;
}
.btn-secondary .tab-count { background: rgba(20,25,40,0.08); color: var(--color-text-muted); }
</style>