<?php
define('VS_INCLUDED',true);
require_once 'includes/config.php';
$slug=$_GET['slug']??'';
if(!$slug){header('Location:/volker/urunler.php');exit;}
try{
  $s=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 p.slug=? AND p.status='active'");
  $s->execute([$slug]);$product=$s->fetch(PDO::FETCH_ASSOC);
  $specs=[];if($product&&$product['specs']){$d=json_decode($product['specs'],true);if(is_array($d))$specs=$d;}
  $gallery=[];if($product&&$product['gallery'])$gallery=array_filter(explode(',',$product['gallery']));
  $related=[];
  if($product){$r=db()->prepare("SELECT p.*,c.name cat_name FROM vs_products p LEFT JOIN vs_categories c ON p.category_id=c.id WHERE p.category_id=? AND p.id!=? AND p.status='active' LIMIT 4");$r->execute([$product['category_id'],$product['id']]);$related=$r->fetchAll(PDO::FETCH_ASSOC);}
}catch(Exception $e){$product=null;}
if(!$product){header('Location:/volker/urunler.php');exit;}
$page_title=$product['name'];
include 'includes/header.php';
?>

<div class="bread">
  <a href="/volker/">Ana Sayfa</a><span class="sep">›</span>
  <a href="/volker/urunler.php">Ürünler</a>
  <?php if($product['cat_name']): ?><span class="sep">›</span><a href="/volker/urunler.php?kategori=<?=clean($product['cat_slug'])?>"><?=clean($product['cat_name'])?></a><?php endif; ?>
  <span class="sep">›</span><span class="cur"><?=clean($product['name'])?></span>
</div>

<section style="padding:24px 0 100px;">
  <div class="pdetail" data-sr>
    <!-- gallery -->
    <div class="pd-gallery">
      <div class="pd-main">
        <?php if($product['image']): ?>
        <img id="pdmain" class="pd-main-img" src="/volker/assets/uploads/<?=clean($product['image'])?>" alt="<?=clean($product['name'])?>">
        <?php else: ?>
        <div style="width:100%;aspect-ratio:4/3;background:var(--ink4);display:flex;align-items:center;justify-content:center;">
          <svg width="80" height="80" fill="none" stroke="currentColor" stroke-width=".5" viewBox="0 0 24 24" style="opacity:.08"><rect x="3" y="3" width="18" height="18" rx="1"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>
        </div>
        <?php endif; ?>
      </div>
      <?php if(!empty($gallery)): ?>
      <div class="pd-thumbs">
        <?php if($product['image']): ?>
        <div class="pd-thumb on" onclick="setThumb('/volker/assets/uploads/<?=clean($product['image'])?>',this)">
          <img src="/volker/assets/uploads/<?=clean($product['image'])?>" alt="">
        </div>
        <?php endif; ?>
        <?php foreach($gallery as $g): $g=trim($g); ?>
        <div class="pd-thumb" onclick="setThumb('/volker/assets/uploads/<?=clean($g)?>',this)">
          <img src="/volker/assets/uploads/<?=clean($g)?>" alt="">
        </div>
        <?php endforeach; ?>
      </div>
      <?php endif; ?>
    </div>

    <!-- info -->
    <div class="pd-info">
      <div>
        <div class="pd-cat"><?=clean($product['cat_name']??'')?></div>
        <h1 class="pd-name"><?=clean($product['name'])?></h1>
      </div>

      <p class="pd-desc"><?=nl2br(clean($product['description']?:$product['short_desc']))?></p>

      <?php if(!empty($specs)): ?>
      <div>
        <div class="pd-spec-ttl">Teknik Özellikler</div>
        <div class="pd-specs">
          <?php foreach($specs as $k=>$v): ?>
          <div class="pd-spec-row"><span class="pd-sk"><?=clean($k)?></span><span class="pd-sv"><?=clean($v)?></span></div>
          <?php endforeach; ?>
        </div>
      </div>
      <?php endif; ?>

      <div class="pd-actions">
        <a href="/volker/iletisim.php?urun=<?=urlencode($product['name'])?>" class="btn btn-primary btn-lg">
          Teklif İste
          <span class="btn-arrow"><svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path d="M5 12h14M12 5l7 7-7 7"/></svg></span>
        </a>
        <a href="/volker/urunler.php<?=$product['cat_slug']?'?kategori='.clean($product['cat_slug']):''?>" class="btn btn-outline">Kategoriye Dön</a>
      </div>
    </div>
  </div>

  <!-- Related -->
  <?php if(!empty($related)): ?>
  <div class="wrap" style="margin-top:80px;" data-sr>
    <div style="border-top:1px solid var(--rule);padding-top:60px;margin-bottom:44px;">
      <span class="eyebrow">Benzer Ürünler</span>
      <h2 style="font-size:clamp(32px,4vw,52px);">İlgili Ürünler</h2>
    </div>
    <div class="pcard-grid" style="grid-template-columns:repeat(<?=min(count($related),4)?>,1fr);">
      <?php foreach($related as $i=>$r): ?>
      <div class="pcard">
        <div class="pcard-media">
          <?php if($r['image']): ?><img class="pcard-img" src="/volker/assets/uploads/<?=clean($r['image'])?>" alt="<?=clean($r['name'])?>">
          <?php else: ?><div class="pcard-ph" style="aspect-ratio:1;"><svg width="48" height="48" 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>
          <div class="pcard-hover"><a href="/volker/urun.php?slug=<?=clean($r['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($r['cat_name']??'')?></div>
          <div class="pcard-name"><?=clean($r['name'])?></div>
          <div class="pcard-foot">
            <a href="/volker/urun.php?slug=<?=clean($r['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($r['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;?>
    </div>
  </div>
  <?php endif;?>
</section>

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