<?php
// tags.php - Browse Products by Tag
// File location: /bidwheels.net/product/tags.php

require_once '../includes/functions.php';
require_once '../includes/header.php';

$tag = isset($_GET['tag']) ? sanitize($_GET['tag']) : '';

if (empty($tag)) {
    redirect('/product/index.php');
}

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * PRODUCTS_PER_PAGE;

// Get products with this tag
$products = fetchAll("
    SELECT p.*, c.name as category_name,
           (SELECT COUNT(*) FROM bids WHERE product_id = p.id) as total_bids,
           (SELECT MAX(amount) FROM bids WHERE product_id = p.id) as highest_bid
    FROM products p
    LEFT JOIN categories c ON p.category_id = c.id
    WHERE p.is_reserved = 0 
    AND p.end_time > NOW()
    AND p.tags LIKE '%$tag%'
    ORDER BY p.end_time ASC
    LIMIT $offset, " . PRODUCTS_PER_PAGE
);

$total = getCount('products', "tags LIKE '%$tag%' AND is_reserved = 0 AND end_time > NOW()");
$total_pages = ceil($total / PRODUCTS_PER_PAGE);

// Get popular tags
$all_products = fetchAll("SELECT tags FROM products WHERE tags IS NOT NULL AND tags != ''");
$tag_counts = [];
foreach ($all_products as $p) {
    $tags = explode(',', $p['tags']);
    foreach ($tags as $t) {
        $t = trim($t);
        if (!empty($t)) {
            $tag_counts[$t] = ($tag_counts[$t] ?? 0) + 1;
        }
    }
}
arsort($tag_counts);
$popular_tags = array_slice($tag_counts, 0, 15, true);
?>

<div class="container">
    <div class="tags-page" style="padding: 40px 0;">
        <div class="breadcrumb" style="margin-bottom: 20px;">
            <a href="<?php echo SITE_URL; ?>/index.php">Home</a> / 
            <a href="index.php">Products</a> / 
            <span>Tag: <?php echo htmlspecialchars($tag); ?></span>
        </div>
        
        <h1 style="margin-bottom: 20px;">Products tagged with: <span style="color: #e63946;">#<?php echo htmlspecialchars($tag); ?></span></h1>
        <p style="margin-bottom: 30px; color: #666;"><?php echo $total; ?> products found</p>
        
        <div class="products-layout" style="display: grid; grid-template-columns: 280px 1fr; gap: 30px;">
            <!-- Sidebar with Popular Tags -->
            <aside class="products-sidebar">
                <div class="sidebar-widget" style="background: white; padding: 20px; border-radius: 10px;">
                    <h3 style="margin-bottom: 15px;">Popular Tags</h3>
                    <div class="tag-cloud">
                        <?php foreach ($popular_tags as $t => $count): ?>
                            <a href="tags.php?tag=<?php echo urlencode($t); ?>" 
                               style="display: inline-block; background: <?php echo $t == $tag ? '#e63946' : '#f0f0f0'; ?>; color: <?php echo $t == $tag ? 'white' : '#666'; ?>; padding: 5px 12px; margin: 0 5px 10px 0; border-radius: 20px; text-decoration: none; font-size: 12px;">
                                #<?php echo htmlspecialchars($t); ?> (<?php echo $count; ?>)
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
            </aside>
            
            <!-- Products Grid -->
            <div class="products-content">
                <?php if (count($products) > 0): ?>
                    <div class="products-grid" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 25px;">
                        <?php foreach ($products as $product): ?>
                            <div class="product-card" style="background: white; border-radius: 10px; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
                                <div class="product-image" style="position: relative; height: 200px;">
                                    <img src="<?php echo getFeaturedImage($product['id']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>" style="width: 100%; height: 100%; object-fit: cover;">
                                    <div class="bid-count" style="position: absolute; bottom: 10px; left: 10px; background: rgba(0,0,0,0.7); color: white; padding: 5px 10px; border-radius: 5px; font-size: 12px;">
                                        <i class="fas fa-gavel"></i> <?php echo $product['total_bids']; ?> bids
                                    </div>
                                </div>
                                <div class="product-info" style="padding: 15px;">
                                    <h3 style="margin-bottom: 10px;"><?php echo htmlspecialchars($product['name']); ?></h3>
                                    <div class="price-info" style="display: flex; justify-content: space-between; margin-bottom: 10px;">
                                        <span class="current-bid" style="font-size: 20px; font-weight: bold; color: #e63946;">
                                            <?php echo formatPrice($product['highest_bid'] ?? $product['starting_price']); ?>
                                        </span>
                                    </div>
                                    <div class="time-left" style="color: #666; font-size: 13px; margin-bottom: 15px;">
                                        <i class="fas fa-clock"></i> <?php echo getRemainingTime($product['end_time']); ?>
                                    </div>
                                    <a href="detail.php?id=<?php echo $product['id']; ?>" class="btn-bid" style="display: block; text-align: center; background: #1a1a2e; color: white; padding: 10px; text-decoration: none; border-radius: 5px;">
                                        Place Bid <i class="fas fa-arrow-right"></i>
                                    </a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                    
                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                        <div class="pagination" style="display: flex; justify-content: center; gap: 10px; margin-top: 40px;">
                            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                <a href="?tag=<?php echo urlencode($tag); ?>&page=<?php echo $i; ?>" 
                                   style="padding: 8px 15px; background: <?php echo $i == $page ? '#e63946' : 'white'; ?>; color: <?php echo $i == $page ? 'white' : '#333'; ?>; border: 1px solid #ddd; text-decoration: none; border-radius: 5px;">
                                    <?php echo $i; ?>
                                </a>
                            <?php endfor; ?>
                        </div>
                    <?php endif; ?>
                    
                <?php else: ?>
                    <div class="no-products" style="text-align: center; padding: 60px; background: white; border-radius: 10px;">
                        <i class="fas fa-tag" style="font-size: 60px; color: #ccc; margin-bottom: 20px;"></i>
                        <h3>No products found with tag #<?php echo htmlspecialchars($tag); ?></h3>
                        <a href="index.php" class="btn btn-primary" style="display: inline-block; margin-top: 15px;">Browse All Products</a>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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