<?php
require_once __DIR__ . '/_nav.php';
$pageTitle  = 'Enter Marks';
$sidebarNav = teacherNav();
$u = function($p) { return \Core\View::url($p); };
?>
<div class="page-header"><div><h1>Enter Marks</h1></div></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:150px"><label>Exam</label>
        <select name="exam_id"><option value="">Select Exam</option>
          <?php foreach($exams as $e): ?><option value="<?= $e['id'] ?>" <?= $examId==$e['id']?'selected':'' ?>><?= htmlspecialchars($e['name']) ?></option><?php endforeach; ?>
        </select>
      </div>
      <div class="form-group" style="margin:0;flex:1;min-width:150px"><label>Stream</label>
        <select name="stream_id"><option value="">Select Stream</option>
          <?php foreach($streams as $s): ?><option value="<?= $s['stream_id'] ?>" <?= $streamId==$s['stream_id']?'selected':'' ?>><?= htmlspecialchars($s['class_name'].' '.$s['stream_name']) ?></option><?php endforeach; ?>
        </select>
      </div>
      <div class="form-group" style="margin:0;flex:1;min-width:150px"><label>Subject</label>
        <select name="subject_id"><option value="">Select Subject</option>
          <?php foreach($streams as $s): ?><option value="<?= $s['subject_id'] ?>" <?= $subjectId==$s['subject_id']?'selected':'' ?>><?= htmlspecialchars($s['subject_name']) ?></option><?php endforeach; ?>
        </select>
      </div>
      <button type="submit" class="btn btn-primary">Load</button>
    </form>
  </div>
</div>
<?php if(!empty($students)): ?>
<div class="card">
  <div class="card-body" style="padding:0">
    <form method="POST" action="<?= $u('teacher/marks/save') ?>">
      <?= \Core\View::csrf() ?>
      <input type="hidden" name="exam_id" value="<?= $examId ?>">
      <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>Student</th><th>Score (/100)</th><th>Grade</th></tr></thead>
          <tbody>
            <?php foreach($students as $i => $s): ?>
            <?php $m = $existing[$s['id']] ?? null; ?>
            <tr>
              <td><?= $i+1 ?></td>
              <td><div style="font-weight:500"><?= htmlspecialchars($s['name']) ?></div><div style="font-size:12px;color:var(--gray)"><?= htmlspecialchars($s['assessment_no']) ?></div></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>
const G=[[80,"A"],[75,"A-"],[70,"B+"],[65,"B"],[60,"B-"],[55,"C+"],[50,"C"],[45,"C-"],[40,"D+"],[35,"D"],[30,"D-"],[0,"E"]];
function grade(s){for(const[m,g]of G)if(s>=m)return g;return"E";}
document.querySelectorAll(".mark-input").forEach(i=>i.addEventListener("input",function(){
  document.getElementById("grade-"+this.dataset.row).textContent=this.value!==""?grade(+this.value):"-";
}));
</script>'; ?>

