<?php
require_once __DIR__.'/config.php';
$pageTitle='Explore properties';
$pdo=db();
$q=trim((string)($_GET['q']??'')); $stype=trim((string)($_GET['stype']??'')); $city=trim((string)($_GET['city']??'')); $type=trim((string)($_GET['type']??''));
$where=["status='available'"]; $params=[];
if($q!==''){ $where[]='(title LIKE :q OR location LIKE :q OR city LIKE :q OR state LIKE :q OR pcontent LIKE :q)'; $params['q']='%'.$q.'%'; }
if($stype!==''){ $where[]='LOWER(stype)=LOWER(:stype)'; $params['stype']=$stype; }
if($city!==''){ $where[]='city=:city'; $params['city']=$city; }
if($type!==''){ $where[]='type=:type'; $params['type']=$type; }
$sql='SELECT * FROM property WHERE '.implode(' AND ',$where).' ORDER BY isFeatured DESC,pid DESC'; $stmt=$pdo->prepare($sql); $stmt->execute($params); $rows=$stmt->fetchAll();
$cities=cities_from_properties(); $types=property_types();
include __DIR__.'/includes/site-header.php';
?>
<section class="page-hero"><div class="container"><div class="eyebrow">PROPERTY COLLECTION</div><h1>Find your next space.</h1><p>Search the live inventory by location, purpose or property type.</p></div></section>
<section class="listing-shell"><div class="container"><form class="filter-bar" method="get"><input name="q" value="<?=e($q)?>" placeholder="Search location, property or keyword"><select name="stype"><option value="">Rent / Sale</option><option value="rent" <?=$stype==='rent'?'selected':''?>>Rent</option><option value="sale" <?=$stype==='sale'?'selected':''?>>Sale</option></select><select name="city"><option value="">Any city</option><?php foreach($cities as $c):?><option value="<?=e($c['city'])?>" <?=$city===$c['city']?'selected':''?>><?=e($c['city'])?></option><?php endforeach;?></select><select name="type"><option value="">All property types</option><?php foreach($types as $k=>$v):?><option value="<?=e($k)?>" <?=$type===$k?'selected':''?>><?=e($v)?></option><?php endforeach;?></select><button class="btn btn-dark" type="submit">Search →</button></form><div class="result-head"><h2><?=count($rows)?> properties</h2><a class="outline-btn" href="<?=url('properties.php')?>">Clear filters</a></div><?php if($rows):?><div class="properties-grid"><?php foreach($rows as $p): include __DIR__.'/includes/card.php'; endforeach;?></div><?php else:?><div class="empty-state"><h3>No properties match your search.</h3><p>Try a different city, property type or purpose.</p><a class="btn btn-dark" href="<?=url('properties.php')?>">View all properties</a></div><?php endif;?></div></section>
<?php include __DIR__.'/includes/site-footer.php'; ?>