<?php
// Include the database connection
include 'db_connection.php';

// Fetch students and classes for the dropdown
$sql_students = "SELECT student_id, name FROM students";
$result_students = mysqli_query($conn, $sql_students);

$sql_classes = "SELECT class_id, class_name FROM classes";
$result_classes = mysqli_query($conn, $sql_classes);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $student_id = $_POST['student_id'];
    $class_id = $_POST['class_id'];

    $sql_enroll = "UPDATE students SET class_id = ? WHERE student_id = ?";
    $stmt = mysqli_prepare($conn, $sql_enroll);
    mysqli_stmt_bind_param($stmt, 'ii', $class_id, $student_id);
    mysqli_stmt_execute($stmt);

    header('Location: manage_classes.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="#"> <!-- Link to your CSS file -->
    <title>Enroll Student</title>
    <style type="text/css">
        body {
    font-family: Arial, sans-serif;
}

.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    background-color: #f8f9fa;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

h2 {
    text-align: center;
    margin-bottom: 20px;
}

.form-group {
    margin-bottom: 15px;
}

label {
    display: block;
    margin-bottom: 5px;
}

input[type="text"],
select {
    width: 100%;
    padding: 8px;
    margin: 5px 0 10px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

.btn {
    background-color: #4CAF50;
    color: white;
    padding: 10px 20px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
    border-radius: 5px;
    border: none;
}

.btn:hover {
    background-color: #45a049;
}

.table {
    width: 100%;
    border-collapse: collapse;
}

.table th, .table td {
    border: 1px solid #ddd;
    padding: 8px;
}

.table th {
    padding-top: 12px;
    padding-bottom: 12px;
    text-align: left;
    background-color: #4CAF50;
    color: white;
}

    </style>
</head>
<body>
    <?php include 'header.php'; ?>

    <div class="container">
        <h2>Enroll Student</h2>
        <form action="enroll_student.php" method="POST">
            <label for="student_id">Select Student:</label>
            <select id="student_id" name="student_id" required>
                <option value="">Select Student</option>
                <?php while ($row = mysqli_fetch_assoc($result_students)) { ?>
                    <option value="<?php echo $row['student_id']; ?>"><?php echo $row['name']; ?></option>
                <?php } ?>
            </select>

            <label for="class_id">Select Class:</label>
            <select id="class_id" name="class_id" required>
                <option value="">Select Class</option>
                <?php while ($row = mysqli_fetch_assoc($result_classes)) { ?>
                    <option value="<?php echo $row['class_id']; ?>"><?php echo $row['class_name']; ?></option>
                <?php } ?>
            </select>

            <button type="submit" class="btn">Enroll Student</button>
        </form>
    </div>
</body>
</html>
