<?php
// Check if attendance_id is provided and valid
if (!isset($_GET['attendance_id']) || !is_numeric($_GET['attendance_id'])) {
    // Handle error, e.g., redirect back to manage_attendance.php with a message
    header("Location: manage_attendance.php?error=Invalid attendance ID");
    exit;
}

$attendance_id = $_GET['attendance_id'];

// Connect to the database (assuming db_connection.php includes your connection code)
include('db_connection.php');

// Fetch attendance record based on attendance_id
$select_query = "SELECT a.teacher_id, a.attendance_date, a.status, t.name
                 FROM attendance a
                 INNER JOIN teachers t ON a.teacher_id = t.teacher_id
                 WHERE a.attendance_id = ?";
$stmt = $conn->prepare($select_query);

if (!$stmt) {
    // Handle error
    die('Error preparing statement: ' . $conn->error);
}

// Bind parameters and execute
$stmt->bind_param("i", $attendance_id);
$stmt->execute();

// Bind result variables
$stmt->bind_result($teacher_id, $date, $status, $teacher_name);

// Fetch the result
$stmt->fetch();

// Close statement
$stmt->close();

// Check if attendance record exists
if (!$teacher_id) {
    // Handle error, e.g., redirect back to manage_attendance.php with a message
    header("Location: manage_attendance.php?error=Attendance not found");
    exit;
}

// Display form with existing attendance details for editing
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Attendance</title>
    <link rel="stylesheet" href="#">
    <style type="text/css">
        /* Reset default browser styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Body styles */
body {
    font-family: Arial, sans-serif;
    background-color: #f0f0f0;
    padding: 20px;
}

/* Container styles */
.container {
    max-width: 600px;
    margin: 0 auto;
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

/* Form styles */
form {
    display: grid;
    gap: 10px;
}

/* Label styles */
label {
    font-weight: bold;
    margin-bottom: 5px;
}

/* Input styles */
input[type="text"],
input[type="date"],
select {
    width: 100%;
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
}

/* Select styles */
select {
    width: calc(100% - 6px); /* Adjust to fit within parent container */
}

/* Button styles */
input[type="submit"] {
    background-color: #4CAF50;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
}

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

/* Error message styles */
.error {
    color: red;
    font-size: 14px;
    margin-top: 5px;
}

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

    </style>
</head>
<body>
    <h2>Edit Attendance</h2>
    <form action="process_edit_attendance.php" method="POST">
        <input type="hidden" name="attendance_id" value="<?php echo $attendance_id; ?>">
        <label for="teacher_name">Teacher Name:</label>
        <input type="text" id="teacher_name" name="teacher_name" value="<?php echo $teacher_name; ?>" readonly><br><br>
        <label for="date">Date:</label>
        <input type="date" id="date" name="date" value="<?php echo $date; ?>"><br><br>
        <label for="status">Status:</label>
        <select id="status" name="status">
            <option value="Present" <?php if ($status == 'Present') echo 'selected'; ?>>Present</option>
            <option value="Absent" <?php if ($status == 'Absent') echo 'selected'; ?>>Absent</option>
        </select><br><br>
        <input type="submit" value="Update Attendance">
    </form>
</body>
</html>
