<?php
// student/check_result.php
require_once '../includes/auth_check.php';
checkAccess('student');
require_once '../config/database.php';
require_once '../includes/pin_validator.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['check_result'])) {
    $pin = trim($_POST['pin_code']);
    $session = $_POST['session'];
    $term = $_POST['term'];
    $student_id = $_SESSION['student_id'];

    $validation = validatePin($pdo, $pin, $student_id);

    if ($validation === true) {
        // Redirect to view_result.php with parameters
        header("Location: view_result.php?session=$session&term=$term");
        exit();
    } else {
        $error = $validation;
    }
}
?>

<!DOCTYPE html>
<html>
<head><title>Check Result</title></head>
<body>
    <?php include '../includes/header.php'; ?>
    <h2>Access Your Academic Report</h2>
    
    <?php if(isset($error)) echo "<p style='color:red;'>$error</p>"; ?>

    <form method="POST">
        <label>Academic Session:</label><br>
        <select name="session" required>
            <option value="2025/2026">2025/2026</option>
        </select><br><br>

        <label>Term:</label><br>
        <select name="term" required>
            <option value="First Term">First Term</option>
            <option value="Second Term">Second Term</option>
            <option value="Third Term">Third Term</option>
        </select><br><br>

        <label>Enter 16-Digit PIN:</label><br>
        <input type="text" name="pin_code" maxlength="16" placeholder="0000111122223333" required><br><br>

        <button type="submit" name="check_result">View Result</button>
    </form>
</body>
</html>