<?php
require_once __DIR__.'/inc/functions.php';
$site = read_json(__DIR__.'/assets/data/site.json');
$brand = $site['brand'] ?? 'Kesin Döner';
$base = rtrim(dirname($_SERVER['SCRIPT_NAME'] ?? '/'), '/\\');
$hero = $site['hero'] ?? [];
$features = $site['features'] ?? [];
$gallery = $site['gallery'] ?? [];
$about = $site['about'] ?? [];
$contact = $site['contact'] ?? [];
$social = $site['social'] ?? [];
// Prepare hero images: mirror remote to local for reliable display
$heroImages = [];
if(isset($hero['image']) && $hero['image']){ $hero['image'] = mirror_image($hero['image']); }
// Prefer Döner & İskender images from features
$preferIds = ['et-doner','iskender'];
foreach($features as $f){
  if(isset($f['id']) && in_array($f['id'],$preferIds,true) && !empty($f['image'])){
    $heroImages[] = mirror_image($f['image']);
  }
}
if(isset($hero['images']) && is_array($hero['images']) && count($hero['images'])>0){
  foreach($hero['images'] as $u){ $heroImages[] = mirror_image($u); }
}
if(count($heroImages)===0){
  $defaults = [
    'https://images.unsplash.com/photo-1604908176997-1021e21e4ec5?q=80&w=1600&auto=format&fit=crop',
    'https://images.unsplash.com/photo-1612878132611-6642d5506c50?q=80&w=1600&auto=format&fit=crop',
    'https://images.unsplash.com/photo-1546793665-c74683f339c1?q=80&w=1600&auto=format&fit=crop'
  ];
  foreach($defaults as $u){ $heroImages[] = mirror_image($u); }
}
if(!isset($hero['image']) || !$hero['image']){ $hero['image'] = $heroImages[0] ?? ''; }
$heroImages = array_map(function($u) use ($base){ return (strpos($u,'/assets/')===0) ? $base.$u : $u; }, $heroImages);
$hero['image'] = (strpos($hero['image'],'/assets/')===0) ? $base.$hero['image'] : $hero['image'];
include __DIR__.'/inc/header.php';
?>
<section class="hero" id="hero">
  <div class="hero-bg" id="hero-bg" style="<?php echo isset($hero['image']) ? 'background-image: radial-gradient(1200px 600px at 10% 10%, rgba(201,29,29,.12), transparent 60%), radial-gradient(1000px 500px at 90% 20%, rgba(212,163,115,.12), transparent 65%), url('.h($hero['image']).');' : ''; ?>"></div>
  <div class="container hero-content">
    <h1 id="hero-title"><?php echo h($hero['title'] ?? 'Kesin Lezzet, Kesin Döner'); ?></h1>
    <p id="hero-sub"><?php echo h($hero['subtitle'] ?? 'Ankara usulü döneri gurme dokunuşla yeniden yorumluyoruz.'); ?></p>
    <div class="hero-actions">
      <a href="/menu.php" class="btn primary">Menüyü Gör</a>
      <a href="#rezervasyon" class="btn ghost">Rezervasyon</a>
    </div>
  </div>
