<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<?php
  $batch = $batch ?? [];
  $students = $students ?? [];
  $scores = $scores ?? [];
  $label = ucfirst((string) ($batch['placement_test'] ?? ''));
  $schoolYear = (string) ($batch['school_year'] ?? '');
?>

<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">Edit Placement Batch</h2>
      <p class="text-muted mb-0">
        <?= esc($label) ?> • <?= esc($schoolYear) ?>
      </p>
    </div>
    <a href="<?= base_url('grading/placement') ?>" class="btn btn-outline-secondary btn-sm">Back to batches</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">Students in batch</h5>
      <span class="text-muted small"><?= count($students) ?> student<?= count($students) === 1 ? '' : 's' ?></span>
    </div>
    <div class="card-body pt-2">
      <?php if (empty($students)): ?>
        <div class="alert alert-light border text-center mb-0">No active students found.</div>
      <?php else: ?>
        <form method="post" action="<?= base_url('grading/placement/batch/' . (int) ($batch['id'] ?? 0)) ?>">
          <?= csrf_field() ?>
          <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>Class Section</th>
                  <th style="width: 160px;">Placement Score</th>
                </tr>
              </thead>
              <tbody>
                <?php foreach ($students as $student): ?>
                  <?php
                    $sid = (int) ($student['student_id'] ?? 0);
                    $scoreRow = $scores[$sid] ?? null;
                    $current = $scoreRow ? (string) ($scoreRow['score'] ?? '') : '';
                    $sectionName = (string) ($student['class_section_name'] ?? '');
                    $className = (string) ($student['class_name'] ?? '');
                    $sectionLabel = $className !== '' ? ($className . ' — ' . $sectionName) : $sectionName;
                  ?>
                  <tr>
                    <td><?= esc($student['school_id'] ?? '') ?></td>
                    <td><?= esc($student['firstname'] ?? '') ?></td>
                    <td><?= esc($student['lastname'] ?? '') ?></td>
                    <td><?= esc($sectionLabel ?: '—') ?></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 gap-2">
            <a href="<?= base_url('grading/placement') ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Save changes</button>
          </div>
        </form>
      <?php endif; ?>
    </div>
  </div>
</div>

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