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

// Check if the teacher is logged in
if (!isset($_SESSION['teacher_id'])) {
    header("Location: teacher_login.php");
    exit();
}

// Get the class ID from the query string
$class_id = isset($_GET['class_id']) ? intval($_GET['class_id']) : 0;

// Fetch class details
$sql_class = "SELECT class_name FROM classes WHERE class_id = ?";
$stmt_class = $conn->prepare($sql_class);
$stmt_class->bind_param("i", $class_id);
$stmt_class->execute();
$stmt_class->bind_result($class_name);
$stmt_class->fetch();
$stmt_class->close();

// Fetch students in the class
$sql_students = "SELECT student_id, name, fees_paid, fees_outstanding FROM students WHERE class_id = ?";
$stmt_students = $conn->prepare($sql_students);
$stmt_students->bind_param("i", $class_id);
$stmt_students->execute();
$result_students = $stmt_students->get_result();
$students = $result_students->fetch_all(MYSQLI_ASSOC);
$stmt_students->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 Details</title>
    <link rel="stylesheet" href="../styles.css"> <!-- Link to your CSS file -->
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        .header {
            background: #333;
           color: #fff;
            width: 100%;
            display: flex;
            padding: 0 20px;
            text-align: left;
            position: relative;
            justify-content: flex-end;
            height: 60px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            align-items: center;
            display: flex;
            gap: 20px;
        }
        .sidebar {
            background: #444;
            color: #fff;
            position: fixed;
            top: 60px; /* Height of the header */
            left: 0;
            width: 200px;
            height: 100%;
            padding-top: 20px;
            box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
        }
        .sidebar a {
            color: #fff;
            padding: 10px 15px;
            display: block;
            text-decoration: none;
        }
        .sidebar a:hover {
            background: #555;
        }
        .main-content {
            margin-left: 200px; /* Same as the width of the sidebar */
            padding: 20px;
            padding-top: 80px; /* Height of the header */
        }
        .footer {
            background: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: fixed;
            bottom: 0;
            width: 100%;
        }
        .container {
            width: 80%;
            margin: auto;
            overflow: hidden;
        }
        .content {
            padding: 2rem;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            font-size: 14px;
            font-weight: bold;
            text-align: center;
            text-decoration: none;
            color: #fff;
            background-color: #5cb85c;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px 0;
        }
        .btn:hover {
            background-color: #4cae4c;
        }
        .btn-secondary {
            background-color: #0275d8;
        }
        .btn-secondary:hover {
            background-color: #025aa5;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>Class Details</h1>
    </div>

  <div class="sidebar">
        <a href="teacher_dashboard.php">Dashboard</a>
        <a href="feedback_grading.php">Manage Grade</a>
        <a href="attendance.php">Manage Attendance</a>
        <a href="add_homework.php">Add Assignment</a>
        <a href="class_timetable.php">Class Timetable</a>
        <a href="profile.php">Profile</a>
        <a href="logout.php">Logout</a>
    </div>
    <div class="main-content">
        <div class="container">
            <div class="content">
                <h2>Class: <?php echo htmlspecialchars($class_name); ?></h2>
                <h3>Students</h3>
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Fees Paid</th>
                            <th>Fees Outstanding</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($students as $student): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($student['student_id']); ?></td>
                                <td><?php echo htmlspecialchars($student['name']); ?></td>
                                <td><?php echo htmlspecialchars($student['fees_paid']); ?></td>
                                <td><?php echo htmlspecialchars($student['fees_outstanding']); ?></td>
                                <td>
                                    <a href="manage_attendance.php?student_id=<?php echo $student['student_id']; ?>" class="btn">Manage Attendance</a>
                                    <a href="manage_performance.php?student_id=<?php echo $student['student_id']; ?>" class="btn btn-secondary">Manage Performance</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="footer">
        <p>&copy; 2024 Your School Name. All rights reserved.</p>
    </div>
</body>
</html>
