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

if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $stmt = $db->prepare("DELETE FROM packages WHERE id = ?");
    $stmt->execute([$id]);
    logActivity($_SESSION['user_id'], 'package_deleted', "Deleted package ID: $id");
    setFlash('success', 'Package deleted successfully.');
    redirect(ADMIN_URL . 'packages-list.php');
}

$packages = $db->query("
    SELECT p.*, pl.name as place_name,
    (SELECT pi.image FROM package_images pi WHERE pi.package_id = p.id AND pi.is_primary = 1 LIMIT 1) as primary_image,
    (SELECT COUNT(*) FROM bookings b WHERE b.package_id = p.id) as booking_count
    FROM packages p 
    JOIN places pl ON p.place_id = pl.id 
    ORDER BY p.created_at DESC
")->fetchAll();
?>

<div class="page-header">
    <h1><i class="fas fa-box-open text-primary"></i> Packages Management</h1>
    <a href="<?= ADMIN_URL ?>package-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add New Package</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>Title</th>
                        <th>Place</th>
                        <th>Price</th>
                        <th>Duration</th>
                        <th>Bookings</th>
                        <th>Featured</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($packages as $i => $pkg): ?>
                        <tr>
                            <td><?= $i + 1 ?></td>
                            <td>
                                <img src="<?= getImageUrl($pkg['primary_image'], 'packages') ?>" alt="" style="width:70px;height:45px;object-fit:cover;border-radius:6px;">
                            </td>
                            <td><strong><?= sanitize(truncateText($pkg['title'], 35)) ?></strong></td>
                            <td><?= sanitize($pkg['place_name']) ?></td>
                            <td>
                                <?php if ($pkg['offer_price']): ?>
                                    <span style="text-decoration:line-through;color:var(--gray-light);font-size:0.8rem;"><?= formatCurrency($pkg['price_per_person']) ?></span><br>
                                    <strong style="color:var(--primary);"><?= formatCurrency($pkg['offer_price']) ?></strong>
                                <?php else: ?>
                                    <strong><?= formatCurrency($pkg['price_per_person']) ?></strong>
                                <?php endif; ?>
                            </td>
                            <td><?= $pkg['duration_days'] ?>D/<?= $pkg['duration_nights'] ?>N</td>
                            <td><?= $pkg['booking_count'] ?></td>
                            <td>
                                <?php if ($pkg['is_featured']): ?>
                                    <span style="color:var(--accent);"><i class="fas fa-star"></i></span>
                                <?php else: ?>
                                    <span style="color:var(--gray-light);">-</span>
                                <?php endif; ?>
                            </td>
                            <td><span class="status-badge status-<?= $pkg['status'] ?>"><?= ucfirst($pkg['status']) ?></span></td>
                            <td>
                                <div class="table-actions">
                                    <a href="<?= BASE_URL ?>package.php?slug=<?= $pkg['slug'] ?>" target="_blank" class="action-btn view" title="View"><i class="fas fa-eye"></i></a>
                                    <a href="<?= ADMIN_URL ?>package-edit.php?id=<?= $pkg['id'] ?>" class="action-btn edit" title="Edit"><i class="fas fa-edit"></i></a>
                                    <a href="<?= ADMIN_URL ?>packages-list.php?delete=<?= $pkg['id'] ?>" class="action-btn delete" title="Delete" data-confirm="Delete '<?= sanitize($pkg['title']) ?>'?"><i class="fas fa-trash"></i></a>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($packages)): ?>
                        <tr><td colspan="10" style="text-align:center;color:var(--gray);padding:30px;">No packages found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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