<?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 class_id and date from POST
$class_id = isset($_POST['class_id']) ? intval($_POST['class_id']) : 0;
$date = isset($_POST['date']) ? $_POST['date'] : '';

if ($class_id == 0 || $date == '') {
    header("Location: add_attendance.php");
    exit();
}

// Fetch class name
$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 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>Mark Attendance</title>
    <link rel="stylesheet" href="../styles.css"> <!-- Link to your CSS file -->
    <style>
        /* styles.css */

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
}

.header {
    background: #333;
    color: #fff;
    padding: 1rem 0;
    text-align: center;
    position: fixed;
    width: 100%;
    top: 0;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.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);
}

form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

label {
    font-weight: bold;
}

select, input[type="date"], input[type="submit"] {
    padding: 10px;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 4px;
}

input[type="submit"] {
    background-color: #333;
    color: #fff;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

input[type="submit"]:hover {
    background-color: #555;
}

    </style>
</head>
<body>
    <div class="header">
        <h1>Mark Attendance for <?php echo htmlspecialchars($class_name); ?> on <?php echo htmlspecialchars($date); ?></h1>
    </div>

    <div class="sidebar">
        <a href="teacher_dashboard.php">Dashboard</a>
        <a href="feedback_grading.php">Manage Feedback and Grading</a>
        <a href="attendance.php">Manage Attendance</a>
        <a href="profile.php">Profile</a>
        <a href="logout.php">Logout</a>
    </div>

    <div class="main-content">
        <div class="container">
            <div class="content">
                <form method="POST" action="submit_attendance.php">
                    <input type="hidden" name="class_id" value="<?php echo $class_id; ?>">
                    <input type="hidden" name="date" value="<?php echo $date; ?>">
                    <table>
                        <thead>
                            <tr>
                                <th>Student ID</th>
                                <th>Student Name</th>
                                <th>Attendance</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>
                                        <select name="attendance[<?php echo $student['student_id']; ?>]">
                                            <option value="present">Present</option>
                                            <option value="absent">Absent</option>
                                        </select>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <br>
                    <input type="submit" value="Submit Attendance">
                </form>
            </div>
        </div>
    </div>

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