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

$teachers = $conn->query("SELECT teacher_id, name FROM teachers");
?>

<!DOCTYPE html>
<html>
<head>
<title>Attendance Management | School ERP</title>

<style>
body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #eef2f7;
}

/* ================= HEADER ================= */
.header {
    background: #1e293b;
    color: white;
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.header h2 {
    margin: 0;
    font-size: 18px;
}

.nav a {
    color: white;
    text-decoration: none;
    margin-left: 15px;
    font-size: 14px;
    padding: 6px 10px;
    border-radius: 5px;
    background: rgba(255,255,255,0.1);
}

.nav a:hover {
    background: rgba(255,255,255,0.25);
}

/* ================= CONTAINER ================= */
.container {
    width: 40%;
    margin: 40px auto;
}

/* ================= CARD ================= */
.card {
    background: white;
    padding: 25px;
    border-radius: 10px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.08);
}

.card h3 {
    text-align: center;
    margin-bottom: 20px;
    color: #1e293b;
}

/* ================= FORM ================= */
form {
    display: flex;
    flex-direction: column;
}

select, input {
    padding: 10px;
    margin-bottom: 15px;
    border-radius: 6px;
    border: 1px solid #ccc;
    font-size: 14px;
}

button {
    background: #16a34a;
    color: white;
    padding: 12px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 15px;
}

button:hover {
    background: #15803d;
}

/* ================= MESSAGE ================= */
.success {
    margin-top: 15px;
    padding: 10px;
    background: #dcfce7;
    color: #166534;
    border-left: 5px solid #16a34a;
    border-radius: 5px;
}

/* ================= RESPONSIVE ================= */
@media (max-width: 768px) {
    .container {
        width: 90%;
    }

    .header {
        flex-direction: column;
        align-items: flex-start;
    }

    .nav {
        margin-top: 10px;
    }
}
</style>
</head>

<body>

<!-- ================= HEADER ================= -->
<div class="header">
    <h2>AGID EDUCATIONAL INSTITUTE</h2>

    <div class="nav">
        <a href="admin_dashboard.php">Dashboard</a>
        <a href="manage_teachers.php">Teachers</a>
        <a href="add_payroll.php">Payroll</a>
        <a href="attendance.php">Attendance</a>
    </div>
</div>

<!-- ================= FORM ================= -->
<div class="container">
<div class="card">

<h3>Teacher Attendance</h3>

<form method="POST">

<select name="teacher_id" required>
    <option value="">Select Teacher</option>
    <?php while($t = $teachers->fetch_assoc()): ?>
        <option value="<?= $t['teacher_id'] ?>">
            <?= htmlspecialchars($t['name']) ?>
        </option>
    <?php endwhile; ?>
</select>

<input type="date" name="date" required>

<select name="status" required>
    <option value="Present">Present</option>
    <option value="Absent">Absent</option>
    <option value="Late">Late</option>
</select>

<button type="submit" name="save">Save Attendance</button>

</form>

<?php
if(isset($_POST['save'])){

$stmt = $conn->prepare("
INSERT INTO attendance (teacher_id, date, status)
VALUES (?, ?, ?)
");

$stmt->bind_param("iss", $_POST['teacher_id'], $_POST['date'], $_POST['status']);
$stmt->execute();

echo "<div class='success'>Attendance Saved Successfully ✔</div>";
}
?>

</div>
</div>

</body>
</html>