<?php
require('fpdf/fpdf.php');
include 'db_config.php';

if (isset($_GET['id'])) {
    $user_id = $_GET['id'];

    // Fetch user data from the database
    $stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $user = $result->fetch_assoc();

    if ($user) {
        class PDF extends FPDF
        {
            function Header()
            {
                // Arial bold 15
                $this->SetFont('Arial', 'B', 20);
                // Title
                $this->Cell(0, 10, 'Mips Annual Conference 2025', 0, 1, 'C');
                // Line break
                $this->Ln(10);
            }

            function Footer()
            {
                // Position at 1.5 cm from bottom
                $this->SetY(-15);
                // Arial italic 8
                $this->SetFont('Arial', 'I', 8);
                // Page number
                $this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');
            }
        }

        $pdf = new PDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 18);

        // Add user details
        $pdf->Cell(0, 10, 'Full Name: ' . $user['fullname'], 0, 1, 'C');
        // $pdf->Cell(0, 10, 'Email: ' . $user['email'], 0, 1, 'C');
        // $pdf->Cell(0, 10, 'Phone: ' . $user['phone'], 0, 1, 'C');
        // $pdf->Cell(0, 10, 'Payment Status: ' . $user['payment_status'], 0, 1, 'C');
        // $pdf->Cell(0, 10, 'Hotel: ' . $user['hotel'], 0, 1, 'C');
        // $pdf->Cell(0, 10, 'Prizes: ' . $user['prizes'], 0, 1, 'C');
        $pdf->Cell(0, 10, 'Institution: ' . $user['institution'], 0, 1, 'C');
        // $pdf->Cell(0, 10, 'Membership Type: ' . $user['membership_type'], 0, 1, 'C');

        // Center QR code image
        $qrSize = 70; // Size of the QR code
        $pageWidth = $pdf->GetPageWidth();
        $pageHeight = $pdf->GetPageHeight();
        $qrX = ($pageWidth - $qrSize) / 2;
        $qrY = ($pageHeight - $qrSize) / 4;

        $pdf->Image($user['qr_code'], $qrX, $qrY, $qrSize, $qrSize);

        $pdf->Output('D',  $user['fullname'] . ' QR Code' . '.pdf');
    } else {
        echo "User not found.";
    }

    $stmt->close();
}

$conn->close();
?>
