<?php $title = 'Riders'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-0 fw-bold">Riders</h4>
        <small class="text-muted"><?= $total ?> rider<?= $total != 1 ? 's' : '' ?> registered</small>
    </div>
    <a href="<?= url('riders/create') ?>" class="btn btn-primary">
        <i class="bi bi-plus-lg me-1"></i> Add Rider
    </a>
</div>

<?= flash_alert() ?>

<!-- Search -->
<div class="card border-0 shadow-sm mb-4">
    <div class="card-body py-2">
        <form method="GET" class="d-flex gap-2">
            <input type="text" name="search" value="<?= e($search) ?>"
                   placeholder="Search by name, phone or email…"
                   class="form-control form-control-sm" style="max-width:320px;">
            <button class="btn btn-sm btn-outline-secondary">
                <i class="bi bi-search"></i> Search
            </button>
            <?php if ($search): ?>
                <a href="<?= url('riders') ?>" class="btn btn-sm btn-outline-danger">Clear</a>
            <?php endif ?>
        </form>
    </div>
</div>

<!-- Table -->
<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-hover align-middle mb-0">
            <thead class="table-light">
                <tr>
                    <th>Rider</th>
                    <th>Phone</th>
                    <th>Vehicle</th>
                    <th class="text-center">Total Deliveries</th>
                    <th class="text-center">Completed</th>
                    <th class="text-center">Active Now</th>
                    <th class="text-center">Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
            <?php if (empty($riders)): ?>
                <tr><td colspan="8" class="text-center text-muted py-4">No riders found.</td></tr>
            <?php else: foreach ($riders as $r): ?>
                <tr>
                    <td>
                        <div class="d-flex align-items-center gap-2">
                            <div class="avatar-circle bg-warning text-dark">
                                <?= strtoupper(substr($r['name'], 0, 1)) ?>
                            </div>
                            <div>
                                <div class="fw-semibold"><?= e($r['name']) ?></div>
                                <small class="text-muted"><?= e($r['email']) ?></small>
                            </div>
                        </div>
                    </td>
                    <td><?= e($r['phone']) ?></td>
                    <td>
                        <?php if ($r['vehicle_type'] || $r['vehicle_number']): ?>
                            <span class="badge bg-light text-dark border">
                                <?= e($r['vehicle_type'] ?? '') ?>
                                <?= $r['vehicle_number'] ? ' · ' . e($r['vehicle_number']) : '' ?>
                            </span>
                        <?php else: ?>
                            <span class="text-muted">—</span>
                        <?php endif ?>
                    </td>
                    <td class="text-center fw-semibold"><?= $r['total_deliveries'] ?></td>
                    <td class="text-center">
                        <span class="text-success fw-semibold"><?= $r['completed'] ?></span>
                    </td>
                    <td class="text-center">
                        <?php if ($r['active_orders'] > 0): ?>
                            <span class="badge bg-warning text-dark"><?= $r['active_orders'] ?> active</span>
                        <?php else: ?>
                            <span class="text-muted">—</span>
                        <?php endif ?>
                    </td>
                    <td class="text-center">
                        <form method="POST" action="<?= url('riders/'.$r['id'].'/toggle') ?>">
                            <?= csrf_field() ?>
                            <button class="btn btn-sm <?= $r['is_active'] ? 'btn-success' : 'btn-secondary' ?>">
                                <?= $r['is_active'] ? 'Active' : 'Inactive' ?>
                            </button>
                        </form>
                    </td>
                    <td class="text-end">
                        <a href="<?= url('riders/'.$r['id']) ?>" class="btn btn-sm btn-outline-primary me-1" title="View">
                            <i class="bi bi-eye"></i>
                        </a>
                        <a href="<?= url('riders/'.$r['id'].'/edit') ?>" class="btn btn-sm btn-outline-secondary me-1" title="Edit">
                            <i class="bi bi-pencil"></i>
                        </a>
                        <form method="POST" action="<?= url('riders/'.$r['id'].'/delete') ?>" class="d-inline"
                              onsubmit="return confirm('Delete this rider?')">
                            <?= csrf_field() ?>
                            <button class="btn btn-sm btn-outline-danger" title="Delete">
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; endif ?>
            </tbody>
        </table>
    </div>
    <?php if ($pages > 1): ?>
    <div class="card-footer bg-white">
        <?= paginate($total, $page, ITEMS_PER_PAGE, url('riders') . '?search=' . urlencode($search)) ?>
    </div>
    <?php endif ?>
</div>
