<?php require_once 'includes/bootstrap.php'; require_once 'includes/product-card.php'; include 'includes/header.php';
$q=isset($_GET['q'])?trim($_GET['q']):''; $cat=isset($_GET['category'])?(int)$_GET['category']:0; $brand=isset($_GET['brand'])?(int)$_GET['brand']:0; $sort=isset($_GET['sort'])?$_GET['sort']:'';
$where=array('p.status=1'); if($q!==''){ $s=mysqli_real_escape_string($conn,$q); $where[]="(p.name_en LIKE '%$s%' OR p.name_ar LIKE '%$s%' OR p.description_en LIKE '%$s%' OR p.features LIKE '%$s%')"; } if($cat>0)$where[]='p.category_id='.$cat; if($brand>0)$where[]='p.brand_id='.$brand;
$order='p.id DESC'; if($sort=='price')$order='p.price ASC'; elseif($sort=='discount')$order='p.discount_percent DESC'; elseif($sort=='name')$order='p.name_en ASC';
$sql="SELECT p.*,b.name brand_name FROM products p LEFT JOIN brands b ON p.brand_id=b.id WHERE ".implode(' AND ',$where)." ORDER BY $order LIMIT 60"; $rs=mysqli_query($conn,$sql);
?>
<main class="container section"><div class="title-row"><h2><?php echo $q?('Search: '.e($q)):'Products'; ?></h2><form><input type="hidden" name="lang" value="<?php echo e($lang); ?>"><input name="q" value="<?php echo e($q); ?>" placeholder="Search"><select name="sort"><option value="">Newest</option><option value="price">Price</option><option value="discount">Discount</option><option value="name">Name</option></select><button class="primary-btn">Filter</button></form></div><div class="product-row"><?php if($rs): while($p=mysqli_fetch_assoc($rs)): render_product_card($conn,$p,$lang); endwhile; endif; ?></div></main><?php include 'includes/footer.php'; ?>
