<?php
require_once __DIR__ . '/_nav.php';
$pageTitle  = 'Attendance';
$sidebarNav = teacherNav();
$u = function($p) { return \Core\View::url($p); };
?>
<div class="page-header"><div><h1>Take Attendance</h1><p><?= date('l, d F Y', strtotime($date)) ?></p></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">
      <select name="stream_id" style="width:200px">
        <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>
      <input type="date" name="date" value="<?= htmlspecialchars($date) ?>" style="width:160px">
      <button type="submit" class="btn btn-primary btn-sm">Load</button>
    </form>
  </div>
</div>
<?php if(!empty($students)): ?>
<div class="card">
  <div class="card-header">
    <h5>Students (<?= count($students) ?>)</h5>
    <div style="display:flex;gap:8px">
      <button onclick="markAll('present')" class="btn btn-sm btn-secondary" style="color:var(--success)">All Present</button>
      <button onclick="markAll('absent')" class="btn btn-sm btn-secondary" style="color:var(--danger)">All Absent</button>
    </div>
  </div>
  <div class="card-body" style="padding:0">
    <form method="POST" action="<?= $u('teacher/attendance/save') ?>">
      <?= \Core\View::csrf() ?>
      <input type="hidden" name="stream_id" value="<?= $streamId ?>">
      <input type="hidden" name="date" value="<?= htmlspecialchars($date) ?>">
      <div class="table-wrap">
        <table>
          <thead><tr><th>#</th><th>Student</th><th>Present</th><th>Absent</th><th>Late</th><th>Excused</th></tr></thead>
          <tbody>
            <?php foreach($students as $i => $s): ?>
            <?php $cur = $existing[$s['id']] ?? 'present'; ?>
            <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>
              <?php foreach(['present','absent','late','excused'] as $st): ?>
              <td style="text-align:center"><input type="radio" name="attendance[<?= $s['id'] ?>]" value="<?= $st ?>" <?= $cur===$st?'checked':'' ?> style="width:18px;height:18px;accent-color:var(--primary)"></td>
              <?php endforeach; ?>
            </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 Attendance</button></div>
    </form>
  </div>
</div>
<?php endif; ?>
<?php $extraScripts = '<script>function markAll(s){document.querySelectorAll(`input[type=radio][value="${s}"]`).forEach(r=>r.checked=true)}</script>'; ?>

