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

// Handle status update
if (isset($_GET['action']) && isset($_GET['id'])) {
    $bookingId = intval($_GET['id']);
    $action = sanitize($_GET['action']);
    $validStatuses = ['confirmed', 'cancelled', 'completed', 'pending'];
    if (in_array($action, $validStatuses)) {
        $stmt = $db->prepare("UPDATE bookings SET status = ? WHERE id = ?");
        $stmt->execute([$action, $bookingId]);
        logActivity($_SESSION['user_id'], 'booking_updated', "Updated booking #$bookingId to $action");
        setFlash('success', 'Booking status updated.');
        redirect(ADMIN_URL . 'bookings.php');
    }
}

$bookings = $db->query("
    SELECT b.*, u.name as user_name, u.email as user_email, u.phone as user_phone, p.title as package_title
    FROM bookings b 
    JOIN users u ON b.user_id = u.id 
    JOIN packages p ON b.package_id = p.id 
    ORDER BY b.created_at DESC
")->fetchAll();
?>

<div class="page-header">
    <h1><i class="fas fa-calendar-check text-primary"></i> Bookings Management</h1>
</div>

<div class="admin-card">
    <div class="card-body" style="padding:0;">
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Booking ID</th>
                        <th>Customer</th>
                        <th>Package</th>
                        <th>Travel Date</th>
                        <th>People</th>
                        <th>Amount</th>
                        <th>Payment</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($bookings as $b): ?>
                        <tr>
                            <td><strong><?= $b['booking_id'] ?></strong></td>
                            <td>
                                <strong><?= sanitize($b['user_name']) ?></strong><br>
                                <small style="color:var(--gray);"><?= sanitize($b['user_email']) ?></small>
                            </td>
                            <td><?= sanitize(truncateText($b['package_title'], 25)) ?></td>
                            <td><?= formatDate($b['travel_date']) ?></td>
                            <td><?= $b['num_people'] ?></td>
                            <td><strong><?= formatCurrency($b['final_amount']) ?></strong></td>
                            <td><span class="status-badge status-<?= $b['payment_status'] ?>"><?= ucfirst($b['payment_status']) ?></span></td>
                            <td><span class="status-badge status-<?= $b['status'] ?>"><?= ucfirst($b['status']) ?></span></td>
                            <td>
                                <div class="table-actions">
                                    <?php if ($b['status'] === 'pending'): ?>
                                        <a href="?action=confirmed&id=<?= $b['id'] ?>" class="action-btn view" title="Confirm"><i class="fas fa-check"></i></a>
                                        <a href="?action=cancelled&id=<?= $b['id'] ?>" class="action-btn delete" title="Cancel" data-confirm="Cancel this booking?"><i class="fas fa-times"></i></a>
                                    <?php elseif ($b['status'] === 'confirmed'): ?>
                                        <a href="?action=completed&id=<?= $b['id'] ?>" class="action-btn view" title="Complete"><i class="fas fa-flag-checkered"></i></a>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($bookings)): ?>
                        <tr><td colspan="9" style="text-align:center;color:var(--gray);padding:30px;">No bookings found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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