<?php
declare(strict_types=1);
require_once __DIR__ . "/../config/database.php";
require_once __DIR__ . "/../includes/auth.php";
requireAuth();
$q = trim($_GET["q"] ?? "");
$sql = "SELECT fo.titre, fo.duree, fo.cout, fr.nom AS formateur
FROM formations fo LEFT JOIN formateurs fr ON fo.formateur_id = fr.id";
$params = [];
if ($q !== "") {
    $sql .= " WHERE fo.titre LIKE :q OR fo.duree LIKE :q OR fr.nom LIKE :q";
    $params["q"] = "%{$q}%";
}
$sql .= " ORDER BY fo.titre";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll();
?>
<!doctype html><html lang="fr"><head><meta charset="utf-8"><title>Formations</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 formations</h2>
<table><tr><th>Titre</th><th>Duree</th><th>Cout</th><th>Formateur</th></tr>
<?php foreach ($rows as $r): ?><tr><td><?= htmlspecialchars($r["titre"]) ?></td><td><?= htmlspecialchars($r["duree"]) ?></td><td><?= htmlspecialchars(number_format((float) $r["cout"], 2, ",", " ")) ?></td><td><?= htmlspecialchars((string) ($r["formateur"] ?? "Non attribue")) ?></td></tr><?php endforeach; ?>
</table>
<script>window.print()</script>
</body></html>
