<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0"><i class="fas fa-chalkboard me-2"></i><?= e($class['name_en']) ?></h4>
    <div>
        <?php if (auth_can('classes.attendance')): ?><a href="/sports/classes/<?= $class['id'] ?>/attendance" class="btn btn-info btn-sm"><i class="fas fa-clipboard-list me-1"></i>Attendance</a><?php endif; ?>
        <?php if (auth_can('classes.manage')): ?><a href="/sports/classes/<?= $class['id'] ?>/edit" class="btn btn-warning btn-sm"><i class="fas fa-edit me-1"></i>Edit</a><?php endif; ?>
        <a href="/sports/classes" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-4">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Class Details</h6></div>
            <div class="card-body">
                <table class="table table-sm mb-0">
                    <tr><th>Code</th><td><?= e($class['class_code']) ?></td></tr>
                    <tr><th>Sport</th><td><?= e($class['sport_name'] ?? '') ?></td></tr>
                    <tr><th>Coach</th><td><?= e($class['coach_name'] ?? 'Unassigned') ?></td></tr>
                    <tr><th>Facility</th><td><?= e($class['facility_name'] ?? 'N/A') ?></td></tr>
                    <tr><th>Level</th><td><?= ucfirst(str_replace('_',' ',$class['class_type'])) ?></td></tr>
                    <tr><th>Capacity</th><td><?= $enrolledCount ?> / <?= $class['max_participants'] ?></td></tr>
                    <tr><th>Fee/Month</th><td><?= format_money($class['fee_per_month']) ?></td></tr>
                    <tr><th>Duration</th><td><?= $class['session_duration_minutes'] ?> min</td></tr>
                    <tr><th>Gender</th><td><?= ucfirst($class['gender_restriction']) ?></td></tr>
                    <tr><th>Status</th><td><?= status_badge($class['status']) ?></td></tr>
                </table>
            </div>
        </div>

        <?php if (!empty($schedules)): ?>
        <div class="card mt-3">
            <div class="card-header"><h6 class="mb-0">Weekly Schedule</h6></div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>Day</th><th>Time</th></tr></thead>
                    <tbody>
                    <?php
                    $dayNames = ['','Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
                    foreach ($schedules as $sched): ?>
                    <tr><td><?= $dayNames[$sched['day_of_week']] ?? $sched['day_of_week'] ?></td><td><?= substr($sched['start_time'],0,5) ?> - <?= substr($sched['end_time'],0,5) ?></td></tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-md-8">
        <ul class="nav nav-tabs">
            <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#tab-enrolled">Enrolled (<?= count($enrollments) ?>)</a></li>
            <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-waitlist">Waitlist (<?= count($waitlist) ?>)</a></li>
        </ul>
        <div class="tab-content border border-top-0 p-3">
            <div class="tab-pane fade show active" id="tab-enrolled">
                <?php if (auth_can('classes.manage')): ?>
                <form method="POST" action="/sports/classes/<?= $class['id'] ?>/enroll" data-ajax="true" class="row g-2 mb-3">
                    <?= csrf_field() ?>
                    <div class="col-md-8">
                        <input type="number" name="member_id" class="form-control form-control-sm" placeholder="Member ID" required>
                    </div>
                    <div class="col-md-4">
                        <button type="submit" class="btn btn-primary btn-sm w-100"><i class="fas fa-user-plus me-1"></i>Enroll</button>
                    </div>
                </form>
                <?php endif; ?>
                <div class="table-responsive">
                <table class="table table-sm table-hover">
                    <thead><tr><th>#</th><th>Member</th><th>Date</th><th>Status</th><th>Fee</th><th></th></tr></thead>
                    <tbody>
                    <?php foreach ($enrollments as $i => $en): ?>
                    <tr>
                        <td><?= $i+1 ?></td>
                        <td><?= e($en['member_name'] ?? '') ?> <small class="text-muted"><?= e($en['membership_number'] ?? '') ?></small><?= $en['dependent_name'] ? '<br><small>Dep: ' . e($en['dependent_name']) . '</small>' : '' ?></td>
                        <td><?= format_date($en['enrollment_date']) ?></td>
                        <td><?= status_badge($en['status']) ?></td>
                        <td><?= format_money($en['fee_amount']) ?></td>
                        <td>
                            <?php if (auth_can('classes.manage') && in_array($en['status'], ['enrolled','active'])): ?>
                            <form method="POST" action="/sports/classes/<?= $class['id'] ?>/withdraw" data-ajax="true" style="display:inline">
                                <?= csrf_field() ?>
                                <input type="hidden" name="enrollment_id" value="<?= $en['id'] ?>">
                                <button type="submit" class="btn btn-sm btn-outline-danger" title="Withdraw"><i class="fas fa-user-minus"></i></button>
                            </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($enrollments)): ?><tr><td colspan="6" class="text-muted text-center">No enrollments</td></tr><?php endif; ?>
                    </tbody>
                </table>
                </div>
            </div>
            <div class="tab-pane fade" id="tab-waitlist">
                <table class="table table-sm">
                    <thead><tr><th>Position</th><th>Member</th><th>Registered</th><th>Status</th></tr></thead>
                    <tbody>
                    <?php foreach ($waitlist as $w): ?>
                    <tr><td><?= $w['queue_position'] ?></td><td><?= e($w['member_name'] ?? '') ?></td><td><?= format_date($w['registered_at']) ?></td><td><?= status_badge($w['status']) ?></td></tr>
                    <?php endforeach; ?>
                    <?php if (empty($waitlist)): ?><tr><td colspan="4" class="text-muted text-center">Empty waitlist</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>