<div class="header">
    <h1>Orders</h1>
</div>
<form method="get" action="<?= base_url('admin/orders') ?>" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 1.5rem;">
    <div>
        <label for="q">Search (user or driver name/phone)</label>
        <input type="text" name="q" id="q" value="<?= esc($filter_q ?? '') ?>" placeholder="Search…" style="min-width: 180px;">
    </div>
    <div>
        <label for="date_from">From date</label>
        <input type="date" name="date_from" id="date_from" value="<?= esc($filter_date_from ?? '') ?>">
    </div>
    <div>
        <label for="date_to">To date</label>
        <input type="date" name="date_to" id="date_to" value="<?= esc($filter_date_to ?? '') ?>">
    </div>
    <div>
        <label for="status">Status</label>
        <select name="status" id="status">
            <option value="">All</option>
            <option value="pending" <?= ($filter_status ?? '') === 'pending' ? 'selected' : '' ?>>Pending</option>
            <option value="paid" <?= ($filter_status ?? '') === 'paid' ? 'selected' : '' ?>>Paid</option>
            <option value="dispatched" <?= ($filter_status ?? '') === 'dispatched' ? 'selected' : '' ?>>Dispatched</option>
            <option value="delivered" <?= ($filter_status ?? '') === 'delivered' ? 'selected' : '' ?>>Delivered (Success)</option>
            <option value="cancelled" <?= ($filter_status ?? '') === 'cancelled' ? 'selected' : '' ?>>Cancelled (Failed)</option>
            <option value="pending_group" <?= ($filter_status ?? '') === 'pending_group' ? 'selected' : '' ?>>Pending (all)</option>
        </select>
    </div>
    <button type="submit" class="btn btn-primary">Filter</button>
    <?php if (($filter_q ?? '') !== '' || ($filter_date_from ?? '') !== '' || ($filter_date_to ?? '') !== '' || ($filter_status ?? '') !== ''): ?>
    <a href="<?= base_url('admin/orders') ?>" class="btn btn-secondary">Clear</a>
    <?php endif ?>
</form>
<table>
    <thead>
        <tr><th>ID</th><th>Type</th><th>Status</th><th>User</th><th>Driver</th><th>Amount</th><th>Date</th><th></th></tr>
    </thead>
    <tbody>
        <?php foreach ($orders as $o): ?>
        <?php
        $userLabel = trim(($o->user_name ?? '') . ' ' . ($o->user_username ?? ''));
        if ($userLabel === '') { $userLabel = $o->user_phone ?? '#' . $o->user_id; }
        $driverLabel = $o->driver_id ? (trim(($o->driver_name ?? '') . ' ' . ($o->driver_username ?? '')) ?: ($o->driver_phone ?? '#' . $o->driver_id)) : '—';
        ?>
        <tr>
            <td>#<?= $o->id ?></td>
            <td><?= esc($o->order_type ?? '—') ?></td>
            <td><span style="display: inline-block; padding: 0.25rem 0.75rem; background: #e5e7eb; border-radius: 9999px; font-size: 0.75rem; font-weight: 600;"><?= esc($o->status) ?></span></td>
            <td><?= esc($userLabel) ?></td>
            <td><?= esc($driverLabel) ?></td>
            <td><?= number_format($o->total_amount) ?> UGX</td>
            <td><?= esc($o->created_at) ?></td>
            <td><a href="<?= base_url('admin/orders/' . $o->id) ?>" class="btn btn-secondary">View</a></td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>