<?php
$pageTitle = 'My Bookings';
require_once '../config/database.php';
$extraCSS = [BASE_URL . 'assets/css/admin.css'];
require_once '../includes/header.php';

if (!isLoggedIn() || isAdmin()) redirect(BASE_URL . 'login.php');

$user = getUserById($_SESSION['user_id']);
$bookings = getUserBookings($_SESSION['user_id']);
?>

<div class="user-panel-wrapper">
    <div class="user-panel-container">
        <!-- Mobile Header -->
        <div class="user-panel-header">
            <h2>My Bookings</h2>
            <button class="user-panel-toggle" onclick="toggleUserSidebar()" aria-label="Toggle menu">
                <i class="fas fa-bars"></i>
            </button>
        </div>
        
        <!-- Sidebar Overlay -->
        <div class="user-sidebar-overlay" onclick="toggleUserSidebar()"></div>
        
        <aside class="user-sidebar">
            <button class="sidebar-close" onclick="toggleUserSidebar()" aria-label="Close menu">
                <i class="fas fa-times"></i>
            </button>
            <div class="user-avatar">
                <?php $sidebarAvatarUrl = !empty($user['profile_image']) ? getImageUrl($user['profile_image'], 'profiles') : 'https://ui-avatars.com/api/?name=' . urlencode($user['name']) . '&background=FF6B35&color=fff&size=80'; ?>
                <img src="<?= $sidebarAvatarUrl ?>" alt="<?= sanitize($user['name']) ?>">
                <h4><?= sanitize($user['name']) ?></h4>
                <p><?= sanitize($user['email']) ?></p>
            </div>
            <nav class="user-menu" role="navigation" aria-label="User menu">
                <a href="<?= USER_URL ?>"><i class="fas fa-tachometer-alt" aria-hidden="true"></i> Dashboard</a>
                <a href="<?= USER_URL ?>bookings.php" class="active" aria-current="page"><i class="fas fa-calendar-check" aria-hidden="true"></i> My Bookings</a>
                <a href="<?= USER_URL ?>transactions.php"><i class="fas fa-credit-card" aria-hidden="true"></i> Transactions</a>
                <a href="<?= USER_URL ?>settings.php"><i class="fas fa-cog" aria-hidden="true"></i> Settings</a>
                <a href="<?= BASE_URL ?>logout.php"><i class="fas fa-sign-out-alt" aria-hidden="true"></i> Logout</a>
            </nav>
        </aside>
        
        <div class="user-content">
            <h1 class="page-title">
                <i class="fas fa-calendar-check" aria-hidden="true"></i> My Bookings
            </h1>
            
            <?php foreach ($bookings as $b): ?>
                <div class="admin-card">
                    <div class="card-body">
                        <div class="booking-card">
                            <img src="<?= getImageUrl($b['package_image'], 'packages') ?>" alt="<?= sanitize($b['package_title']) ?>" class="booking-card-image">
                            <div class="booking-card-content">
                                <div class="booking-card-header">
                                    <div>
                                        <h3 class="booking-card-title">
                                            <a href="<?= BASE_URL ?>package.php?slug=<?= $b['package_slug'] ?>"><?= sanitize($b['package_title']) ?></a>
                                        </h3>
                                        <p>Booking ID: <strong><?= $b['booking_id'] ?></strong></p>
                                    </div>
                                    <div class="status-badges">
                                        <span class="status-badge status-<?= $b['status'] ?>"><?= ucfirst($b['status']) ?></span>
                                        <span class="status-badge status-<?= $b['payment_status'] ?>">Payment: <?= ucfirst($b['payment_status']) ?></span>
                                    </div>
                                </div>
                                <div class="booking-card-meta">
                                    <span><i class="fas fa-calendar" aria-hidden="true"></i> <?= formatDate($b['travel_date']) ?></span>
                                    <span><i class="fas fa-users" aria-hidden="true"></i> <?= $b['num_people'] ?> People</span>
                                    <span class="booking-card-price"><?= formatCurrency($b['final_amount']) ?></span>
                                </div>
                                <?php if ($b['payment_status'] === 'pending'): ?>
                                    <div class="booking-card-actions">
                                        <a href="<?= BASE_URL ?>payment.php?booking=<?= $b['id'] ?>" class="btn btn-sm btn-primary">
                                            <i class="fas fa-credit-card" aria-hidden="true"></i> Complete Payment
                                        </a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
            
            <?php if (empty($bookings)): ?>
                <div class="admin-card">
                    <div class="card-body">
                        <div class="empty-state">
                            <i class="fas fa-suitcase-rolling" aria-hidden="true"></i>
                            <h3>No bookings yet</h3>
                            <p>Start exploring our amazing tour packages!</p>
                            <a href="<?= BASE_URL ?>packages.php" class="btn btn-primary"><i class="fas fa-compass" aria-hidden="true"></i> Browse Packages</a>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

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