<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Include config first
require_once "config/db.php";

// Get categories (exclude Accessories and Kids Wear)
$categories = mysqli_query($conn, "SELECT * FROM categories WHERE category_name NOT IN ('Accessories','Kids Wear') ORDER BY category_name");

// Build WHERE clause for filters
$where_conditions = ["p.stock > 0"];
$params = [];

// Category filter
if (isset($_GET['cat']) && !empty($_GET['cat'])) {
    $cat_id = mysqli_real_escape_string($conn, $_GET['cat']);
    $where_conditions[] = "p.category_id = '$cat_id'";
    $params['cat'] = $cat_id;
}

// Build final WHERE clause
$where_clause = !empty($where_conditions) ? "WHERE " . implode(" AND ", $where_conditions) : "";

// Get products
$result = mysqli_query($conn, 
    "SELECT p.*, c.category_name 
     FROM products p 
     LEFT JOIN categories c ON p.category_id = c.id 
     $where_clause 
     ORDER BY p.created_at DESC"
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rent Clothes - ShopNest</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="assets/css/style.css" rel="stylesheet">
    <style>
        .btn-orange {
            background: linear-gradient(45deg, #ff6b35, #ff8e53);
            border: none;
            color: white;
            padding: 8px 20px;
            border-radius: 20px;
            font-weight: 600;
        }
        .btn-orange:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(255, 107, 53, 0.3);
            color: white;
        }
        .card {
            border: none;
            border-radius: 15px;
            overflow: hidden;
            transition: all 0.3s;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.1);
        }
        .product-image {
            height: 250px;
            object-fit: cover;
        }
        .category-badge {
            position: absolute;
            top: 10px;
            left: 10px;
            background: rgba(0,0,0,0.7);
            color: white;
            padding: 5px 10px;
            border-radius: 5px;
            font-size: 0.8rem;
        }
    </style>
</head>
<body>
    <?php include "includes/header.php"; ?>  <!-- Fixed path -->

    <div class="container mt-4">
        <h2 class="mb-4">
            <i class="fas fa-tshirt me-2"></i>
            <?php if(isset($_GET['cat'])): 
                $cat_query = mysqli_query($conn, "SELECT category_name FROM categories WHERE id='$cat_id'");
                $cat_name = mysqli_fetch_assoc($cat_query);
            ?>
                Rent <?php echo htmlspecialchars($cat_name['category_name']); ?>
            <?php else: ?>
                Rent Designer Clothes
            <?php endif; ?>
        </h2>

        <!-- Categories Filter -->
        <div class="mb-4">
            <a href="products.php" class="btn btn-outline-primary mb-2">
                <i class="fas fa-th me-2"></i>All Categories
            </a>
            <?php mysqli_data_seek($categories, 0); ?>
            <?php while($cat = mysqli_fetch_assoc($categories)): ?>
                <a href="products.php?cat=<?php echo $cat['id']; ?>" class="btn btn-outline-primary mb-2">
                    <i class="fas fa-tshirt me-2"></i><?php echo htmlspecialchars($cat['category_name']); ?>
                </a>
            <?php endwhile; ?>
        </div>

        <!-- Products Grid -->
        <div class="row">
            <?php if(mysqli_num_rows($result) > 0): ?>
                <?php while($row = mysqli_fetch_assoc($result)): ?>
                <div class="col-lg-3 col-md-4 col-sm-6 mb-4">
                    <div class="card h-100">
                        <div class="position-relative">
                               <?php
                               $pimg_file = '';
                               if (!empty($row['images'])) {
                                   $decoded = json_decode($row['images'], true);
                                   $pimg_file = is_array($decoded) ? ($decoded[0] ?? '') : $row['images'];
                               }
                               $pimg_src = !empty($pimg_file) ? 'assets/images/products/' . $pimg_file : 'assets/images/products/placeholder.jpg';
                               ?>
                               <div class="product-image-container" style="height: 200px; background: #f8f9fa; display: flex; align-items: center; justify-content: center; overflow: hidden;">
                                   <img src="<?php echo $pimg_src; ?>" 
                                       class="card-img-top" 
                                       alt="<?php echo htmlspecialchars($row['name']); ?>"
                                       style="height: 100%; width: 100%; object-fit: cover;"
                                       onerror="this.src='assets/images/products/placeholder.jpg'">
                               </div>
                            <span class="category-badge"><?php echo htmlspecialchars($row['category_name']); ?></span>
                        </div>
                        
                        <div class="card-body">
                            <h6 class="card-title"><?php echo htmlspecialchars($row['name']); ?></h6>
                            <p class="card-text small text-muted mb-2">
                                <i class="fas fa-ruler-vertical me-1"></i>Size: <?php echo $row['size']; ?>
                                <br>
                                <i class="fas fa-palette me-1"></i>Color: <?php echo $row['color']; ?>
                            </p>
                            <div class="price-info small mb-2">
                                <?php
                                $rent_price = floatval($row['rental_price'] > 0 ? $row['rental_price'] : $row['price']);
                                $disc_percent = intval($row['discount_percent']);
                                $final_rent = floatval($row['price']);
                                ?>
                                <div class="d-flex justify-content-between align-items-center mb-1">
                                    <strong class="text-muted">Base Rental:</strong> 
                                    <span>₹<?php echo number_format($rent_price, 2); ?>/day</span>
                                </div>
                                <?php if ($disc_percent > 0): ?>
                                    <div class="d-flex justify-content-between align-items-center mb-1">
                                        <strong class="text-warning">Discount:</strong> 
                                        <span class="badge bg-warning text-dark"><?php echo $disc_percent; ?>% OFF</span>
                                    </div>
                                <?php endif; ?>
                            </div>
                            <div class="d-flex justify-content-between align-items-center mb-2">
                                <span class="text-success small fw-bold">FINAL PRICE</span>
                                <strong style="font-size: 1.3rem; color: #28a745;">₹<?php echo number_format($final_rent, 2); ?>/day</strong>
                            </div>
                        </div>
                        
                        <div class="card-footer bg-white">
                            <div class="d-flex justify-content-between">
                                <a href="product-details.php?slug=<?php echo $row['slug']; ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-eye me-1"></i>Details
                                </a>
                                <form method="POST" action="cart.php">
                                    <input type="hidden" name="product_id" value="<?php echo $row['id']; ?>">
                                    <input type="hidden" name="product_name" value="<?php echo htmlspecialchars($row['name']); ?>">
                                    <input type="hidden" name="product_price" value="<?php echo $row['price']; ?>">
                                    <input type="hidden" name="product_image" value="<?php echo $row['images']; ?>">
                                    <button class="btn btn-sm btn-orange" name="add_to_cart">
                                        <i class="fas fa-cart-plus me-1"></i>Add to Cart
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endwhile; ?>
            <?php else: ?>
                <div class="col-12 text-center py-5">
                    <i class="fas fa-tshirt fa-4x mb-3" style="color: #ddd;"></i>
                    <h4>No outfits found</h4>
                    <p class="text-muted">Try selecting a different category</p>
                    <a href="products.php" class="btn btn-orange">View All</a>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <?php include "includes/footer.php"; ?>  <!-- Fixed path -->