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

// Fetch teachers for the dropdown
$sql_teachers = "SELECT teacher_id, name FROM teachers";
$result_teachers = mysqli_query($conn, $sql_teachers);

// Fetch the class details
$class_id = $_GET['id'];
$sql_class = "SELECT * FROM classes WHERE class_id = ?";
$stmt_class = mysqli_prepare($conn, $sql_class);
mysqli_stmt_bind_param($stmt_class, 'i', $class_id);
mysqli_stmt_execute($stmt_class);
$result_class = mysqli_stmt_get_result($stmt_class);
$class = mysqli_fetch_assoc($result_class);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $class_name = $_POST['class_name'];
    $teacher_id = $_POST['teacher_id'];

    $sql_update = "UPDATE classes SET class_name = ?, teacher_id = ? WHERE class_id = ?";
    $stmt_update = mysqli_prepare($conn, $sql_update);
    mysqli_stmt_bind_param($stmt_update, 'sii', $class_name, $teacher_id, $class_id);
    mysqli_stmt_execute($stmt_update);

    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>Edit Class</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>Edit Class</h2>
        <form action="edit_class.php?id=<?php echo $class_id; ?>" method="POST">
            <label for="class_name">Class Name:</label>
            <input type="text" id="class_name" name="class_name" value="<?php echo $class['class_name']; ?>" required>

            <label for="teacher_id">Assign Teacher:</label>
            <select id="teacher_id" name="teacher_id" required>
                <option value="">Select Teacher</option>
                <?php while ($row = mysqli_fetch_assoc($result_teachers)) { ?>
                    <option value="<?php echo $row['teacher_id']; ?>" <?php if ($row['teacher_id'] == $classes['teacher_id']) echo 'selected'; ?>><?php echo $row['name']; ?></option>
                <?php } ?>
            </select>

            <button type="submit" class="btn">Update Class</button>
        </form>
    </div>
</body>
</html>
