<?php
require_once 'config.inc.php';
require_once('fpdf/fpdf.php');

if (!isset($_SESSION['user'])) {
    header('Location: login.php');
    exit;
}

$user = $_SESSION['user'];
$formation_ref = $_GET['ref'] ?? '';

// Vérifier si formation complétée
$stmt = $dbh->prepare("SELECT * FROM mondialisa_formation_user_progress WHERE user_ref=? AND formation_ref=? AND completed=1");
$stmt->execute([$user['ref'], $formation_ref]);
$progress = $stmt->fetch();

if (!$progress) 
{
    addMsg('info','Formation non complétée')

}else{


    

// Récupérer formation
$stmt = $dbh->prepare("SELECT * FROM mondialisa_formations WHERE ref=?");
$stmt->execute([$formation_ref]);
$formation = $stmt->fetch();

// Génération PDF
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0,10,'Attestation de formation',0,1,'C');
$pdf->Ln(10);
$pdf->SetFont('Arial','',12);
$pdf->MultiCell(0,10,"Nous attestons que ". $user['firstname'] ." ". $user['lastname'] ." a suivi la formation : ". $formation['title'] ." sur Learn Mondialisa.\n\nDate de complétion : ". date('d/m/Y', strtotime($progress['date_completed'])));
$pdf->Ln(10);
$pdf->Cell(0,10,'Signature : Mondialisa.com',0,1,'R');
$pdf->Output('D','Attestation_'.$user['firstname'].'.pdf');
}