<?php
$pageTitle = 'Enquiries';
require_once 'includes/header.php';

if (isset($_GET['status']) && isset($_GET['id'])) {
    $stmt = $db->prepare("UPDATE enquiries SET status = ? WHERE id = ?");
    $stmt->execute([sanitize($_GET['status']), intval($_GET['id'])]);
    setFlash('success', 'Enquiry status updated.');
    redirect(ADMIN_URL . 'enquiries.php');
}

if (isset($_GET['delete'])) {
    $db->prepare("DELETE FROM enquiries WHERE id = ?")->execute([intval($_GET['delete'])]);
    setFlash('success', 'Enquiry deleted.');
    redirect(ADMIN_URL . 'enquiries.php');
}

$enquiries = $db->query("
    SELECT e.*, p.title as package_title 
    FROM enquiries e 
    LEFT JOIN packages p ON e.package_id = p.id 
    ORDER BY e.created_at DESC
")->fetchAll();
?>

<div class="page-header">
    <h1><i class="fas fa-question-circle text-primary"></i> Enquiries</h1>
</div>

<div class="admin-card">
    <div class="card-body" style="padding:0;">
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Package</th>
                        <th>Message</th>
                        <th>Status</th>
                        <th>Date</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($enquiries as $i => $e): ?>
                        <tr>
                            <td><?= $i + 1 ?></td>
                            <td><strong><?= sanitize($e['name']) ?></strong></td>
                            <td><?= sanitize($e['email']) ?></td>
                            <td><?= sanitize($e['phone'] ?: '-') ?></td>
                            <td><?= $e['package_title'] ? sanitize(truncateText($e['package_title'], 20)) : '-' ?></td>
                            <td title="<?= sanitize($e['message']) ?>"><?= sanitize(truncateText($e['message'], 40)) ?></td>
                            <td><span class="status-badge status-<?= $e['status'] ?>"><?= ucfirst($e['status']) ?></span></td>
                            <td><?= formatDate($e['created_at']) ?></td>
                            <td>
                                <div class="table-actions">
                                    <?php if ($e['status'] === 'new'): ?>
                                        <a href="?status=read&id=<?= $e['id'] ?>" class="action-btn view" title="Mark Read"><i class="fas fa-eye"></i></a>
                                    <?php endif; ?>
                                    <a href="?status=replied&id=<?= $e['id'] ?>" class="action-btn edit" title="Mark Replied"><i class="fas fa-reply"></i></a>
                                    <a href="?delete=<?= $e['id'] ?>" class="action-btn delete" title="Delete" data-confirm="Delete this enquiry?"><i class="fas fa-trash"></i></a>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($enquiries)): ?>
                        <tr><td colspan="9" style="text-align:center;color:var(--gray);padding:30px;">No enquiries found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
