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

// Fetch classes and their assigned teachers
$sql = "SELECT classes.class_id, classes.class_name, teachers.name AS teacher_name
        FROM classes
        JOIN teachers ON classes.teacher_id = teachers.teacher_id";
$result = mysqli_query($conn, $sql);

// Check if the query was successful
if (!$result) {
    die("Error fetching classes: " . mysqli_error($conn));
}
?>

<!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="styles.css"> <!-- Link to your CSS file -->
    <title>Manage Classes</title>
    <style>
        .container {
            width: 80%;
            margin: 0 auto;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        
    </style>
</head>
<body>
    <?php include 'header.php'; ?>

    <div class="container">
        <h2>Manage Classes</h2>
        <?php if (mysqli_num_rows($result) > 0) { ?>
           <table>
    <thead>
        <tr>
            <th>Class Name</th>
            <th>Teacher</th>
        </tr>
    </thead>
    <tbody>
        <?php while ($row = mysqli_fetch_assoc($result)) { ?>
            <tr>
                <td><?php echo htmlspecialchars($row['class_name']); ?></td>
                <td><?php echo htmlspecialchars($row['teacher_name']); ?></td>
            </tr>
        <?php } ?>
    </tbody>
</table>

        <?php } else { ?>
            <p>No classes found.</p>
        <?php } ?>
    </div>
</body>
</html>
