<?php
require_once __DIR__ . '/../../admin/_nav.php';
$pageTitle  = 'Attendance Report';
$sidebarNav = adminNav();
$u = fn($p) => \Core\View::url($p);
?>
<div class="page-header">
  <div><h1>Attendance Report</h1></div>
  <form method="GET" style="display:flex;gap:8px;flex-wrap:wrap">
    <input type="date" name="from" value="<?= htmlspecialchars($from) ?>" style="width:150px">
    <input type="date" name="to" value="<?= htmlspecialchars($to) ?>" style="width:150px">
    <button type="submit" class="btn btn-primary btn-sm">Generate</button>
  </form>
</div>
<div class="card">
  <div class="table-wrap">
    <table>
      <thead><tr><th>Student</th><th>Class/Stream</th><th>Present</th><th>Absent</th><th>Late</th><th>Excused</th><th>Rate</th></tr></thead>
      <tbody>
        <?php foreach($grouped as $row): ?>
        <?php $total = $row['present']+$row['absent']+$row['late']+$row['excused']; $rate = $total>0?round($row['present']/$total*100,1):0; ?>
        <tr>
          <td><div style="font-weight:500"><?= htmlspecialchars($row['name']) ?></div><div style="font-size:12px;color:var(--gray)"><?= htmlspecialchars($row['assessment_no']) ?></div></td>
          <td><?= htmlspecialchars($row['class'].' '.$row['stream']) ?></td>
          <td><span class="badge badge-success"><?= $row['present'] ?></span></td>
          <td><span class="badge badge-danger"><?= $row['absent'] ?></span></td>
          <td><span class="badge badge-warning"><?= $row['late'] ?></span></td>
          <td><span class="badge badge-info"><?= $row['excused'] ?></span></td>
          <td><strong style="color:<?= $rate>=80?'var(--success)':($rate>=60?'var(--warning)':'var(--danger)') ?>"><?= $rate ?>%</strong></td>
        </tr>
        <?php endforeach; ?>
        <?php if(empty($grouped)): ?><tr><td colspan="7" style="text-align:center;padding:40px;color:var(--gray)">No data for selected period</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

