<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<?php
  $students = $students ?? [];
  $levels = $levels ?? [];
  $classSectionId = $classSectionId ?? 0;
  $classSectionName = $classSectionName ?? '';
  $classId = (int) ($classId ?? 0);
  $schoolYear = $schoolYear ?? '';
?>

<div class="container-fluid">
  <div class="d-flex flex-wrap align-items-center justify-content-between mb-3 gap-2">
    <div>
      <h2 class="mb-1">Placement Scores</h2>
      <p class="text-muted mb-0">
        Section: <?= esc($classSectionName ?: $classSectionId) ?> • School year: <?= esc($schoolYear) ?>
      </p>
    </div>
    <a href="<?= base_url('grading?class_id=' . rawurlencode((string) $classId)) ?>" class="btn btn-outline-secondary btn-sm">Back to grading</a>
  </div>

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

  <div class="card shadow-sm">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
      <h5 class="mb-0">Set placement score</h5>
      <span class="text-muted small"><?= count($students) ?> student<?= count($students) === 1 ? '' : 's' ?></span>
    </div>
    <div class="card-body">
      <?php if (empty($students)): ?>
        <div class="alert alert-light border text-center mb-0">No students in this section.</div>
      <?php else: ?>
        <form action="<?= base_url('grading/placement') ?>" method="post">
          <?= csrf_field() ?>
          <input type="hidden" name="class_section_id" value="<?= esc($classSectionId) ?>">
          <input type="hidden" name="school_year" value="<?= esc($schoolYear) ?>">

          <div class="table-responsive">
            <table class="table table-bordered table-striped align-middle mt-2">
              <thead class="table-light">
                <tr>
                  <th>School ID</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th style="width: 160px;">Placement Score</th>
                </tr>
              </thead>
              <tbody>
                <?php foreach ($students as $student): ?>
                  <?php
                    $sid = (int) ($student['student_id'] ?? 0);
                    $current = (string) ($levels[$sid] ?? '');
                  ?>
                  <tr>
                    <td><?= esc($student['school_id'] ?? '') ?></td>
                    <td><?= esc($student['firstname'] ?? '') ?></td>
                    <td><?= esc($student['lastname'] ?? '') ?></td>
                    <td>
                      <input type="number"
                             name="placement_level[<?= esc((string) $sid) ?>]"
                             class="form-control form-control-sm"
                             min="0"
                             max="100"
                             step="1"
                             value="<?= esc($current) ?>"
                             placeholder="0-100">
                    </td>
                  </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>
          <div class="d-flex justify-content-end">
            <button type="submit" class="btn btn-primary">Save placement scores</button>
          </div>
        </form>
      <?php endif; ?>
    </div>
  </div>
</div>

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