<?php
$pageTitle = 'My Dashboard';
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']);
$recentBookings = array_slice($bookings, 0, 5);

// Stats
$totalBookings = count($bookings);
$confirmedBookings = count(array_filter($bookings, fn($b) => $b['status'] === 'confirmed'));
$pendingBookings = count(array_filter($bookings, fn($b) => $b['status'] === 'pending'));
$totalSpent = array_sum(array_map(fn($b) => $b['payment_status'] === 'paid' ? $b['final_amount'] : 0, $bookings));
?>

<div class="user-panel-wrapper">
    <div class="user-panel-container">
        <!-- Mobile Header -->
        <div class="user-panel-header">
            <h2>My Dashboard</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>
        
        <!-- Sidebar -->
        <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 ?>" class="active" aria-current="page"><i class="fas fa-tachometer-alt" aria-hidden="true"></i> Dashboard</a>
                <a href="<?= USER_URL ?>bookings.php"><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>
        
        <!-- Content -->
        <div class="user-content">
            <h1 class="page-title">
                Welcome back, <?= sanitize($user['name']) ?>! 👋
            </h1>
            
            <!-- Stats -->
            <div class="stats-grid">
                <div class="stat-card">
                    <div class="stat-icon blue"><i class="fas fa-suitcase" aria-hidden="true"></i></div>
                    <div class="stat-info">
                        <h3><?= $totalBookings ?></h3>
                        <p>Total Bookings</p>
                    </div>
                </div>
                <div class="stat-card">
                    <div class="stat-icon green"><i class="fas fa-check-circle" aria-hidden="true"></i></div>
                    <div class="stat-info">
                        <h3><?= $confirmedBookings ?></h3>
                        <p>Confirmed</p>
                    </div>
                </div>
                <div class="stat-card">
                    <div class="stat-icon yellow"><i class="fas fa-clock" aria-hidden="true"></i></div>
                    <div class="stat-info">
                        <h3><?= $pendingBookings ?></h3>
                        <p>Pending</p>
                    </div>
                </div>
                <div class="stat-card">
                    <div class="stat-icon orange"><i class="fas fa-rupee-sign" aria-hidden="true"></i></div>
                    <div class="stat-info">
                        <h3><?= formatCurrency($totalSpent) ?></h3>
                        <p>Total Spent</p>
                    </div>
                </div>
            </div>
            
            <!-- Quick Actions -->
            <div class="admin-card">
                <div class="card-header"><h3>Quick Actions</h3></div>
                <div class="card-body">
                    <div class="quick-actions">
                        <a href="<?= BASE_URL ?>packages.php" class="btn btn-primary"><i class="fas fa-compass" aria-hidden="true"></i> Browse Packages</a>
                        <a href="<?= USER_URL ?>bookings.php" class="btn btn-outline"><i class="fas fa-calendar-check" aria-hidden="true"></i> View Bookings</a>
                        <a href="<?= USER_URL ?>settings.php" class="btn btn-outline"><i class="fas fa-user-edit" aria-hidden="true"></i> Edit Profile</a>
                    </div>
                </div>
            </div>
            
            <!-- Recent Bookings -->
            <div class="admin-card">
                <div class="card-header">
                    <h3>Recent Bookings</h3>
                    <a href="<?= USER_URL ?>bookings.php" class="btn btn-sm btn-outline">View All</a>
                </div>
                <div class="card-body" style="padding:0;">
                    <div class="table-responsive">
                        <table class="data-table" role="table" aria-label="Recent bookings">
                            <thead>
                                <tr>
                                    <th scope="col">Booking ID</th>
                                    <th scope="col">Package</th>
                                    <th scope="col">Travel Date</th>
                                    <th scope="col">Amount</th>
                                    <th scope="col">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($recentBookings as $b): ?>
                                    <tr>
                                        <td><strong><?= $b['booking_id'] ?></strong></td>
                                        <td><?= sanitize(truncateText($b['package_title'], 30)) ?></td>
                                        <td><?= formatDate($b['travel_date']) ?></td>
                                        <td><?= formatCurrency($b['final_amount']) ?></td>
                                        <td><span class="status-badge status-<?= $b['status'] ?>"><?= ucfirst($b['status']) ?></span></td>
                                    </tr>
                                <?php endforeach; ?>
                                <?php if (empty($recentBookings)): ?>
                                    <tr><td colspan="5" style="text-align:center;color:var(--gray);padding:30px;">No bookings yet. <a href="<?= BASE_URL ?>packages.php" style="color:var(--primary);">Browse packages</a></td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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