<?php
session_start();
session_regenerate_id(true);

// Enforce HTTPS
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}

// Disable debug in production
if ($_SERVER['SERVER_NAME'] !== 'localhost') {
    ini_set('display_errors', 0);
    error_reporting(0);
}

// Check if admin is logged in
if (!isset($_SESSION['student_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: login.php");
    exit();
}

// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "school_db";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    error_log("Connection failed: " . $e->getMessage());
    $error = "Connection failed. Please contact support.";
}

// CSRF token
if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$csrf_token = $_SESSION['csrf_token'];

// Fetch admin's student details
try {
    $stmt = $conn->prepare("SELECT name, grade, contact FROM students WHERE id = :student_id");
    $stmt->execute([':student_id' => $_SESSION['student_id']]);
    $admin_student = $stmt->fetch(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    error_log("Admin student fetch error: " . $e->getMessage());
    $error = "Error fetching admin's student data.";
}

// Fetch contact messages
$messages = [];
try {
    $stmt = $conn->query("SELECT id, name, email, message, submitted_date, status FROM contact_messages ORDER BY submitted_date DESC");
    $messages = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch(PDOException $e) {
    error_log("Contact messages fetch error: " . $e->getMessage());
    $error = "Error fetching contact messages.";
}

// Handle update/delete/status
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['csrf_token'] === $csrf_token) {
    if ($_POST['action'] === 'update_message') {
        $id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
        $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
        $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
        $message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
        $status = filter_input(INPUT_POST, 'status', FILTER_SANITIZE_STRING);

        if ($id && $name && $email && $message && in_array($status, ['read', 'unread'])) {
            try {
                $stmt = $conn->prepare("UPDATE contact_messages SET name = :name, email = :email, message = :message, status = :status WHERE id = :id");
                $stmt->execute([
                    ':name' => $name,
                    ':email' => $email,
                    ':message' => $message,
                    ':status' => $status,
                    ':id' => $id
                ]);
                $success = "Message updated successfully.";
                header("Location: manage_contact_messages.php?success=" . urlencode($success));
                exit();
            } catch(PDOException $e) {
                error_log("Update message error: " . $e->getMessage());
                $error = "Error updating message.";
            }
        } else {
            $error = "Invalid message details or status.";
        }
    } elseif ($_POST['action'] === 'delete_message') {
        $id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
        if ($id) {
            try {
                $stmt = $conn->prepare("DELETE FROM contact_messages WHERE id = :id");
                $stmt->execute([':id' => $id]);
                $success = "Message deleted successfully.";
                header("Location: manage_contact_messages.php?success=" . urlencode($success));
                exit();
            } catch(PDOException $e) {
                error_log("Delete message error: " . $e->getMessage());
                $error = "Error deleting message.";
            }
        } else {
            $error = "Invalid message ID.";
        }
    } elseif ($_POST['action'] === 'update_status') {
        $id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
        $status = filter_input(INPUT_POST, 'status', FILTER_SANITIZE_STRING);
        if ($id && in_array($status, ['read', 'unread'])) {
            try {
                $stmt = $conn->prepare("UPDATE contact_messages SET status = :status WHERE id = :id");
                $stmt->execute([':status' => $status, ':id' => $id]);
                $success = "Message status updated to $status.";
                header("Location: manage_contact_messages.php?success=" . urlencode($success));
                exit();
            } catch(PDOException $e) {
                error_log("Update message status error: " . $e->getMessage());
                $error = "Error updating message status.";
            }
        } else {
            $error = "Invalid status or message ID.";
        }
    }
}

// Handle logout
if (isset($_POST['logout']) && $_POST['csrf_token'] === $csrf_token) {
    session_unset();
    session_destroy();
    header("Location: login.php");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Manage contact messages for Gakero E.L.C.K High School.">
    <meta name="keywords" content="Gakero E.L.C.K High School, admin, contact messages management">
    <meta name="author" content="Gakero E.L.C.K High School">
    <title>Manage Contact Messages - Gakero E.L.C.K High School</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
    <style>
        * { box-sizing: border-box; }
        body { font-family: 'Poppins', sans-serif; background-color: #f5f6f5; color: #333; margin: 0; overflow-x: hidden; }
        .navbar { background-color: #003087; padding: 8px 0; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); z-index: 1001; position: sticky; top: 0; }
        .navbar-brand { font-size: 1.25rem; font-weight: 600; color: #fff !important; }
        .nav-link { font-size: 0.9rem; color: #fff !important; padding: 6px 12px; transition: color 0.3s ease, background-color 0.3s ease; }
        .nav-link:hover { color: #a9d6e5 !important; background-color: rgba(255, 255, 255, 0.1); border-radius: 4px; }
        .navbar-toggler { border-color: rgba(255, 255, 255, 0.5); }
        .sidebar { position: fixed; top: 48px; left: 0; width: 250px; height: calc(100vh - 48px); background-color: #003087; color: #fff; overflow-y: auto; z-index: 1000; box-shadow: 2px 0 4px rgba(0, 0, 0, 0.1); transition: transform 0.3s ease; }
        .sidebar-header { border-bottom: 1px solid rgba(255, 255, 255, 0.2); }
        .sidebar .nav-link { font-size: 0.95rem; padding: 10px 15px; color: #fff; transition: background-color 0.3s ease, padding-left 0.3s ease; }
        .sidebar .nav-link:hover { background-color: rgba(255, 255, 255, 0.1); padding-left: 20px; }
        .profile-card { background: rgba(255, 255, 255, 0.05); border-radius: 8px; margin: 15px; padding: 15px; }
        .avatar { width: 80px; height: 80px; display: flex; align-items: center; justify-content: center; border-radius: 50%; }
        main { margin-left: 250px; padding: 15px; min-height: calc(100vh - 48px); background-color: #fff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05); z-index: 999; width: calc(100% - 250px); }
        footer { background-color: #003087; color: #fff; padding: 20px 0; margin-top: 20px; z-index: 999; width: 100%; }
        footer a { color: #a9d6e5; text-decoration: none; transition: color 0.3s ease; }
        footer a:hover { color: #fff; }
        .hero-section { background: linear-gradient(135deg, #003087, #005f73); border-radius: 8px; padding: 20px; margin-bottom: 2rem; }
        .hero-section h1 { font-size: 1.75rem; font-weight: 600; }
        .hero-section p { font-size: 1rem; }
        .section-title { font-size: 1.75rem; color: #003087; margin-bottom: 1rem; position: relative; }
        .section-title::after { content: ''; display: block; width: 50px; height: 3px; background: #003087; margin-top: 0.5rem; }
        .card { border: none; border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); transition: transform 0.3s ease, box-shadow 0.3s ease; }
        .card:hover { transform: translateY(-5px); box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2); }
        .card-title { font-size: 1.25rem; color: #003087; }
        .btn-primary { background-color: #003087; border: none; padding: 0.75rem 1.5rem; border-radius: 8px; transition: background-color 0.3s ease; }
        .btn-primary:hover { background-color: #00205b; }
        .btn-outline-primary { border-color: #003087; color: #003087; }
        .btn-outline-primary:hover { background-color: #003087; color: #fff; }
        .btn-outline-danger { border-color: #dc3545; color: #dc3545; }
        .btn-outline-danger:hover { background-color: #dc3545; color: #fff; }
        .table { margin-top: 1rem; }
        .table th { background-color: #e9ecef; color: #003087; }
        .form-control { border-radius: 8px; border: 1px solid #ced4da; transition: border-color 0.3s ease, box-shadow 0.3s ease; }
        .form-control:focus { border-color: #003087; box-shadow: 0 0 5px rgba(0, 48, 135, 0.5); }
        .form-label { color: #003087; }
        .alert { border-radius: 8px; font-size: 0.9rem; }
        @media (max-width: 991px) {
            .sidebar { display: none; }
            main { margin-left: 0; width: 100%; }
            .navbar-brand { font-size: 1.1rem; }
            .nav-link { font-size: 0.85rem; }
            .hero-section { padding: 2rem 0; }
            .hero-section h1 { font-size: 1.5rem; }
            .section-title { font-size: 1.5rem; }
        }
        @media (min-width: 992px) {
            .sidebar { display: block; }
            main { margin-left: 250px; width: calc(100% - 250px); }
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg bg-primary text-white">
        <div class="container-fluid">
            <a class="navbar-brand" href="index.php">Gakero E.L.C.K High School</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item"><a class="nav-link" href="index.php">Home</a></li>
                    <li class="nav-item"><a class="nav-link" href="about.php">About</a></li>
                    <li class="nav-item"><a class="nav-link" href="contact.php">Contact</a></li>
                    <li class="nav-item"><a class="nav-link" href="news.php">News</a></li>
                    <li class="nav-item">
                        <form method="POST" action="manage_contact_messages.php" style="display:inline;">
                            <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token); ?>">
                            <button type="submit" name="logout" class="nav-link btn btn-link text-white">Logout</button>
                        </form>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="d-flex">
        <nav class="sidebar bg-primary text-white" id="sidebar">
            <div class="sidebar-header text-center p-3">
                <h4 class="mb-0">Gakero E.L.C.K</h4>
            </div>
            <div class="profile-card">
                <div class="text-center">
                    <div class="avatar bg-light text-primary rounded-circle mx-auto mb-2" role="img" aria-label="Admin profile icon">
                        <i class="bi bi-person-circle fs-2"></i>
                    </div>
                    <h6 class="mb-1"><?php echo htmlspecialchars($admin_student['name'] ?? 'Admin'); ?></h6>
                    <small class="text-white-50">ID: <?php echo htmlspecialchars($_SESSION['student_id'] ?? 'N/A'); ?></small><br>
                    <small class="text-white-50">Grade: <?php echo htmlspecialchars($admin_student['grade'] ?? 'N/A'); ?></small><br>
                    <small class="text-white-50">Contact: <?php echo htmlspecialchars($admin_student['contact'] ?? 'N/A'); ?></small>
                </div>
            </div>
            <ul class="nav flex-column p-3">
                <li class="nav-item"><a class="nav-link text-white" href="admin_page.php"><i class="bi bi-house-door me-2"></i>Dashboard</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="accommodation.php"><i class="bi bi-building me-2"></i>Manage Accommodation</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_academics.php"><i class="bi bi-book me-2"></i>Manage Academics</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_clearance.php"><i class="bi bi-check-circle me-2"></i>Manage Clearance</a></li>
                <li class="nav-item"><a class="nav-link text-white active" href="manage_contact_messages.php"><i class="bi bi-envelope me-2"></i>Manage Contact Messages</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_financials.php"><i class="bi bi-wallet me-2"></i>Manage Financials</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_news.php"><i class="bi bi-newspaper me-2"></i>Manage News</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_resources.php"><i class="bi bi-file-earmark me-2"></i>Manage Resources</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_special_exams.php"><i class="bi bi-pencil-square me-2"></i>Manage Special Exams</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_students.php"><i class="bi bi-people me-2"></i>Manage Students</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_student_requests.php"><i class="bi bi-file-text me-2"></i>Manage Student Requests</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_timetables.php"><i class="bi bi-calendar me-2"></i>Manage Timetables</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_transactions.php"><i class="bi bi-currency-exchange me-2"></i>Manage Transactions</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_transfers.php"><i class="bi bi-arrow-left-right me-2"></i>Manage Transfers</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_users.php"><i class="bi bi-person-circle me-2"></i>Manage Users</a></li>
                <li class="nav-item"><a class="nav-link text-white" href="manage_rooms.php"><i class="bi bi-door-open me-2"></i>Manage Rooms</a></li>
                <li class="nav-item">
                    <form method="POST">
                        <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token); ?>">
                        <button type="submit" name="logout" class="nav-link text-white bg-transparent border-0"><i class="bi bi-box-arrow-right me-2"></i>Logout</button>
                    </form>
                </li>
            </ul>
        </nav>

        <main class="flex-grow-1 p-4">
            <button class="btn btn-primary d-lg-none mb-3" type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebarOffcanvas" aria-controls="sidebarOffcanvas">
                <i class="bi bi-list"></i> Menu
            </button>
            <div class="offcanvas offcanvas-start bg-primary text-white" tabindex="-1" id="sidebarOffcanvas" aria-labelledby="sidebarOffcanvasLabel">
                <div class="offcanvas-header">
                    <h5 class="offcanvas-title" id="sidebarOffcanvasLabel">Gakero E.L.C.K</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvas" aria-label="Close"></button>
                </div>
                <div class="offcanvas-body">
                    <div class="profile-card">
                        <div class="text-center">
                            <div class="avatar bg-light text-primary rounded-circle mx-auto mb-2" role="img" aria-label="Admin profile icon">
                                <i class="bi bi-person-circle fs-2"></i>
                            </div>
                            <h6 class="mb-1"><?php echo htmlspecialchars($admin_student['name'] ?? 'Admin'); ?></h6>
                            <small class="text-white-50">ID: <?php echo htmlspecialchars($_SESSION['student_id'] ?? 'N/A'); ?></small><br>
                            <small class="text-white-50">Grade: <?php echo htmlspecialchars($admin_student['grade'] ?? 'N/A'); ?></small><br>
                            <small class="text-white-50">Contact: <?php echo htmlspecialchars($admin_student['contact'] ?? 'N/A'); ?></small>
                        </div>
                    </div>
                    <ul class="nav flex-column">
                        <li class="nav-item"><a class="nav-link text-white" href="admin_page.php"><i class="bi bi-house-door me-2"></i>Dashboard</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="accommodation.php"><i class="bi bi-building me-2"></i>Manage Accommodation</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_academics.php"><i class="bi bi-book me-2"></i>Manage Academics</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_clearance.php"><i class="bi bi-check-circle me-2"></i>Manage Clearance</a></li>
                        <li class="nav-item"><a class="nav-link text-white active" href="manage_contact_messages.php"><i class="bi bi-envelope me-2"></i>Manage Contact Messages</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_financials.php"><i class="bi bi-wallet me-2"></i>Manage Financials</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_news.php"><i class="bi bi-newspaper me-2"></i>Manage News</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_resources.php"><i class="bi bi-file-earmark me-2"></i>Manage Resources</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_special_exams.php"><i class="bi bi-pencil-square me-2"></i>Manage Special Exams</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_students.php"><i class="bi bi-people me-2"></i>Manage Students</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_student_requests.php"><i class="bi bi-file-text me-2"></i>Manage Student Requests</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_timetables.php"><i class="bi bi-calendar me-2"></i>Manage Timetables</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_transactions.php"><i class="bi bi-currency-exchange me-2"></i>Manage Transactions</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_transfers.php"><i class="bi bi-arrow-left-right me-2"></i>Manage Transfers</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_users.php"><i class="bi bi-person-circle me-2"></i>Manage Users</a></li>
                        <li class="nav-item"><a class="nav-link text-white" href="manage_rooms.php"><i class="bi bi-door-open me-2"></i>Manage Rooms</a></li>
                        <li class="nav-item">
                            <form method="POST">
                                <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token); ?>">
                                <button type="submit" name="logout" class="nav-link text-white bg-transparent border-0"><i class="bi bi-box-arrow-right me-2"></i>Logout</button>
                            </form>
                        </li>
                    </ul>
                </div>
            </div>

            <header class="hero-section text-white text-center py-4 rounded mb-4">
                <h1 class="display-5">Manage Contact Messages</h1>
                <p class="lead">View, update, or delete contact messages</p>
            </header>

            <?php if (isset($error)): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <?php echo htmlspecialchars($error); ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            <?php endif; ?>
            <?php if (isset($success)): ?>
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <?php echo htmlspecialchars($success); ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            <?php endif; ?>

            <section class="mb-5">
                <h2 class="section-title">Contact Messages</h2>
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th scope="col">Name</th>
                                <th scope="col">Email</th>
                                <th scope="col">Message</th>
                                <th scope="col">Submitted Date</th>
                                <th scope="col">Status</th>
                                <th scope="col">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($messages)): ?>
                                <?php foreach ($messages as $item): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($item['name']); ?></td>
                                        <td><?php echo htmlspecialchars($item['email']); ?></td>
                                        <td><?php echo htmlspecialchars(substr($item['message'], 0, 50)) . (strlen($item['message']) > 50 ? '...' : ''); ?></td>
                                        <td><?php echo date('F j, Y', strtotime($item['submitted_date'])); ?></td>
                                        <td><?php echo htmlspecialchars($item['status']); ?></td>
                                        <td>
                                            <button class="btn btn-outline-primary btn-sm" data-bs-toggle="modal" data-bs-target="#editMessageModal<?php echo $item['id']; ?>">Edit</button>
                                            <form method="POST" style="display: inline;" onsubmit="return confirm('Are you sure you want to delete this message?');">
                                                <input type="hidden" name="action" value="delete_message">
                                                <input type="hidden" name="id" value="<?php echo htmlspecialchars($item['id']); ?>">
                                                <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token); ?>">
                                                <button type="submit" class="btn btn-outline-danger btn-sm">Delete</button>
                                            </form>
                                            <form method="POST" style="display: inline;">
                                                <input type="hidden" name="action" value="update_status">
                                                <input type="hidden" name="id" value="<?php echo htmlspecialchars($item['id']); ?>">
                                                <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token); ?>">
                                                <select name="status" onchange="this.form.submit()" class="form-select form-select-sm d-inline-block w-auto">
                                                    <option value="unread" <?php echo $item['status'] === 'unread' ? 'selected' : ''; ?>>Unread</option>
                                                    <option value="read" <?php echo $item['status'] === 'read' ? 'selected' : ''; ?>>Read</option>
                                                </select>
                                            </form>
                                        </td>
                                    </tr>
                                    <div class="modal fade" id="editMessageModal<?php echo $item['id']; ?>" tabindex="-1" aria-labelledby="editMessageModalLabel<?php echo $item['id']; ?>" aria-hidden="true">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h5 class="modal-title" id="editMessageModalLabel<?php echo $item['id']; ?>">Edit Message</h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <form method="POST">
                                                        <input type="hidden" name="action" value="update_message">
                                                        <input type="hidden" name="id" value="<?php echo htmlspecialchars($item['id']); ?>">
                                                        <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token); ?>">
                                                        <div class="mb-3">
                                                            <label for="name_<?php echo $item['id']; ?>" class="form-label">Name</label>
                                                            <input type="text" id="name_<?php echo $item['id']; ?>" name="name" class="form-control" value="<?php echo htmlspecialchars($item['name']); ?>" required>
                                                        </div>
                                                        <div class="mb-3">
                                                            <label for="email_<?php echo $item['id']; ?>" class="form-label">Email</label>
                                                            <input type="email" id="email_<?php echo $item['id']; ?>" name="email" class="form-control" value="<?php echo htmlspecialchars($item['email']); ?>" required>
                                                        </div>
                                                        <div class="mb-3">
                                                            <label for="message_<?php echo $item['id']; ?>" class="form-label">Message</label>
                                                            <textarea id="message_<?php echo $item['id']; ?>" name="message" class="form-control" rows="5" required><?php echo htmlspecialchars($item['message']); ?></textarea>
                                                        </div>
                                                        <div class="mb-3">
                                                            <label for="status_<?php echo $item['id']; ?>" class="form-label">Status</label>
                                                            <select id="status_<?php echo $item['id']; ?>" name="status" class="form-control" required>
                                                                <option value="unread" <?php echo $item['status'] === 'unread' ? 'selected' : ''; ?>>Unread</option>
                                                                <option value="read" <?php echo $item['status'] === 'read' ? 'selected' : ''; ?>>Read</option>
                                                            </select>
                                                        </div>
                                                        <button type="submit" class="btn btn-primary">Save Changes</button>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr><td colspan="6" class="text-center">No contact messages found.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </main>
    </div>

    <footer class="bg-dark text-white text-center py-4">
        <div class="container">
            <p>&copy; <?php echo date("Y"); ?> Gakero E.L.C.K High School. All rights reserved.</p>
            <p>Kisii County, Gucha Sub-county, Misesi Location</p>
            <p>Email: <a href="mailto:info@gakeroelckhigh.edu" class="text-white">info@gakeroelckhigh.edu</a> | Phone: <a href="tel:+1234567890" class="text-white">(123) 456-7890</a></p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>