<?php
session_start();
include "db.php";

if (!isset($_SESSION['admin'])) {
    header("Location: index.php");
    exit();
}

$result = $conn->query("SELECT studentCode, firstname, surname FROM student");
?>

<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>ระบบเช็คชื่อ</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h2>ระบบเช็คชื่อนักเรียน</h2>
            <a href="logout.php" class="btn btn-danger">ออกจากระบบ</a>
        </div>

        <div class="card p-3 shadow">
            <table class="table table-bordered table-striped">
                <thead class="table-dark">
                    <tr>
                        <th>รหัสนักเรียน</th>
                        <th>ชื่อ</th>
                        <th>นามสกุล</th>
                        <th>เช็คชื่อ</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($row['studentCode']); ?></td>
                            <td><?php echo htmlspecialchars($row['firstname']); ?></td>
                            <td><?php echo htmlspecialchars($row['surname']); ?></td>
                            <td>
                                <form action="mark_attendance.php" method="post" class="d-flex gap-2">
                                    <input type="hidden" name="studentCode" value="<?php echo htmlspecialchars($row['studentCode']); ?>">
                                    <button type="submit" name="status" value="present" class="btn btn-success">มาเรียน</button>
                                    <button type="submit" name="status" value="absent" class="btn btn-warning">ขาดเรียน</button>
                                </form>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>
