<?php
require_once __DIR__ . '/../config.php';
$db = getDB();
$cat_slug = trim($_GET['cat'] ?? '');
$search   = trim($_GET['q'] ?? '');

$where = ["p.status='active'"];
$params = [];
if ($cat_slug) {
    $where[] = "c.slug = ?"; $params[] = $cat_slug;
}
if ($search) {
    $where[] = "(p.name LIKE ? OR p.short_desc LIKE ?)"; $params[] = "%$search%"; $params[] = "%$search%";
}
$whereStr = 'WHERE ' . implode(' AND ', $where);
$stmt = $db->prepare("SELECT p.*, c.name AS cat_name, c.slug AS cat_slug FROM products p LEFT JOIN categories c ON p.category_id=c.id $whereStr ORDER BY p.is_featured DESC, p.id ASC");
$stmt->execute($params);
$products = $stmt->fetchAll();

$categories = $db->query("SELECT * FROM categories ORDER BY id")->fetchAll();
$active_cat = $cat_slug ? $db->prepare("SELECT * FROM categories WHERE slug=?") : null;
if ($active_cat) { $active_cat->execute([$cat_slug]); $active_cat = $active_cat->fetch(); }

$page_title = ($active_cat ? h($active_cat['name']) . ' – ' : '') . 'Products | Balimeen Private Limited';
include __DIR__ . '/../includes/header.php';
?>

<div class="page-hero">
  <div class="container">
    <h1><?= $active_cat ? h($active_cat['name']) : 'All Products' ?></h1>
    <p><?= $active_cat ? h($active_cat['description']) : 'Browse our complete range of premium stainless steel kitchen sinks.' ?></p>
    <div class="breadcrumb"><a href="<?= SITE_URL ?>">Home</a> → Products<?= $active_cat ? ' → '.h($active_cat['name']) : '' ?></div>
  </div>
</div>

<section class="products-section section-pad">
  <div class="container">
    <!-- Search -->
    <form method="GET" style="margin-bottom:24px;display:flex;gap:10px;max-width:480px;">
      <input type="text" name="q" value="<?= h($search) ?>" placeholder="🔍 Search products..." style="flex:1;padding:10px 14px;border:1.5px solid #e2e8f0;border-radius:8px;font-family:inherit;font-size:.9rem;">
      <?php if ($cat_slug): ?><input type="hidden" name="cat" value="<?= h($cat_slug) ?>"><?php endif; ?>
      <button type="submit" class="btn btn-primary btn-sm">Search</button>
    </form>

    <!-- Filters -->
    <div class="products-filter">
      <a href="products.php" class="filter-btn <?= !$cat_slug ? 'active' : '' ?>">All Sinks</a>
      <?php foreach ($categories as $c): ?>
      <a href="products.php?cat=<?= h($c['slug']) ?>" class="filter-btn <?= $cat_slug === $c['slug'] ? 'active' : '' ?>"><?= h($c['name']) ?></a>
      <?php endforeach; ?>
    </div>

    <?php if ($products): ?>
    <p style="color:var(--text-light);margin-bottom:20px;font-size:.88rem;"><?= count($products) ?> product(s) found</p>
    <div class="products-grid">
      <?php foreach ($products as $p): ?>
      <div class="product-card" onclick="window.location='product.php?slug=<?= h($p['slug']) ?>'">
        <div class="product-card-img">
          <img src="<?= SITE_URL ?>/assets/images/<?= h($p['image']) ?>" alt="<?= h($p['name']) ?>" loading="lazy">
          <?php if ($p['is_featured']): ?><span class="product-badge">⭐ Featured</span><?php endif; ?>
        </div>
        <div class="product-card-body">
          <h3><?= h($p['name']) ?></h3>
          <p><?= h($p['short_desc']) ?></p>
          <div style="font-size:.78rem;color:var(--text-light);margin-bottom:10px;">
            📐 <?= h($p['dimensions']) ?> &nbsp;|&nbsp; ⚙️ <?= h($p['material']) ?>
          </div>
          <div class="product-card-meta">
            <div class="product-price">₹<?= number_format($p['price']) ?> <span>onwards</span></div>
            <a href="product.php?slug=<?= h($p['slug']) ?>" class="btn btn-primary btn-sm" onclick="event.stopPropagation()">View →</a>
          </div>
        </div>
      </div>
      <?php endforeach; ?>
    </div>
    <?php else: ?>
    <div style="text-align:center;padding:60px 20px;">
      <div style="font-size:3rem;margin-bottom:16px;">🔍</div>
      <h3 style="color:var(--blue-dark);margin-bottom:8px;">No products found</h3>
      <p style="color:var(--text-light);">Try a different search or category.</p>
      <a href="products.php" class="btn btn-primary" style="margin-top:16px;">View All Products</a>
    </div>
    <?php endif; ?>
  </div>
</section>

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