</section>
<section class="features" id="lezzetler">
  <div class="container section-head">
    <h2>İmza Lezzetler</h2>
    <p>Günlük taze ürünler, özel soslar ve dengeli sunum.</p>
    <div class="badge-list">
      <span class="badge">
        <span class="badge-ico"><img alt="Günlük Taze" src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path fill='%23ffffff' d='M12 3c-1.8 2.8-4.8 4.6-8 5 1 6.2 5.2 10.5 8 13 2.8-2.5 7-6.8 8-13-3.2-.4-6.2-2.2-8-5zm0 6c1.7 0 3 1.3 3 3s-1.3 3-3 3-3-1.3-3-3 1.3-3 3-3z'/></svg>'"></span>
        <span>Günlük Taze</span>
      </span>
      <span class="badge">
        <span class="badge-ico"><img alt="Özel Sos" src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path fill='%23ffffff' d='M7 3h10l1 3-2 2v10a3 3 0 0 1-3 3H11a3 3 0 0 1-3-3V8L6 6l1-3zm4 7h2v8h-2v-8z'/></svg>'"></span>
        <span>Özel Sos</span>
      </span>
      <span class="badge">
        <span class="badge-ico"><img alt="Usta Elinden" src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path fill='%23ffffff' d='M12 2c2.2 0 4 1.8 4 4v2h2a2 2 0 0 1 2 2v3a5 5 0 0 1-5 5H9a5 5 0 0 1-5-5V10a2 2 0 0 1 2-2h2V6c0-2.2 1.8-4 4-4z'/></svg>'"></span>
        <span>Usta Elinden</span>
      </span>
      <span class="badge">
        <span class="badge-ico"><img alt="Dengeli Sunum" src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path fill='%23ffffff' d='M12 4l2 4h4l-3 3 1 5-4-2-4 2 1-5-3-3h4l2-4z'/></svg>'"></span>
        <span>Dengeli Sunum</span>
      </span>
    </div>
  </div>
  <div class="container grid cards" id="features-list">
    <?php foreach($features as $f): ?>
      <article class="card">
        <img src="<?php echo h($f['image'] ?? ''); ?>" alt="<?php echo h($f['name'] ?? ''); ?>">
        <div class="card-body">
          <h3><?php echo h($f['name'] ?? ''); ?></h3>
          <p><?php echo h($f['desc'] ?? ''); ?></p>
          <div class="price"><?php echo isset($f['price']) ? '₺'.h($f['price']) : ''; ?></div>
        </div>
      </article>
    <?php endforeach; ?>
  </div>
</section>
<?php $signatureImage = $site['signature_image'] ?? ''; $signatureSrc = (strpos($signatureImage,'/assets/')===0) ? $base.$signatureImage : $signatureImage; ?>
<?php if($signatureImage): ?>
<section class="signature">
  <div class="container">
    <img src="<?php echo h($signatureSrc); ?>" alt="İmza Lezzetler Görseli" class="signature-image">
  </div>
</section>
<?php endif; ?>
<section class="gallery" id="galeri">
  <div class="container section-head">
    <h2>Galeri</h2>
    <p>Sunum ve detaylarda farkı görün.</p>
  </div>
  <div class="container grid gallery-grid" id="gallery-grid">
    <?php foreach($gallery as $it): ?>
      <?php $u = is_array($it) ? ($it['url'] ?? '') : $it; $t = is_array($it) ? ($it['type'] ?? '') : (preg_match('#\\.(mp4|webm|ogg|ogv|m4v|mov|avi|mkv)(\\?.*)?$#i',$u)?'video':'image'); $ttl = is_array($it) ? ($it['title'] ?? '') : ''; $u2 = (strpos($u,'/assets/')===0) ? $base.$u : $u; ?>
      <div class="g-item" data-url="<?php echo h($u2); ?>" data-type="<?php echo h($t); ?>" data-title="<?php echo h($ttl); ?>">
        <?php if($t==='video'): ?>
          <span class="video-badge" aria-hidden="true"></span>
          <video src="<?php echo h($u2); ?>" playsinline muted style="width:100%; height:100%; object-fit:cover;"></video>
        <?php else: ?>
          <img loading="lazy" src="<?php echo h($u2); ?>" alt="<?php echo h($ttl ?: 'Galeri'); ?>">
        <?php endif; ?>
      </div>
    <?php endforeach; ?>
  </div>
</section>
<div class="gallery-modal" id="gallery-modal">
  <div class="gm-backdrop" id="gm-backdrop"></div>
  <div class="gm-content">
    <button class="btn ghost gm-close" id="gm-close">Kapat</button>
    <div class="gm-body" id="gm-body"></div>
    <div class="gm-title" id="gm-title"></div>
  </div>
</div>
<section class="about" id="hakkimizda">
  <div class="container section-head">
    <h2>Hakkımızda</h2>
    <p>Yerel lezzeti çağdaş sunumla buluşturuyoruz.</p>
  </div>
  <div class="container about-long" id="about-long">
    <?php
      $texts = [];
      foreach($about as $a){
        $t = trim($a['text'] ?? '');
        if($t!==''){ $texts[] = $t; }
      }
      $full = implode("\n\n", $texts);
      echo '<p class="about-p">'.h($full).'</p>';
    ?>
  </div>
