<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>

<div class="w-100 py-3 px-3">
    <div class="d-flex flex-wrap justify-content-between align-items-center mb-2">
        <div>
            <h4 class="mb-1" style="font-family: Arial, sans-serif;">Enter Competition Scores</h4>
            <div class="text-muted"><?= esc($competition['title'] ?? 'Competition') ?></div>
        </div>
        <a class="btn btn-outline-secondary" href="<?= base_url('/teacher/competition-scores') ?>">Back</a>
    </div>

    <?php $isLocked = !empty($isLocked); ?>
    <?php $lockedAttr = $isLocked ? 'disabled' : ''; ?>
    <?php if ($isLocked): ?>
        <div class="alert alert-warning">This competition is locked. Scores are read-only.</div>
    <?php endif; ?>

    <div class="mb-3">
        <?php if (!empty($classSectionName)): ?>
            <div><strong>Class Section:</strong> <?= esc($classSectionName) ?></div>
        <?php endif; ?>
        <div>
            <strong>Students:</strong> <?= esc($classStudentCount ?? 0) ?>
            <?php if ($questionCount !== null): ?>
                | <strong>Questions:</strong> <?= esc($questionCount) ?>
            <?php endif; ?>
        </div>
        <?php if (!empty($classSelectionLocked)): ?>
            <div class="text-muted small">This competition is locked to a specific class.</div>
        <?php endif; ?>
    </div>

    <?php if (session('success')): ?>
        <div class="alert alert-success"><?= esc(session('success')) ?></div>
    <?php endif; ?>
    <?php if (session('error')): ?>
        <div class="alert alert-danger"><?= esc(session('error')) ?></div>
    <?php endif; ?>

    <form method="post" action="<?= base_url('/teacher/competition-scores/' . ($competition['id'] ?? 0)) ?>">
        <?= csrf_field() ?>
        <input type="hidden" name="class_section_id" value="<?= esc($classSectionId ?? '') ?>">

        <?php if (empty($classSectionId)): ?>
            <div class="alert alert-warning">Select a class section to load students.</div>
        <?php else: ?>
            <?php $scoreMax = ($questionCount !== null && (int) $questionCount > 0) ? (int) $questionCount : null; ?>
            <div class="table-responsive">
                <table class="table table-bordered table-sm" id="competitionScoresTable">
                    <thead class="table-light">
                        <tr>
                            <th style="width:140px;">School ID</th>
                            <th>Student</th>
                            <th style="width:180px;">
                                Score<?= $scoreMax !== null ? ' (max ' . esc($scoreMax) . ')' : '' ?>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($students as $student): ?>
                        <?php
                            $name = trim(($student['firstname'] ?? '') . ' ' . ($student['lastname'] ?? ''));
                            $studentId = $student['id'] ?? $student['student_id'] ?? null;
                            $schoolId = $student['school_id'] ?? null;
                            $val  = $studentId !== null ? ($scoreMap[$studentId] ?? '') : '';
                            if (is_numeric($val)) {
                                $val = (string) (int) $val;
                            }
                        ?>
                        <tr>
                            <td><?= esc($schoolId ?: $studentId) ?></td>
                            <td><?= esc($name !== '' ? $name : ('Student #' . $studentId)) ?></td>
                            <td>
                                <input class="form-control form-control-sm"
                                       name="scores[<?= esc($studentId) ?>]"
                                       type="number" step="1" min="0" inputmode="numeric" pattern="[0-9]*"
                                       value="<?= esc($val) ?>"
                                       <?= $scoreMax !== null ? 'max="' . esc($scoreMax) . '"' : '' ?>
                                       <?= $lockedAttr ?>>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>

        <div class="d-flex gap-2">
            <button class="btn btn-primary" <?= $lockedAttr ?>>Save Scores</button>
            <a class="btn btn-outline-secondary" href="<?= base_url('/teacher/competition-scores') ?>">Back to list</a>
        </div>
    </form>
</div>

<?= $this->endSection() ?>
