<?php
session_start();
include('config.php');

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Fetch user details
$user_id = $_SESSION['user_id'];
$query = "SELECT * FROM registration WHERE id = '$user_id'";
$userDetails = $db->getRow($query);

require('fpdf/fpdf.php');

class PDF extends FPDF {
    function Header() {
        // Add certificate template image
        $this->Image('certificate_template.jpeg', 0, 0, 297, 210); // A4 landscape size
    }
}

$pdf = new PDF('L', 'mm', 'A4');
$pdf->AddPage();

// Add participant name
$pdf->SetFont('Arial', 'B', 20);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetY(85); // Adjust Y position as needed
$pdf->Cell(0, 10, utf8_decode($userDetails['fullname']), 0, 1, 'C');

// Add city
$pdf->SetFont('Arial', '', 12);
$pdf->SetY(140); // Adjust Y position as needed
$pdf->SetX(70); // Adjust X position as needed
$pdf->Cell(50, 10, utf8_decode($userDetails['city_name']), 0, 0, 'L');

// Add date
$pdf->SetFont('Arial', '', 12);
$pdf->SetY(140); // Adjust Y position as needed
$pdf->SetX(180); // Adjust X position as needed
$pdf->Cell(50, 10, date('d-m-Y'), 0, 0, 'L');

// Add signature
$pdf->Image('sign_image.png', 240, 160, 40); // Adjust position and size

// Force download
$pdf->Output('D', 'Certificate_' . $userDetails['fullname'] . '.pdf');
exit();