</section>
<section class="cta" id="rezervasyon">
  <div class="container cta-inner">
    <h2>Rezervasyon ve İletişim</h2>
    <p>Telefonla arayın veya WhatsApp üzerinden hızlıca ulaşın.</p>
    <?php
      $rawPhone = $contact['phone'] ?? '';
      $rawWa = $contact['whatsapp'] ?? '';
      $safeTel = preg_replace('/[^\+\d]/', '', $rawPhone);
      $safeWa = preg_replace('/\D+/', '', $rawWa);
    ?>
    <div class="cta-actions">
      <a class="btn primary" id="cta-call" href="<?php echo $safeTel ? 'tel:'.h($safeTel) : '#'; ?>">Ara</a>
      <div class="wa-dropdown">
        <a class="btn ghost" id="cta-wa" data-wa-number="<?php echo h($safeWa); ?>" href="<?php echo $safeWa ? 'https://wa.me/'.h($safeWa) : '#'; ?>" target="_blank" rel="noopener" aria-haspopup="true" aria-expanded="false">WhatsApp</a>
        <div class="wa-menu" role="menu">
          <button class="wa-item" data-wa="order" role="menuitem">Sipariş vereceğim</button>
          <button class="wa-item" data-wa="status" role="menuitem">Siparişimin durumunu öğreneceğim</button>
        </div>
      </div>
    </div>
    <div class="cta-phones">
      <a class="cta-phone" href="<?php echo $safeTel ? 'tel:'.h($safeTel) : '#'; ?>"><?php echo h($rawPhone); ?></a>
      <span class="sep">•</span>
      <a class="cta-phone" href="<?php echo $safeWa ? 'https://wa.me/'.h($safeWa) : '#'; ?>" target="_blank" rel="noopener"><?php echo h($rawWa); ?></a>
    </div>
    <div class="cta-details">
      <div>
        <strong>Adres</strong>
        <p id="cta-address"><?php echo h($contact['address'] ?? ''); ?></p>
      </div>
      <div>
        <strong>Çalışma Saatleri</strong>
        <p id="cta-hours"><?php echo h($contact['hours'] ?? ''); ?></p>
      </div>
    </div>
  </div>
</section>
<script>
  (function(){
    const imgs = <?php echo json_encode($heroImages, JSON_UNESCAPED_UNICODE); ?>;
    const el = document.getElementById('hero-bg');
    if(!el || !imgs || imgs.length===0) return;
    let i = 0;
    function setBg(u){
      el.style.backgroundImage = "radial-gradient(1200px 600px at 10% 10%, rgba(201,29,29,.12), transparent 60%), radial-gradient(1000px 500px at 90% 20%, rgba(212,163,115,.12), transparent 65%), url('"+u+"')";
    }
    setBg(imgs[0]);
    if(imgs.length>1){
      setInterval(function(){ i=(i+1)%imgs.length; setBg(imgs[i]); }, 4500);
    }
  })();
</script>
<script>
  (function(){
    const grid = document.getElementById('gallery-grid');
    const modal = document.getElementById('gallery-modal');
    const body = document.getElementById('gm-body');
    const title = document.getElementById('gm-title');
    const closeBtn = document.getElementById('gm-close');
    const backdrop = document.getElementById('gm-backdrop');
    function open(it){
      const url = it.dataset.url;
      const type = it.dataset.type;
      const ttl = it.dataset.title || '';
      body.innerHTML = '';
      title.textContent = ttl;
      if(type==='video'){
        const v = document.createElement('video');
        v.src = url; v.controls = true; v.playsInline = true; v.style.width='100%'; v.style.maxHeight='70vh';
        body.appendChild(v);
      } else {
        const img = document.createElement('img');
        img.src = url; img.alt = ttl || 'Galeri'; img.style.width='100%'; img.style.maxHeight='80vh'; img.style.objectFit='contain';
        body.appendChild(img);
      }
      modal.classList.add('open');
    }
    function close(){ modal.classList.remove('open'); }
    if(grid){
      grid.querySelectorAll('.g-item').forEach(el=>{
        el.addEventListener('click',()=>open(el));
      });
    }
    if(closeBtn) closeBtn.addEventListener('click', close);
    if(backdrop) backdrop.addEventListener('click', close);
    document.addEventListener('keydown', function(e){ if(e.key==='Escape') close(); });
  })();
</script>
<?php include __DIR__.'/inc/footer.php'; ?>
