<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\Database;
$q=trim((string)($_GET['q']??'')); $category=trim((string)($_GET['category']??'')); $brand=trim((string)($_GET['brand']??'')); $shop=trim((string)($_GET['shop']??''));
$where=['p.status="published"']; $params=[];
if($q!==''){ $where[]='(p.name LIKE ? OR p.sku LIKE ? OR p.barcode LIKE ? OR v.shop_name LIKE ? OR b.name LIKE ?)'; for($i=0;$i<5;$i++)$params[]='%'.$q.'%'; }
if($category!==''){ $where[]='c.slug=?'; $params[]=$category; }
if($brand!==''){ $where[]='b.slug=?'; $params[]=$brand; }
if($shop!==''){ $where[]='v.slug=?'; $params[]=$shop; }
$sql='SELECT p.*,v.shop_name,v.slug vendor_slug,b.name brand_name,b.slug brand_slug,c.name category_name,c.slug category_slug FROM products p LEFT JOIN vendors v ON v.id=p.vendor_id LEFT JOIN brands b ON b.id=p.brand_id LEFT JOIN categories c ON c.id=p.category_id WHERE '.implode(' AND ',$where).' ORDER BY p.is_featured DESC,p.id DESC LIMIT 100';
$products=Database::safeFetchAll($sql,$params); if(!$products&&!Database::isConnected()&&$q===''&&$category===''&&$brand===''&&$shop==='')$products=sample_products();
$pageTitle='Products | '.BRAND_NAME; include ROOT_PATH.'/app/Layouts/frontend_header.php';
?>
<section class="container py-5"><div class="d-flex justify-content-between align-items-end mb-4"><div><h1 class="section-title">Products</h1><p class="text-muted mb-0"><?= $q!==''?'Search results for “'.e($q).'”':'Browse available marketplace products.' ?></p></div><a class="btn btn-outline-primary" href="<?= url('public/cart.php') ?>">View Cart</a></div><div class="row g-4">
<?php if(!$products): ?><div class="col-12"><div class="alert alert-info">No matching products found.</div></div><?php endif; ?>
<?php foreach($products as $p): ?><div class="col-sm-6 col-lg-3"><div class="product-card h-100"><a href="<?= url('product/'.$p['slug']) ?>"><img class="w-100" src="<?= e(product_image($p)) ?>" alt="<?= e($p['name']) ?>"></a><div class="p-3"><small class="text-muted"><?= e($p['category_name']??$p['sku']) ?></small><h6><a class="text-dark text-decoration-none" href="<?= url('product/'.$p['slug']) ?>"><?= e($p['name']) ?></a></h6><div class="price"><?= money($p['sale_price']?:$p['regular_price']) ?></div><small class="text-muted">Stock: <?= (int)$p['stock'] ?></small><div class="d-grid gap-2 mt-3"><button class="btn btn-primary btn-sm" <?= (int)$p['stock']<1?'disabled':'' ?> onclick="phAddCart(<?= (int)$p['id'] ?>,1)"><?= (int)$p['stock']<1?'Out of stock':'Add to cart' ?></button><?php if(current_user()): ?><div class="btn-group"><button class="btn btn-outline-secondary btn-sm" onclick="phToggleList('wishlist',<?= (int)$p['id'] ?>,this,false)"><i class="bi bi-heart"></i> Save</button><button class="btn btn-outline-secondary btn-sm" onclick="phToggleList('compare',<?= (int)$p['id'] ?>,this,false)">Compare</button></div><?php endif; ?></div></div></div></div><?php endforeach; ?>
</div></section><?php include ROOT_PATH.'/app/Layouts/frontend_footer.php'; ?>
