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

// Handle delete
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $stmt = $db->prepare("DELETE FROM places WHERE id = ?");
    $stmt->execute([$id]);
    logActivity($_SESSION['user_id'], 'place_deleted', "Deleted place ID: $id");
    setFlash('success', 'Place deleted successfully.');
    redirect(ADMIN_URL . 'places.php');
}

$places = $db->query("SELECT p.*, (SELECT COUNT(*) FROM packages pk WHERE pk.place_id = p.id) as package_count FROM places p ORDER BY p.created_at DESC")->fetchAll();
?>

<div class="page-header">
    <h1><i class="fas fa-map-marker-alt text-primary"></i> Places Management</h1>
    <a href="<?= ADMIN_URL ?>place-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add New Place</a>
</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>Image</th>
                        <th>Name</th>
                        <th>Packages</th>
                        <th>Featured</th>
                        <th>Status</th>
                        <th>Created</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($places as $i => $place): ?>
                        <tr>
                            <td><?= $i + 1 ?></td>
                            <td>
                                <img src="<?= getImageUrl($place['image'], 'places') ?>" alt="<?= sanitize($place['name']) ?>" style="width:60px;height:40px;object-fit:cover;border-radius:6px;">
                            </td>
                            <td><strong><?= sanitize($place['name']) ?></strong></td>
                            <td><?= $place['package_count'] ?></td>
                            <td>
                                <?php if ($place['is_featured']): ?>
                                    <span style="color:var(--accent);"><i class="fas fa-star"></i> Yes</span>
                                <?php else: ?>
                                    <span style="color:var(--gray-light);">No</span>
                                <?php endif; ?>
                            </td>
                            <td><span class="status-badge status-<?= $place['status'] ?>"><?= ucfirst($place['status']) ?></span></td>
                            <td><?= formatDate($place['created_at']) ?></td>
                            <td>
                                <div class="table-actions">
                                    <a href="<?= ADMIN_URL ?>place-edit.php?id=<?= $place['id'] ?>" class="action-btn edit" title="Edit"><i class="fas fa-edit"></i></a>
                                    <a href="<?= ADMIN_URL ?>places.php?delete=<?= $place['id'] ?>" class="action-btn delete" title="Delete" data-confirm="Are you sure you want to delete '<?= sanitize($place['name']) ?>'?"><i class="fas fa-trash"></i></a>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($places)): ?>
                        <tr><td colspan="8" style="text-align:center;color:var(--gray);padding:30px;">No places found. <a href="<?= ADMIN_URL ?>place-add.php">Add one now</a></td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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