<?php
session_start();

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

// Get mode from URL
$mode = $_GET['mode'] ?? '';

if ($mode !== 'facilitated' && $mode !== 'self') {
    header("Location: learning_mode.php");
    exit();
}

$error = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $student_name = trim($_POST['student_name'] ?? '');
    $roll_number  = trim($_POST['roll_number'] ?? '');

    // Validation
    if ($mode === 'self') {
        // Self-Assessment: at least one required
        if ($student_name === '' && $roll_number === '') {
            $error = "Please enter your name or roll number.";
        }
    }

    if ($error === "") {
        // Save student details in session
        $_SESSION['student_name'] = $student_name;
        $_SESSION['roll_number']  = $roll_number;
        $_SESSION['assessment_mode'] = $mode;

        // Go to learning style quiz
        header("Location: learning_style.php");
        exit();
    }
}
?>

<?php require_once 'includes/header.php'; ?>

<div class="student-wrap">

    <h2>
        <?php echo ($mode === 'facilitated') ? 'Facilitated Assessment' : 'Self-Assessment'; ?>
    </h2>

    <p class="sub">
        <?php if ($mode === 'facilitated'): ?>
            Enter the student details before starting the assessment.
        <?php else: ?>
            Please enter your details to begin the assessment.
        <?php endif; ?>
    </p>

    <?php if ($error): ?>
        <div class="error-box"><?php echo $error; ?></div>
    <?php endif; ?>

    <form method="POST">

        <label>Student Name</label>
        <input type="text" name="student_name" placeholder="e.g. Arun Kumar">

        <label>Roll Number</label>
        <input type="text" name="roll_number" placeholder="e.g. CS23A014">

        <button type="submit">Proceed to Assessment</button>
    </form>

    <a href="learning_mode.php" class="back-link">← Back</a>
</div>

</body>
</html>
