<?php
// student/register.php - Student Registration (QR Code Entry Point)
require_once '../config/config.php';

$exam_token = $_GET['token'] ?? null;

if (!$exam_token) {
    die('Invalid assessment link');
}

// Get exam details
$stmt = $pdo->prepare("SELECT * FROM exams WHERE exam_token = ? AND status = 'published'");
$stmt->execute([$exam_token]);
$exam = $stmt->fetch();

if (!$exam) {
    die('Assessment not found or not published');
}

$error = '';

// Handle registration
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $full_name = trim($_POST['full_name']);
    $mobile = trim($_POST['mobile']);
    $email = trim($_POST['email']);
    $company = trim($_POST['company']);
    
    // Check if already registered
    $stmt = $pdo->prepare("SELECT id FROM students WHERE mobile = ? AND exam_id = ?");
    $stmt->execute([$mobile, $exam->id]);
    
    if ($stmt->fetch()) {
        $error = 'You have already registered for this assessment with this mobile number.';
    } else {
        // Register student
        $device_fingerprint = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']);
        
        $stmt = $pdo->prepare("
            INSERT INTO students (full_name, mobile, email, company, exam_id, device_fingerprint)
            VALUES (?, ?, ?, ?, ?, ?)
        ");
        $stmt->execute([$full_name, $mobile, $email, $company, $exam->id, $device_fingerprint]);
        $student_id = $pdo->lastInsertId();
        
        // Create student exam entry
        $stmt = $pdo->prepare("
            INSERT INTO student_exams (student_id, exam_id, total_marks, status)
            VALUES (?, ?, ?, 'not_started')
        ");
        $stmt->execute([$student_id, $exam->id, $exam->total_marks]);
        
        // Set session
        $_SESSION['student_id'] = $student_id;
        $_SESSION['student_name'] = $full_name;
        $_SESSION['exam_id'] = $exam->id;
        
        redirect('student/instructions.php');
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register for <?php echo escape($exam->title); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gradient-to-br from-blue-500 to-purple-600 min-h-screen flex items-center justify-center p-4">
    <div class="max-w-md w-full">
        <div class="bg-white rounded-2xl shadow-2xl p-8">
            <div class="text-center mb-6">
                <!-- Time Training Center Logo -->
                <div class="mb-4">
                    <img src="https://www.timetraining.ae/design/images/new_logo.png" 
                         alt="Time Training Center" 
                         class="h-16 mx-auto">
                </div>
                <h1 class="text-2xl font-bold text-gray-800 mb-2">Assessment Registration</h1>
                <h2 class="text-lg text-blue-600 font-semibold"><?php echo escape($exam->title); ?></h2>
            </div>
            
            <?php if ($error): ?>
                <div class="mb-6 bg-red-50 border-l-4 border-red-500 p-4 rounded">
                    <p class="text-red-700 text-sm"><?php echo escape($error); ?></p>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="">
                <div class="mb-4">
                    <label class="block text-gray-700 font-semibold mb-2 text-sm">Full Name *</label>
                    <input type="text" name="full_name" required 
                           class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                           placeholder="Enter your full name">
                </div>
                
                <div class="mb-4">
                    <label class="block text-gray-700 font-semibold mb-2 text-sm">Mobile Number *</label>
                    <input type="tel" name="mobile" required pattern="[0-9]{10,15}"
                           class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                           placeholder="Enter your mobile number">
                </div>
                
                <div class="mb-4">
                    <label class="block text-gray-700 font-semibold mb-2 text-sm">Email *</label>
                    <input type="email" name="email" required 
                           class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                           placeholder="Enter your email">
                </div>
                
                <div class="mb-6">
                    <label class="block text-gray-700 font-semibold mb-2 text-sm">Company *</label>
                    <input type="text" name="company" required 
                           class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                           placeholder="Enter your company name">
                </div>
                
                <div class="mb-6 bg-blue-50 border border-blue-200 rounded-lg p-4">
                    <p class="text-sm text-gray-700">
                        <strong>Training Assessment:</strong><br>
                        <span class="text-blue-600"><?php echo escape($exam->title); ?></span>
                    </p>
                </div>
                
                <button type="submit" 
                        class="w-full bg-gradient-to-r from-blue-600 to-purple-600 text-white font-semibold py-3 rounded-lg hover:from-blue-700 hover:to-purple-700 transition shadow-lg">
                    Register & Continue to Instructions
                </button>
            </form>
        </div>
    </div>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</body>
</html>