<?php
$current_page = basename($_SERVER['PHP_SELF']);

// Ensure we have access to user info if not fetched by the parent page
if (!isset($user['name'])) {
    if (isset($_SESSION['user_id'])) {
        $sidebar_user_id = $_SESSION['user_id'];
        $sidebar_query = mysqli_query($conn, "SELECT * FROM users WHERE id='$sidebar_user_id'");
        $sidebar_user = mysqli_fetch_assoc($sidebar_query);
    } else {
        $sidebar_user = ['name' => 'Guest', 'created_at' => date('Y-m-d')];
    }
} else {
    $sidebar_user = $user;
}
?>

<style>
    .profile-sidebar {
        background: white;
        border-radius: 15px;
        padding: 25px;
        box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        height: 100%;
    }
    .user-avatar {
        width: 120px;
        height: 120px;
        border-radius: 50%;
        background: linear-gradient(45deg, #ff6b35, #3498db);
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 20px;
        color: white;
        font-size: 3rem;
    }
    .nav-pills .nav-link {
        color: #2c3e50;
        padding: 12px 20px;
        border-radius: 10px;
        margin-bottom: 10px;
        transition: all 0.3s;
    }
    .nav-pills .nav-link.active {
        background: linear-gradient(45deg, #ff6b35, #ff8e53);
        color: white;
    }
    .nav-pills .nav-link:hover:not(.active) {
        background: #f8f9fa;
    }
</style>

<div class="col-md-3 mb-4">
    <div class="profile-sidebar">
        <div class="user-avatar">
            <i class="fas fa-user"></i>
        </div>
        <h4 class="text-center mb-3"><?php echo htmlspecialchars($sidebar_user['name']); ?></h4>
        <p class="text-center text-muted mb-4">Member since <?php echo date('M Y', strtotime($sidebar_user['created_at'])); ?></p>
        
        <ul class="nav nav-pills flex-column">
            <li class="nav-item">
                <a class="nav-link <?php echo $current_page == 'profile.php' ? 'active' : ''; ?>" href="profile.php">
                    <i class="fas fa-user-circle me-2"></i>Profile
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo $current_page == 'my-rentals.php' ? 'active' : ''; ?>" href="my-rentals.php">
                    <i class="fas fa-box me-2"></i>My Rentals
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo $current_page == 'wishlist.php' ? 'active' : ''; ?>" href="wishlist.php">
                    <i class="fas fa-heart me-2"></i>Wishlist
                    <?php if(isset($_SESSION['wishlist']) && count($_SESSION['wishlist']) > 0): ?>
                        <span class="badge bg-danger float-end"><?php echo count($_SESSION['wishlist']); ?></span>
                    <?php endif; ?>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo $current_page == 'addresses.php' ? 'active' : ''; ?>" href="addresses.php">
                    <i class="fas fa-map-marker-alt me-2"></i>Addresses
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link text-danger <?php echo $current_page == 'logout.php' ? 'active' : ''; ?>" href="../logout.php">
                    <i class="fas fa-sign-out-alt me-2"></i>Logout
                </a>
            </li>
        </ul>
    </div>
</div>
