<?php
$db = getDB();
$messages = $db->query("SELECT * FROM contact_messages ORDER BY created_at DESC")->fetchAll();

include __DIR__ . '/admin_header.php';
?>

<div class="admin-topbar">
    <h2>Contact Messages</h2>
</div>

<div class="admin-card">
    <h3>All Messages</h3>
    <table>
        <thead>
            <tr><th>ID</th><th>Name</th><th>Email</th><th>Subject</th><th>Message</th><th>Date</th></tr>
        </thead>
        <tbody>
        <?php foreach ($messages as $m): ?>
            <tr>
                <td><?= $m['id'] ?></td>
                <td><?= htmlspecialchars($m['name']) ?></td>
                <td><?= htmlspecialchars($m['email']) ?></td>
                <td><?= htmlspecialchars($m['subject'] ?? '') ?></td>
                <td><?= htmlspecialchars(substr($m['message'], 0, 80)) ?><?= strlen($m['message']) > 80 ? '...' : '' ?></td>
                <td><?= htmlspecialchars($m['created_at']) ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php include __DIR__ . '/admin_footer.php'; ?>
