<div class="header">
    <h1>User #<?= (int) $user->id ?></h1>
    <p><a href="<?= base_url('admin/users') ?>" class="btn btn-secondary">← Back to users</a></p>
</div>

<div style="background: var(--surface); padding: 1.5rem; border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 2rem;">
    <h2 style="margin-bottom: 1rem;">Details</h2>
    <p><strong>ID:</strong> <?= (int) $user->id ?></p>
    <p><strong>Phone:</strong> <?= esc($user->phone ?? '—') ?></p>
    <p><strong>Username:</strong> <?= esc($user->username ?? '—') ?></p>
    <p><strong>Display name:</strong> <?= esc($user->display_name ?? '—') ?></p>
    <p><strong>Role:</strong> <?= esc($user->role ?? 'user') ?></p>
    <p><strong>Status:</strong>
        <span style="display: inline-block; padding: 0.25rem 0.75rem; background: <?= ($user->jambo_status ?? 'active') === 'active' ? '#d1fae5' : '#fee2e2' ?>; color: <?= ($user->jambo_status ?? 'active') === 'active' ? '#065f46' : '#991b1b' ?>; border-radius: 9999px; font-size: 0.75rem; font-weight: 600;">
            <?= esc($user->jambo_status ?? 'active') ?>
        </span>
    </p>
    <?php if (($user->role ?? '') !== 'admin'): ?>
    <form method="post" action="<?= base_url('admin/users/' . $user->id . '/toggle') ?>" style="display:inline; margin-top: 1rem;">
        <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
        <button type="submit" class="btn btn-secondary"><?= ($user->jambo_status ?? 'active') === 'active' ? 'Deactivate' : 'Activate' ?></button>
    </form>
    <?php endif ?>
</div>

<h2 style="margin: 2rem 0 1rem;">Order history</h2>
<form method="get" action="<?= base_url('admin/users/' . $user->id) ?>" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 1.5rem;">
    <div>
        <label for="q">Quick search (ID, amount, date, type, status)</label>
        <input type="text" name="q" id="q" value="<?= esc($filter_q) ?>" placeholder="Search orders…" style="min-width: 200px;">
    </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</option>
            <option value="cancelled" <?= ($filter_status ?? '') === 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
        </select>
    </div>
    <button type="submit" class="btn btn-primary">Search</button>
    <?php if ($filter_q !== '' || ($filter_status ?? '') !== ''): ?>
    <a href="<?= base_url('admin/users/' . $user->id) ?>" class="btn btn-secondary">Clear</a>
    <?php endif ?>
</form>
<table>
    <thead>
        <tr><th>ID</th><th>Type</th><th>Status</th><th>Amount</th><th>Date</th><th></th></tr>
    </thead>
    <tbody>
        <?php foreach ($orders as $o): ?>
        <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><?= 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 order</a></td>
        </tr>
        <?php endforeach ?>
        <?php if (empty($orders)): ?>
        <tr><td colspan="6">No orders found.</td></tr>
        <?php endif ?>
    </tbody>
</table>
