<?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 attendance ID from the query string
$attendance_id = isset($_GET['attendance_id']) ? intval($_GET['attendance_id']) : 0;

if ($attendance_id > 0) {
    // Fetch existing attendance record
    $sql = "SELECT student_id, class_id, date, status FROM student_attendance WHERE attendance_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $attendance_id);
    $stmt->execute();
    $stmt->bind_result($student_id, $class_id, $date, $status);
    $stmt->fetch();
    $stmt->close();
    
    // Fetch student name
    $sql_student = "SELECT name FROM students WHERE student_id = ?";
    $stmt_student = $conn->prepare($sql_student);
    $stmt_student->bind_param("i", $student_id);
    $stmt_student->execute();
    $stmt_student->bind_result($student_name);
    $stmt_student->fetch();
    $stmt_student->close();
    
    // 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();
} else {
    echo "Invalid attendance ID.";
    exit();
}

$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Attendance</title>
    <link rel="stylesheet" href="styles.css"> <!-- Link to your CSS file -->
    <style type="text/css">
             /* 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>Edit Attendance</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>Attendance Record</h2>
                <form action="process_attendance.php" method="POST">
                    <input type="hidden" name="attendance_id" value="<?php echo htmlspecialchars($attendance_id); ?>">
                    <input type="hidden" name="student_id" value="<?php echo htmlspecialchars($student_id); ?>">
                    <input type="hidden" name="class_id" value="<?php echo htmlspecialchars($class_id); ?>">
                    <div>
                        <label for="student_name">Student Name:</label>
                        <input type="text" id="student_name" value="<?php echo htmlspecialchars($student_name); ?>" readonly>
                    </div>
                    <div>
                        <label for="class_name">Class Name:</label>
                        <input type="text" id="class_name" value="<?php echo htmlspecialchars($class_name); ?>" readonly>
                    </div>
                    <div>
                        <label for="date">Date:</label>
                        <input type="date" id="date" name="date" value="<?php echo htmlspecialchars($date); ?>" required>
                    </div>
                    <div>
                        <label for="status">Status:</label>
                        <select id="status" name="status" required>
                            <option value="Present" <?php echo $status == 'Present' ? 'selected' : ''; ?>>Present</option>
                            <option value="Absent" <?php echo $status == 'Absent' ? 'selected' : ''; ?>>Absent</option>
                        </select>
                    </div>
                    <div>
                        <input type="submit" value="Update Attendance">
                    </div>
                </form>
            </div>
        </div>
    </div>

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