<?php
declare(strict_types=1);
require_once __DIR__ . "/../config/database.php";
require_once __DIR__ . "/../includes/auth.php";
requireAuth();
$q = trim($_GET["q"] ?? "");
$sql = "SELECT nom, prenoms, sexe, numero, date_naissance, lieu_naissance FROM stagiaires";
$params = [];
if ($q !== "") {
    $sql .= " WHERE nom LIKE :q OR prenoms LIKE :q OR numero LIKE :q OR lieu_naissance LIKE :q";
    $params["q"] = "%{$q}%";
}
$sql .= " ORDER BY nom";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll();
?>
<!doctype html><html lang="fr"><head><meta charset="utf-8"><title>Apprenants</title><style>body{font-family:Arial}table{width:100%;border-collapse:collapse}th,td{border:1px solid #999;padding:8px;text-align:left}</style></head><body>
<h2>Liste des apprenants</h2>
<table><tr><th>Nom</th><th>Prenoms</th><th>Sexe</th><th>Numero</th><th>Date naissance</th><th>Lieu naissance</th></tr>
<?php foreach ($rows as $r): ?><tr><td><?= htmlspecialchars($r["nom"]) ?></td><td><?= htmlspecialchars($r["prenoms"]) ?></td><td><?= htmlspecialchars((string) $r["sexe"]) ?></td><td><?= htmlspecialchars((string) $r["numero"]) ?></td><td><?= htmlspecialchars((string) $r["date_naissance"]) ?></td><td><?= htmlspecialchars((string) $r["lieu_naissance"]) ?></td></tr><?php endforeach; ?>
</table>
<script>window.print()</script>
</body></html>
