<div class="header">
    <h1>Drivers</h1>
</div>

<div style="background: var(--surface); padding: 1.5rem; border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 2rem;">
    <h3 style="margin-bottom: 1rem;">Add new driver</h3>
    <form method="post" action="<?= base_url('admin/drivers/add') ?>" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap;">
        <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
        <div style="flex: 1 1 180px;">
            <label>Name (shown to customers)</label>
            <input type="text" name="name" placeholder="e.g. John or John M." required style="width:100%;">
        </div>
        <div style="flex: 1 1 180px;">
            <label>Phone (9 digits)</label>
            <input type="text" name="phone" placeholder="712345678" style="width:100%;">
        </div>
        <div style="flex: 0 1 120px;">
            <label>PIN (6 digits)</label>
            <input type="password" name="pin" placeholder="******" style="width:100%;">
        </div>
        <button type="submit" class="btn btn-primary">Add driver</button>
    </form>
</div>

<h2 style="margin: 2rem 0 1rem;">Driver list</h2>
<form method="get" action="<?= base_url('admin/drivers') ?>" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 1.5rem;">
    <div>
        <label for="q">Search (name, phone)</label>
        <input type="text" name="q" id="q" value="<?= esc($filter_q ?? '') ?>" placeholder="Search…" style="min-width: 180px;">
    </div>
    <div>
        <label for="status">Status</label>
        <select name="status" id="status">
            <option value="">All</option>
            <option value="active" <?= ($filter_status ?? '') === 'active' ? 'selected' : '' ?>>Active</option>
            <option value="inactive" <?= ($filter_status ?? '') === 'inactive' ? 'selected' : '' ?>>Inactive</option>
        </select>
    </div>
    <button type="submit" class="btn btn-primary">Filter</button>
    <?php if (($filter_q ?? '') !== '' || ($filter_status ?? '') !== ''): ?>
    <a href="<?= base_url('admin/drivers') ?>" class="btn btn-secondary">Clear</a>
    <?php endif ?>
</form>
<table>
    <thead>
        <tr><th>ID</th><th>Name</th><th>Phone</th><th>Status</th><th>Action</th></tr>
    </thead>
    <tbody>
        <?php foreach ($drivers as $d): ?>
        <tr>
            <td><?= $d->id ?></td>
            <td><?= esc($d->display_name ?? $d->username ?? '—') ?> <a href="<?= base_url('admin/drivers/edit/' . $d->id) ?>" style="font-size:0.85rem;">Edit name</a></td>
            <td><?= esc($d->phone) ?></td>
            <td>
                <span style="display: inline-block; padding: 0.25rem 0.75rem; background: <?= ($d->jambo_status ?? 'active') === 'active' ? '#d1fae5' : '#fee2e2' ?>; color: <?= ($d->jambo_status ?? 'active') === 'active' ? '#065f46' : '#991b1b' ?>; border-radius: 9999px; font-size: 0.75rem; font-weight: 600;">
                    <?= esc($d->jambo_status ?? 'active') ?>
                </span>
            </td>
            <td>
                <form method="post" action="<?= base_url('admin/drivers/' . $d->id . '/toggle') ?>" style="display:inline;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <button type="submit" class="btn btn-secondary"><?= ($d->jambo_status ?? 'active') === 'active' ? 'Deactivate' : 'Activate' ?></button>
                </form>
            </td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>