<?php
// QR Code Generator - Displays QR code for download
require_once 'config.php';

// Get event slug
$event_slug = isset($_GET['event']) ? trim($_GET['event']) : '';

if (empty($event_slug)) {
    die('No event specified.');
}

// Sanitize
$event_slug = preg_replace('/[^a-z0-9-]/', '', strtolower($event_slug));

// Verify event exists
$conn = getDBConnection();
$stmt = $conn->prepare("SELECT event_name FROM events WHERE event_slug = ?");
$stmt->bind_param("s", $event_slug);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    $stmt->close();
    $conn->close();
    die('Event not found.');
}

$event_data = $result->fetch_assoc();
$stmt->close();
$conn->close();

// Get base URL
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$base_url = $protocol . '://' . $host . dirname($_SERVER['SCRIPT_NAME']);
$event_url = $base_url . '/index.php?event=' . urlencode($event_slug);

// QR code size
$size = isset($_GET['size']) ? intval($_GET['size']) : 500;
$size = min(max($size, 100), 1000); // Limit between 100 and 1000
$qr_url = 'https://api.qrserver.com/v1/create-qr-code/?size=' . $size . 'x' . $size . '&data=' . urlencode($event_url);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>QR Code - <?php echo htmlspecialchars($event_data['event_name']); ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            background-color: #ffffff;
            border-radius: 8px;
            padding: 40px;
            max-width: 600px;
            width: 100%;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            font-size: 24px;
            font-weight: 400;
            margin-bottom: 20px;
            color: #2c3e50;
        }

        .qr-code {
            margin: 20px 0;
        }

        .qr-code img {
            border: 1px solid #e0e0e0;
            padding: 20px;
            background: white;
            border-radius: 4px;
            max-width: 100%;
        }

        .download-btn {
            display: inline-block;
            margin-top: 20px;
            padding: 12px 30px;
            background-color: #2c3e50;
            color: #ffffff;
            text-decoration: none;
            border-radius: 4px;
            font-size: 16px;
            transition: background-color 0.2s;
        }

        .download-btn:hover {
            background-color: #34495e;
        }

        .info {
            font-size: 14px;
            color: #6c757d;
            margin-top: 15px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>QR Code</h1>
        <p class="info"><?php echo htmlspecialchars($event_data['event_name']); ?></p>
        <div class="qr-code">
            <img src="<?php echo htmlspecialchars($qr_url); ?>" alt="QR Code">
        </div>
        <a href="<?php echo htmlspecialchars($qr_url); ?>" class="download-btn" download>Download QR Code</a>
        <p class="info" style="margin-top: 20px;">Right-click the image and select "Save image as..." to download</p>
    </div>
</body>
</html>

