<?php
define('VS_INCLUDED',true);
require_once 'includes/config.php';
$page_title='Ürün Kataloğu';
$cat_slug = $_GET['kategori']??'';
$search = trim($_GET['ara']??'');
try{
  $cats = db()->query("SELECT c.*,(SELECT COUNT(*) FROM vs_products WHERE category_id=c.id AND status='active') cnt FROM vs_categories c ORDER BY sort_order")->fetchAll(PDO::FETCH_ASSOC);
  $where=["p.status='active'"]; $params=[]; $active_cat=null;
  if($cat_slug){ $r=db()->prepare("SELECT * FROM vs_categories WHERE slug=?");$r->execute([$cat_slug]);$active_cat=$r->fetch(PDO::FETCH_ASSOC);
    if($active_cat){$where[]="p.category_id=?";$params[]=$active_cat['id'];} }
  if($search){$where[]="(p.name LIKE ? OR p.short_desc LIKE ?)";$params[]="%$search%";$params[]="%$search%";}
  $stmt=db()->prepare("SELECT p.*,c.name cat_name,c.slug cat_slug FROM vs_products p LEFT JOIN vs_categories c ON p.category_id=c.id WHERE ".implode(' AND ',$where)." ORDER BY p.featured DESC,p.sort_order,p.created_at DESC");
  $stmt->execute($params); $products=$stmt->fetchAll(PDO::FETCH_ASSOC);
}catch(Exception $e){$products=[];$cats=[];}
include 'includes/header.php';
?>

<div class="phero">
  <div class="phero-inner">
    <span class="eyebrow">Ürün Kataloğu</span>
    <h1 class="phero-h"><?=$active_cat?clean($active_cat['name']):'Tüm Ürünler'?></h1>
    <p class="phero-p"><?=$active_cat&&$active_cat['description']?clean($active_cat['description']):'Profesyonel savunma ve endüstriyel ekipmanların tamamı — filtreleyin, keşfedin.'?></p>
  </div>
  <div class="phero-num"><?=str_pad(count($products),2,'0',STR_PAD_LEFT)?></div>
</div>

<section class="section" style="background:var(--ink2);">
  <div class="wrap">
    <!-- search -->
    <form method="GET" style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;margin-bottom:40px;" data-sr>
      <?php if($cat_slug):?><input type="hidden" name="kategori" value="<?=clean($cat_slug)?>"><?php endif;?>
      <input class="vf-inp" type="text" name="ara" value="<?=clean($search)?>" placeholder="Ürün veya model ara..." style="max-width:380px;flex:1;">
      <button type="submit" class="btn btn-primary" style="padding:14px 28px;font-size:12px;">Ara</button>
      <?php if($search):?><a href="/volker/urunler.php<?=$cat_slug?'?kategori='.$cat_slug:''?>" class="btn btn-outline" style="padding:14px 24px;font-size:12px;">Temizle</a><?php endif;?>
      <span style="font-family:var(--mono);font-size:10px;color:var(--white4);letter-spacing:.12em;margin-left:auto;"><?=count($products)?> ÜRÜN</span>
    </form>

    <!-- cat tabs -->
    <?php if(!empty($cats)): ?>
    <div style="display:flex;gap:4px;flex-wrap:wrap;margin-bottom:44px;" data-sr>
      <a href="/volker/urunler.php" class="btn <?=!$cat_slug?'btn-primary':'btn-outline'?>" style="font-size:11px;padding:9px 18px;">Tümü</a>
      <?php foreach($cats as $cat): ?>
      <a href="/volker/urunler.php?kategori=<?=clean($cat['slug'])?>" class="btn <?=$cat_slug==$cat['slug']?'btn-primary':'btn-outline'?>" style="font-size:11px;padding:9px 18px;"><?=clean($cat['name'])?> <span style="opacity:.5;margin-left:4px;">(<?=$cat['cnt']?>)</span></a>
      <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <!-- grid -->
    <div class="pcard-grid cols3" data-sr>
      <?php if(empty($products)): ?>
      <div style="grid-column:1/-1;background:var(--ink3);padding:80px;text-align:center;">
        <p style="font-family:var(--mono);font-size:11px;letter-spacing:.12em;color:var(--white4);">SONUÇ BULUNAMADI</p>
      </div>
      <?php else: foreach($products as $i=>$p): ?>
      <div class="pcard">
        <div class="pcard-media">
          <?php if($p['image']): ?><img class="pcard-img" src="/volker/assets/uploads/<?=clean($p['image'])?>" alt="<?=clean($p['name'])?>">
          <?php else: ?><div class="pcard-ph"><svg width="56" height="56" fill="none" stroke="currentColor" stroke-width=".6" viewBox="0 0 24 24" style="opacity:.08"><rect x="3" y="3" width="18" height="18" rx="1"/></svg></div><?php endif;?>
          <span class="pcard-badge"><?=str_pad($i+1,2,'0',STR_PAD_LEFT)?></span>
          <?php if($p['featured']??false): ?><span class="pcard-tag">Öne Çıkan</span><?php endif; ?>
          <div class="pcard-hover"><a href="/volker/urun.php?slug=<?=clean($p['slug'])?>" class="btn btn-primary" style="padding:12px 24px;font-size:12px;">İncele</a></div>
        </div>
        <div class="pcard-body">
          <div class="pcard-cat"><?=clean($p['cat_name']??'')?></div>
          <div class="pcard-name"><?=clean($p['name'])?></div>
          <div class="pcard-desc"><?=clean(mb_substr($p['short_desc']??'',0,80))?></div>
          <div class="pcard-foot">
            <a href="/volker/urun.php?slug=<?=clean($p['slug'])?>" class="pcard-cta">Detaylar <svg width="12" height="12" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path d="M5 12h14M12 5l7 7-7 7"/></svg></a>
            <a href="/volker/urun.php?slug=<?=clean($p['slug'])?>" class="pcard-arrow"><svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M5 12h14M12 5l7 7-7 7"/></svg></a>
          </div>
        </div>
      </div>
      <?php endforeach; endif; ?>
    </div>
  </div>
</section>

<section class="cta-band" style="padding:72px 0;">
  <div class="cta-band-bg"></div><div class="cta-band-grid"></div>
  <div class="cta-inner" data-sr>
    <div>
      <h2 class="cta-h" style="font-size:clamp(32px,4vw,60px);">Aradığınızı Bulamadınız mı?<em>Bize Sorun.</em></h2>
      <p class="cta-p">Özel talep ve toplu sipariş için ekibimizle hemen iletişime geçin.</p>
    </div>
    <div class="cta-btns"><a href="/volker/iletisim.php" class="btn btn-primary btn-lg">Bize Ulaşın</a></div>
  </div>
</section>

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