<?php
$statusColors = [
    'pending'    => 'warning',
    'confirmed'  => 'info',
    'cooking'    => 'danger',
    'ready'      => 'success',
    'picked_up'  => 'primary',
    'on_the_way' => 'accent',
    '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>Orders</h1>
        <p>All customer orders with full tracking.</p>
    </div>
    <a href="<?= url('orders/live') ?>" class="btn btn-accent">
        <i class="bi bi-kanban me-1"></i> Live Board
    </a>
</div>

<!-- Filters -->
<div class="card mb-4">
    <div class="card-body py-3">
        <form method="GET" action="<?= url('orders') ?>" class="row g-2 align-items-end">
            <div class="col-12 col-md-3">
                <input type="text" name="search" class="form-control form-control-sm"
                       placeholder="Order # / customer / phone"
                       value="<?= e($filters['search']) ?>">
            </div>
            <div class="col-6 col-md-2">
                <select name="status" class="form-select form-select-sm">
                    <option value="">All Statuses</option>
                    <?php foreach ($statuses as $s): ?>
                    <option value="<?= $s ?>" <?= $filters['status'] === $s ? 'selected' : '' ?>>
                        <?= $statusLabels[$s] ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-6 col-md-2">
                <input type="date" name="date_from" class="form-control form-control-sm"
                       value="<?= e($filters['date_from']) ?>" placeholder="From">
            </div>
            <div class="col-6 col-md-2">
                <input type="date" name="date_to" class="form-control form-control-sm"
                       value="<?= e($filters['date_to']) ?>" placeholder="To">
            </div>
            <div class="col-6 col-md-3 d-flex gap-2">
                <button type="submit" class="btn btn-accent btn-sm px-3">
                    <i class="bi bi-search me-1"></i> Filter
                </button>
                <a href="<?= url('orders') ?>" class="btn btn-outline-secondary btn-sm">Reset</a>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="bi bi-receipt text-accent"></i>
        <h6 class="card-title">All Orders</h6>
        <span class="ms-auto text-secondary" style="font-size:.8rem;"><?= $total ?> total</span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($orders)): ?>
        <div class="text-center py-5 text-secondary">
            <i class="bi bi-receipt fs-1 d-block mb-3 opacity-25"></i>
            <h6>No orders found</h6>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th>Order #</th>
                        <th>Customer</th>
                        <th>Phone</th>
                        <th class="text-end">Total</th>
                        <th class="text-center">Status</th>
                        <th>Placed At</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>
                            <?php
                            $isG = empty($o['customer_id']);
                            $cn  = $isG ? ($o['guest_name'] ?? 'Guest') : ($o['customer_name'] ?? '—');
                            $del = (int)($o['cust_delivered_orders'] ?? 0);
                            $tot = (int)($o['cust_total_orders']     ?? 0);
                            $ct  = $isG ? null : customer_tier($del);
                            $tcMap = ['Platinum'=>['#e0f7fa','#006064'],'Gold'=>['#fff8e1','#f57f17'],
                                      'Silver'=>['#f5f5f5','#424242'],'Bronze'=>['#fbe9e7','#bf360c'],'New'=>['#f3f4f6','#6b7280']];
                            $tc2 = $ct ? ($tcMap[$ct['label']] ?? $tcMap['New']) : null;
                            ?>
                            <div class="fw-600"><?= e($cn) ?>
                                <?php if ($isG): ?>
                                <span class="badge bg-secondary ms-1" style="font-size:.6rem;">Guest</span>
                                <?php elseif ($ct): ?>
                                <span style="display:inline-flex;align-items:center;gap:3px;padding:1px 7px;border-radius:20px;
                                             font-size:.65rem;font-weight:700;background:<?= $tc2[0] ?>;color:<?= $tc2[1] ?>;">
                                    <?= $ct['icon'] ?> <?= $ct['label'] ?>
                                </span>
                                <?php endif; ?>
                            </div>
                            <?php if (!$isG && $tot > 0): ?>
                            <div style="font-size:.7rem;color:#6b7280;margin-top:1px;">
                                <?= $tot ?> order<?= $tot !== 1 ? 's' : '' ?> · <?= $del ?> delivered
                            </div>
                            <?php endif; ?>
                        </td>
                        <td class="text-secondary" style="font-size:.85rem;"><?= e($isG ? ($o['guest_phone'] ?? '—') : ($o['customer_phone'] ?? '—')) ?></td>
                        <td class="text-end fw-700"><?= money((float)$o['total_amount']) ?></td>
                        <td class="text-center">
                            <?php $sc = $statusColors[$o['status']] ?? 'secondary'; ?>
                            <span class="badge bg-<?= $sc === 'accent' ? 'warning' : $sc ?>
                                <?= $sc === 'accent' ? 'text-dark' : '' ?>"
                                  style="<?= $sc === 'accent' ? 'background:var(--accent)!important;color:#fff!important;' : '' ?>">
                                <?= $statusLabels[$o['status']] ?? ucfirst($o['status']) ?>
                            </span>
                        </td>
                        <td style="font-size:.82rem;" class="text-secondary">
                            <?= format_date($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" title="View">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="<?= url('orders/'.$o['id'].'/invoice') ?>"
                                   class="btn btn-sm btn-outline-secondary" title="Invoice" target="_blank">
                                    <i class="bi bi-printer"></i>
                                </a>
                                <a href="<?= url('orders/'.$o['id'].'/kot') ?>"
                                   class="btn btn-sm btn-outline-secondary" title="KOT" target="_blank">
                                    <i class="bi bi-journal-text"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php if ($total > ITEMS_PER_PAGE): ?>
        <div class="d-flex justify-content-between align-items-center px-4 py-3 border-top">
            <small class="text-secondary">Showing <?= count($orders) ?> of <?= $total ?></small>
            <?= paginate($total, $page, ITEMS_PER_PAGE, url('orders')) ?>
        </div>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
