<?php
session_start();
include('db_connection.php');

/* ===============================
   AUTH CHECK
================================ */
if (!isset($_SESSION['teacher_id'])) {
    header("Location: teacher_login.php");
    exit();
}

$teacher_id = $_SESSION['teacher_id'];

/* ===============================
   FETCH TIMETABLE
================================ */
$sql = "SELECT 
            t.day,
            t.period,
            t.start_time,
            t.end_time,
            t.location,
            c.class_name,
            s.subject_name
        FROM timetable t
        INNER JOIN classes c ON t.class_id = c.class_id
        INNER JOIN subject s ON t.subject_id = s.subject_id
        WHERE t.teacher_id = ?
        ORDER BY 
            FIELD(t.day, 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
            t.start_time";

$stmt = $conn->prepare($sql);

if (!$stmt) {
    die("SQL Error: " . $conn->error);
}

$stmt->bind_param("i", $teacher_id);
$stmt->execute();
$result = $stmt->get_result();
$timetable = $result->fetch_all(MYSQLI_ASSOC);

$stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Class Timetable</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body {
    background: #f4f6f9;
    font-family: Arial;
}

/* SIDEBAR */
.sidebar {
    width: 240px;
    height: 100vh;
    background: #2c3e50;
    position: fixed;
    padding-top: 20px;
}

.sidebar h4 {
    color: #fff;
    text-align: center;
    margin-bottom: 20px;
}

.sidebar a {
    display: block;
    padding: 12px 20px;
    color: #ecf0f1;
    text-decoration: none;
}

.sidebar a:hover {
    background: #34495e;
}

.main {
    margin-left: 240px;
    padding: 20px;
}

/* CARD */
.card-box {
    background: #fff;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

/* TABLE */
table th {
    background: #2c3e50;
    color: #fff;
}
</style>
</head>

<body>

<!-- SIDEBAR -->
<div class="sidebar">
    <h4>Teacher Panel</h4>

    <a href="teacher_dashboard.php">Dashboard</a>
    <a href="feedback_grading.php">Feedback & Grading</a>
    <a href="attendance.php">Attendance</a>
    <a href="add_homework.php">Assignments</a>
    <a href="class_timetable.php">Timetable</a>
    <a href="profile.php">Profile</a>
    <a href="logout.php">Logout</a>
</div>

<!-- MAIN -->
<div class="main">

    <div class="card-box">
        <h4 class="mb-3">My Timetable</h4>

        <?php if (empty($timetable)): ?>
            <div class="alert alert-info">No timetable found.</div>
        <?php else: ?>

        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Day</th>
                        <th>Period</th>
                        <th>Time</th>
                        <th>Class</th>
                        <th>Subject</th>
                        <th>Location</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($timetable as $row): ?>
                    <tr>
                        <td><?= htmlspecialchars($row['day']) ?></td>
                        <td><?= htmlspecialchars($row['period']) ?></td>
                        <td>
                            <?= htmlspecialchars($row['start_time']) ?> -
                            <?= htmlspecialchars($row['end_time']) ?>
                        </td>
                        <td><?= htmlspecialchars($row['class_name']) ?></td>
                        <td><?= htmlspecialchars($row['subject_name']) ?></td>
                        <td><?= htmlspecialchars($row['location']) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <?php endif; ?>

    </div>

</div>

</body>
</html>