<?php
declare(strict_types=1);
if (session_status()===PHP_SESSION_NONE) session_start();
require_once __DIR__.'/../includes/db.php';
require_once __DIR__.'/../includes/auth.php';
require_once __DIR__.'/../includes/helpers.php';

require_role(['super_admin','admin','branch_admin']);

$sql = "SELECT s.id, s.student_id, s.batch_id, s.assessment_id, s.final_score, s.updated_at, a.title
        FROM lms_submissions s
        JOIN lms_assessments a ON a.id=s.assessment_id
        WHERE s.workflow_status IN ('coord_approved','admin_review')
        ORDER BY s.updated_at DESC
        LIMIT 50";
$rows = $mysqli->query($sql)->fetch_all(MYSQLI_ASSOC);
?>
<!doctype html><html lang="en"><head>
<meta charset="utf-8"><title>Admin Approvals</title>
<style>
  body{font-family:system-ui,Segoe UI,Arial,sans-serif; background:#f6f7fb}
  .wrap{max-width:1100px;margin:24px auto;padding:0 16px}
  table{width:100%;border-collapse:collapse;background:#fff;border-radius:12px;overflow:hidden}
  th,td{padding:10px;border-bottom:1px solid #eee}
  .btn{padding:6px 10px;border:1px solid #ddd;border-radius:8px;text-decoration:none;background:#fff}
</style>
</head><body><div class="wrap">
  <h1>Admin — Approvals</h1>
  <?php if(!$rows): ?><p>No items waiting for admin.</p>
  <?php else: ?>
  <table>
    <thead><tr><th>Assessment</th><th>Student</th><th>Score</th><th>Updated</th><th></th></tr></thead>
    <tbody>
      <?php foreach($rows as $r): ?>
        <tr>
          <td><?=esc($r['title'])?></td>
          <td>#<?=esc($r['student_id'])?></td>
          <td><?=esc($r['final_score'] ?? '—')?></td>
          <td><?=esc($r['updated_at'])?></td>
          <td><a class="btn" href="approve_decide.php?sid=<?=esc($r['id'])?>">Review</a></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
  <?php endif; ?>
</div></body></html>
