<?php
session_start();
require_once('../config.php');

if ($_SESSION['role'] != 'student') {
    header("Location: dashboard.php");
    exit();
}

$courses = $pdo->query("SELECT * FROM courses")->fetchAll();

if(isset($_GET['course_id'])){
    $stmt = $pdo->prepare("INSERT IGNORE INTO student_courses (student_id, course_id) VALUES (?, ?)");
    $stmt->execute([$_SESSION['user_id'], $_GET['course_id']]);
}
?>

<div class="container py-5">
<h3>Select Course</h3>

<?php foreach($courses as $c): ?>
<div class="card p-3 mb-3">
<h5><?= $c['course_name']; ?></h5>
<a href="?course_id=<?= $c['id']; ?>" class="btn btn-primary">Enroll</a>
</div>
<?php endforeach; ?>
</div>