<?php
require_once __DIR__ . '/../functions.php';
ensure_role(['Admin', 'Manager']);
$messages = pdo()->query('SELECT * FROM contact_messages ORDER BY created_at DESC')->fetchAll();
?>
<?php include __DIR__ . '/includes/header.php'; ?>
    <section class="panel">
        <header><h2>Contact Messages</h2></header>
        <div class="table-scroll">
            <table>
                <thead><tr><th>Name</th><th>Email</th><th>Subject</th><th>Status</th><th>Received</th><th>Action</th></tr></thead>
                <tbody>
                <?php foreach ($messages as $message): ?>
                    <tr>
                        <td><?= clean($message['name']) ?></td>
                        <td><?= clean($message['email']) ?></td>
                        <td><?= clean($message['subject']) ?></td>
                        <td><?= clean($message['status']) ?></td>
                        <td><?= clean($message['created_at']) ?></td>
                        <td>
                            <select onchange="updateStatus('update_message_status', <?= $message['id'] ?>, this.value)">
                                <option value="Unread" <?= $message['status'] === 'Unread' ? 'selected' : '' ?>>Unread</option>
                                <option value="Read" <?= $message['status'] === 'Read' ? 'selected' : '' ?>>Read</option>
                            </select>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
<?php include __DIR__ . '/includes/footer.php'; ?>
