<?php
require_once __DIR__ . '/../../admin/_nav.php';
$pageTitle  = 'Marks Entry';
$sidebarNav = adminNav();
$u = function($p) { return \Core\View::url($p); };
?>
<div class="page-header">
  <div><h1>Assessment Marks Entry</h1><p><?= htmlspecialchars($exam['name']) ?></p></div>
  <a href="<?= $u('admin/exams') ?>" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Back</a>
</div>

<div class="card" style="margin-bottom:20px">
  <div class="card-body" style="padding:14px">
    <form method="GET" style="display:flex;gap:10px;flex-wrap:wrap;align-items:flex-end">
      <div class="form-group" style="margin:0;flex:1;min-width:160px">
        <label>Stream</label>
        <select name="stream_id" onchange="this.form.submit()">
          <option value="">Select Stream</option>
          <?php foreach($streams as $s): ?>
          <option value="<?= $s['id'] ?>" <?= $streamId==$s['id']?'selected':'' ?>><?= htmlspecialchars($s['full_name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="form-group" style="margin:0;flex:1;min-width:160px">
        <label>Subject</label>
        <select name="subject_id">
          <option value="">Select Subject</option>
          <?php foreach($subjects as $s): ?>
          <option value="<?= $s['id'] ?>" <?= $subjectId==$s['id']?'selected':'' ?>><?= htmlspecialchars($s['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <button type="submit" class="btn btn-primary">Load Students</button>
    </form>
  </div>
</div>

<?php if(!empty($students)): ?>
<div class="card">
  <div class="card-header">
    <h5>Enter Marks (<?= count($students) ?> students)</h5>
    <span style="font-size:13px;color:var(--gray)">CBC Competency: EE≥75% | ME≥50% | AE≥25% | BE&lt;25%</span>
  </div>
  <div class="card-body" style="padding:0">
    <form method="POST" action="<?= $u('admin/exams/'.$exam['id'].'/marks') ?>">
      <?= \Core\View::csrf() ?>
      <input type="hidden" name="stream_id" value="<?= $streamId ?>">
      <input type="hidden" name="subject_id" value="<?= $subjectId ?>">
      <div class="table-wrap">
        <table>
          <thead><tr><th>#</th><th>Adm No</th><th>Student Name</th><th>Score (/100)</th><th>Grade</th></tr></thead>
          <tbody>
            <?php foreach($students as $i => $s): ?>
            <?php $m = $existingMarks[$s['id']] ?? null; ?>
            <tr>
              <td><?= $i+1 ?></td>
              <td><?= htmlspecialchars($s['assessment_no']) ?></td>
              <td><?= htmlspecialchars($s['name']) ?></td>
              <td><input type="number" name="marks[<?= $s['id'] ?>]" value="<?= $m['score'] ?? '' ?>" min="0" max="100" step="0.5" style="width:90px" class="mark-input" data-row="<?= $i ?>"></td>
              <td><span class="badge badge-info" id="grade-<?= $i ?>"><?= $m['grade'] ?? '-' ?></span></td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
      <div style="padding:16px;border-top:1px solid var(--border)">
        <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Save Marks</button>
      </div>
    </form>
  </div>
</div>
<?php endif; ?>

<?php $extraScripts = '<script>
function grade(s){
  if(s>=75) return "EE";
  if(s>=50) return "ME";
  if(s>=25) return "AE";
  return "BE";
}
function gradeColor(g){
  return {EE:"#065f46",ME:"#1e40af",AE:"#92400e",BE:"#991b1b"}[g]||"";
}
document.querySelectorAll(".mark-input").forEach(i=>i.addEventListener("input",function(){
  const g=this.value!==""?grade(+this.value):"-";
  const el=document.getElementById("grade-"+this.dataset.row);
  el.textContent=g;
  el.style.color=gradeColor(g);
}));
</script>'; ?>

