

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Timetable</title>
    <link rel="stylesheet" href="styles.css"> <!-- Link your CSS file here -->
     <style>
        /* Styles for dropdown and timetable display */
        .container {
            width: 90%;
            margin: 20px auto;
            padding: 20px;
            background: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
        .class-dropdown {
            margin-bottom: 20px;
        }
        .timetable-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        .timetable-table, th, td {
            border: 1px solid #ccc;
        }
        .table{
            width: 100%;
        }
        th, td {
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
    </style>

</head>
<body>
    <div class="container">
        <h2>View Timetable</h2>

        <form action="" method="GET">
            <label for="class_id">Select Class:</label>
            <select name="class_id" id="class_id">
                <?php
                include('db_connection.php'); // Include your database connection file

                // Query to fetch classes from the database
                $query = "SELECT class_id, class_name FROM classes ORDER BY class_id";
                $result = mysqli_query($conn, $query);

                // Check if query successful and fetch results
                if ($result) {
                    while ($row = mysqli_fetch_assoc($result)) {
                        $class_id = $row['class_id'];
                        $class_name = $row['class_name'];
                        echo "<option value='{$class_id}'>{$class_name}</option>";
                    }
                    mysqli_free_result($result);
                } else {
                    echo "<option value=''>No classes available</option>";
                }

                mysqli_close($conn);
                ?>
            </select>

            <label for="day">Select Day:</label>
            <select name="day" id="day">
                <option value="Monday">Monday</option>
                <option value="Tuesday">Tuesday</option>
                <option value="Wednesday">Wednesday</option>
                <option value="Thursday">Thursday</option>
                <option value="Friday">Friday</option>
            </select>

            <button type="submit">View Timetable</button>
        </form>
 <?php
        if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['class_id']) && isset($_GET['day'])) {
            include('db_connection.php'); // Include your database connection file

            $class_id = $_GET['class_id'];
            $day = $_GET['day'];

            // Query to fetch timetable based on class_id and day, joining subjects and teachers
            $query = "SELECT timetable_id, timetable.day, classes.class_name, period, subject.subject_name AS subject_name, teachers.name AS teacher_name
                      FROM timetable
                      JOIN subject ON timetable.subject_id = subject.subject_id
                      JOIN teachers ON timetable.teacher_id = teachers.teacher_id
                      JOIN classes ON timetable.class_id = classes.class_id
                      WHERE timetable.class_id = ? AND timetable.day = ?
                      ORDER BY period";

            $stmt = $conn->prepare($query);
            $stmt->bind_param("is", $class_id, $day);
            $stmt->execute();
            $result = $stmt->get_result();

            // Display timetable in a table format
            if ($result->num_rows > 0) {
                // Fetch the class name for display
                $class_name = "";
                if ($row = mysqli_fetch_assoc($result)) {
                    $class_name = $row['class_name'];
                }
                
                echo "<h3>Timetable for {$day} - {$class_name}</h3>";
                echo "<table class='table'>";
                echo "<tr><th>Day</th><th>Class</th><th>Period</th><th>Subject</th><th>Teacher</th></tr>";
                // Display timetable rows
                mysqli_data_seek($result, 0); // Reset result pointer to fetch rows again
                while ($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>{$row['day']}</td>";
                    echo "<td>{$row['class_name']}</td>";
                    echo "<td>{$row['period']}</td>";
                    echo "<td>{$row['subject_name']}</td>";
                    echo "<td>{$row['teacher_name']}</td>";
                    echo "</tr>";
                }
                echo "</table>";
            } else {
                echo "<p>No timetable entries found for {$day} - Class ID: {$class_id}</p>";
            }

            $stmt->close();
            mysqli_close($conn);
        }
        ?>
    </div>
</body>
</html>