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

if (!isset($_SESSION['teacher_id']) || $_SERVER['REQUEST_METHOD'] !== 'POST') {
    header("Location: attendance.php");
    exit();
}

$class_id = (int)$_POST['class_id'];
$date = $_POST['date'];

$sql = "SELECT student_id, name FROM students WHERE class_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $class_id);
$stmt->execute();
$students = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Mark Attendance</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
.sidebar{width:240px;height:100vh;position:fixed;background:#1e2a38;padding-top:20px}
.sidebar a{display:block;color:#fff;padding:12px;text-decoration:none}
.main{margin-left:240px;padding:20px}
</style>
</head>

<body>

<div class="sidebar">
    <a href="teacher_dashboard.php">Dashboard</a>
    <a href="attendance.php">Attendance</a>
</div>

<div class="main">

<h3>Mark Attendance (<?= $date ?>)</h3>

<form method="POST" action="submit_attendance.php">

<input type="hidden" name="class_id" value="<?= $class_id ?>">
<input type="hidden" name="date" value="<?= $date ?>">

<table class="table table-bordered">
<tr>
<th>Student</th>
<th>Status</th>
</tr>

<?php foreach ($students as $s): ?>
<tr>
<td><?= $s['name'] ?></td>
<td>
<select name="attendance[<?= $s['student_id'] ?>]" class="form-select">
<option value="present">Present</option>
<option value="absent">Absent</option>
</select>
</td>
</tr>
<?php endforeach; ?>

</table>

<button class="btn btn-success">Submit Attendance</button>

</form>

</div>

</body>
</html>