<?php
session_start();
include_once("../config/db.php");

// Security: Ensure user is an admin before performing any action.
if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit();
}

// Handle delete request
if(isset($_GET['delete'])){
    $id = $_GET['delete'];
    $stmt = mysqli_prepare($conn, "DELETE FROM contact_messages WHERE id=?");
    mysqli_stmt_bind_param($stmt, "i", $id);
    mysqli_stmt_execute($stmt);
    header("Location: booking.php"); // Redirect to prevent re-deleting on refresh
    exit();
}

include 'admin_header.php';

$result = mysqli_query($conn,"SELECT * FROM contact_messages WHERE subject = 'New Houseboat Booking Inquiry' ORDER BY id DESC");
?>

<div class="page-header">
    <h1>Booking Inquiries</h1>
</div>

<div class="content-wrapper">
    <?php if (mysqli_num_rows($result) > 0): ?>
        <div class="message-list">
            <?php while ($row = mysqli_fetch_assoc($result)): ?>
                <div class="message-card">
                    <div class="message-header">
                        <div>
                            <div class="message-sender"><?php echo htmlspecialchars($row['name']); ?></div>
                            <div class="message-subject"><?php echo htmlspecialchars($row['subject']); ?></div>
                        </div>
                        <div class="message-date"><?php echo date("M d, Y, g:i A", strtotime($row['created_at'])); ?></div>
                    </div>
                    <div class="message-body">
                        <p><?php echo isset($row['message']) ? nl2br(htmlspecialchars($row['message'])) : 'No message content.'; ?></p>
                    </div>
                    <div class="message-footer">
                        <div class="message-contact">
                            <?php if(!empty($row['mobile'])): ?>
                                <span><i class="fas fa-phone"></i> <?php echo htmlspecialchars($row['mobile']); ?></span>
                            <?php endif; ?>
                        </div>
                        <div class="message-actions">
                            <a href="?delete=<?php echo $row['id']; ?>" class="delete" onclick="return confirm('Are you sure you want to delete this inquiry?');">
                                <i class="fas fa-trash"></i> Delete
                            </a>
                        </div>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    <?php else: ?>
        <div class="message-card" style="padding: 40px; text-align: center; color: var(--content-text-secondary);">
            <i class="fas fa-calendar-check" style="font-size: 48px; margin-bottom: 15px; opacity: 0.3;"></i>
            <p>No new booking inquiries found.</p>
        </div>
    <?php endif; ?>
</div>

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