<?php
declare(strict_types=1);
require_once __DIR__ . "/../config/database.php";
require_once __DIR__ . "/../includes/auth.php";
requireAuth();
$q = trim($_GET["q"] ?? "");
$sql = "SELECT CONCAT(s.nom, ' ', s.prenoms) AS stagiaire, f.titre AS formation, p.montant, p.date_paiement
FROM paiements p
JOIN inscriptions i ON p.inscription_id = i.id
JOIN stagiaires s ON i.stagiaire_id = s.id
JOIN formations f ON i.formation_id = f.id";
$params = [];
if ($q !== "") {
    $sql .= " WHERE s.nom LIKE :q OR s.prenoms LIKE :q OR f.titre LIKE :q OR p.date_paiement LIKE :q";
    $params["q"] = "%{$q}%";
}
$sql .= " ORDER BY p.date_paiement DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll();
?>
<!doctype html><html lang="fr"><head><meta charset="utf-8"><title>Paiements</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 paiements</h2>
<table><tr><th>Apprenant</th><th>Formation</th><th>Montant</th><th>Date</th></tr>
<?php foreach ($rows as $r): ?><tr><td><?= htmlspecialchars($r["stagiaire"]) ?></td><td><?= htmlspecialchars($r["formation"]) ?></td><td><?= htmlspecialchars(number_format((float) $r["montant"], 2, ",", " ")) ?></td><td><?= htmlspecialchars($r["date_paiement"]) ?></td></tr><?php endforeach; ?>
</table>
<script>window.print()</script>
</body></html>
