<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0"><i class="fas fa-clipboard-list me-2"></i>Attendance: <?= e($class['name_en']) ?></h4>
    <a href="/sports/classes/<?= $class['id'] ?>" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left me-1"></i>Back to Class</a>
</div>

<div class="card">
    <div class="card-header">
        <form method="GET" action="/sports/classes/<?= $class['id'] ?>/attendance" class="row g-2 align-items-end">
            <div class="col-md-3">
                <label class="form-label small">Session Date</label>
                <input type="date" name="date" class="form-control form-control-sm datepicker" value="<?= e($sessionDate) ?>">
            </div>
            <div class="col-md-2"><button type="submit" class="btn btn-primary btn-sm">Load</button></div>
        </form>
    </div>
    <div class="card-body">
        <form method="POST" action="/sports/classes/<?= $class['id'] ?>/attendance" data-ajax="true">
            <?= csrf_field() ?>
            <input type="hidden" name="session_date" value="<?= e($sessionDate) ?>">
            <input type="hidden" name="session_start" value="09:00">
            <table class="table table-sm table-hover">
                <thead><tr><th>#</th><th>Member</th><th>Status</th></tr></thead>
                <tbody>
                <?php foreach ($enrollments as $i => $en):
                    $existing = $attendanceMap[$en['id']] ?? null;
                    $currentStatus = $existing['status'] ?? 'present';
                ?>
                <tr>
                    <td><?= $i+1 ?></td>
                    <td><?= e($en['member_name'] ?? '') ?> <small class="text-muted"><?= e($en['membership_number'] ?? '') ?></small></td>
                    <td>
                        <div class="btn-group btn-group-sm" role="group">
                            <?php foreach (['present' => 'success', 'absent' => 'danger', 'late' => 'warning', 'excused' => 'info'] as $st => $color): ?>
                            <input type="radio" class="btn-check" name="attendance[<?= $en['id'] ?>]" id="att_<?= $en['id'] ?>_<?= $st ?>" value="<?= $st ?>" <?= $currentStatus === $st ? 'checked' : '' ?>>
                            <label class="btn btn-outline-<?= $color ?>" for="att_<?= $en['id'] ?>_<?= $st ?>"><?= ucfirst($st) ?></label>
                            <?php endforeach; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($enrollments)): ?><tr><td colspan="3" class="text-muted text-center">No enrolled members</td></tr><?php endif; ?>
                </tbody>
            </table>
            <?php if (!empty($enrollments)): ?>
            <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>Save Attendance</button>
            <?php endif; ?>
        </form>
    </div>
</div>