<section class="admin-panel">
    <div class="admin-panel-heading admin-toolbar">
        <form class="admin-filters" action="<?= e(url('/admin/orders')) ?>" method="get">
            <input type="search" name="q" value="<?= e($query) ?>" placeholder="Order, customer, or email">
            <select name="status">
                <option value="">All statuses</option>
                <?php foreach (['pending', 'accepted', 'declined', 'processing', 'ready', 'completed'] as $status): ?>
                    <option value="<?= $status ?>" <?= selected($statusFilter, $status) ?>><?= e(order_status_label($status)) ?></option>
                <?php endforeach; ?>
            </select>
            <button class="button button-small" type="submit">Filter</button>
        </form>
        <a class="button button-small button-ghost" href="<?= e(url('/admin/export/orders.csv')) ?>">Export CSV</a>
    </div>
    <div class="table-wrap">
        <table>
            <thead>
                <tr><th>Order</th><th>Customer</th><th>Placed</th><th>Fulfillment</th><th>Payment</th><th>Total</th><th></th></tr>
            </thead>
            <tbody>
            <?php if (!$orders): ?><tr><td colspan="7" class="empty-cell">No orders match those filters.</td></tr><?php endif; ?>
            <?php foreach ($orders as $order): ?>
                <tr>
                    <td><strong><?= e($order['order_number']) ?></strong></td>
                    <td><?= e($order['customer_name']) ?><small><?= e($order['customer_email']) ?></small></td>
                    <td><?= e(store_datetime((string) $order['placed_at'], 'M j, g:i A')) ?></td>
                    <td><span class="status-pill status-<?= e($order['status']) ?>"><?= e(order_status_label((string) $order['status'])) ?></span></td>
                    <td>
                        <span class="status-pill status-<?= e($order['payment_status']) ?>"><?= e(ucwords(str_replace('_', ' ', (string) $order['payment_status']))) ?></span>
                        <?php if ((int) $order['refunded_cents'] > 0): ?><small><?= money($order['refunded_cents']) ?> returned</small><?php endif; ?>
                    </td>
                    <td><?= money($order['total_cents']) ?></td>
                    <td><a class="button button-small button-ghost" href="<?= e(url('/admin/order')) ?>?id=<?= (int) $order['id'] ?>">Open</a></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
