<?php
/**
 * User Wishlist Page
 * Digital Marketplace
 */

require_once __DIR__ . '/../config/database.php';

require_login();

$page_title = 'My Wishlist';

$user_id = get_current_user_id();

// Handle remove from wishlist
if (isset($_GET['remove'])) {
    $product_id = (int)$_GET['remove'];
    remove_from_wishlist($product_id);
    set_flash_message('Product removed from wishlist.', 'success');
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

// Get wishlist items
$wishlist = get_user_wishlist($user_id);

include __DIR__ . '/../includes/header.php';
?>

<section class="section">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center mb-4" data-aos="fade-up">
            <h1><i class="fas fa-heart me-2"></i>My Wishlist</h1>
            <a href="<?php echo BASE_URL; ?>/products.php" class="btn btn-primary">
                <i class="fas fa-shopping-bag me-2"></i>Continue Shopping
            </a>
        </div>
        
        <?php if (empty($wishlist)): ?>
            <div class="card" data-aos="fade-up">
                <div class="card-body text-center py-5">
                    <i class="fas fa-heart fa-4x text-muted mb-4"></i>
                    <h3>Your wishlist is empty</h3>
                    <p class="text-muted mb-4">Save your favorite products for later.</p>
                    <a href="<?php echo BASE_URL; ?>/products.php" class="btn btn-primary">
                        <i class="fas fa-shopping-bag me-2"></i>Browse Products
                    </a>
                </div>
            </div>
        <?php else: ?>
            <div class="row g-4">
                <?php foreach ($wishlist as $product): ?>
                <div class="col-md-6 col-lg-4 col-xl-3" data-aos="fade-up">
                    <div class="product-card">
                        <div class="product-image">
                            <img src="<?php echo get_product_image_url($product); ?>" 
                                 alt="<?php echo sanitize($product['name']); ?>"
                                 onerror="this.src='<?php echo ASSETS_URL; ?>/images/no-image.jpg'">
                            
                            <?php if ($product['sale_price'] > 0 && $product['sale_price'] < $product['price']): ?>
                            <span class="product-badge sale">-<?php echo calculate_discount($product['price'], $product['sale_price']); ?>%</span>
                            <?php endif; ?>
                            
                            <?php if ($product['featured']): ?>
                            <span class="product-badge featured">Featured</span>
                            <?php endif; ?>
                            
                            <div class="product-actions">
                                <a href="<?php echo BASE_URL; ?>/product.php?slug=<?php echo $product['slug']; ?>" 
                                   class="btn btn-light btn-sm" title="View Details">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="?remove=<?php echo $product['id']; ?>" 
                                   class="btn btn-danger btn-sm" 
                                   title="Remove from Wishlist"
                                   onclick="return confirm('Remove this product from your wishlist?')">
                                    <i class="fas fa-heart-broken"></i>
                                </a>
                            </div>
                        </div>
                        
                        <div class="product-content">
                            <h5 class="product-title">
                                <a href="<?php echo BASE_URL; ?>/product.php?slug=<?php echo $product['slug']; ?>">
                                    <?php echo sanitize($product['name']); ?>
                                </a>
                            </h5>
                            
                            <div class="product-price">
                                <?php if ($product['sale_price'] > 0 && $product['sale_price'] < $product['price']): ?>
                                    <span class="sale-price"><?php echo format_price($product['sale_price']); ?></span>
                                    <span class="original-price"><?php echo format_price($product['price']); ?></span>
                                <?php else: ?>
                                    <span class="sale-price"><?php echo format_price($product['price']); ?></span>
                                <?php endif; ?>
                            </div>
                            
                            <div class="d-grid mt-3">
                                <form action="<?php echo BASE_URL; ?>/cart.php" method="POST">
                                    <input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
                                    <input type="hidden" name="quantity" value="1">
                                    <button type="submit" name="add_to_cart" class="btn btn-primary w-100">
                                        <i class="fas fa-cart-plus me-2"></i>Add to Cart
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

<?php include __DIR__ . '/../includes/footer.php'; ?>
