<?php
// WhatsApp numarasını DB'den al (eğer dışarıdan $wa tanımlı değilse)
if (!isset($wa)) {
    if (!function_exists('getDB')) require_once dirname(__DIR__) . '/api/db.php';
    $db = getDB();
    $wa = $db->querySingle("SELECT value FROM settings WHERE key='whatsapp_number'") ?: '905344250782';
}
$wa_enc = htmlspecialchars($wa);
?>
<style>
#wa-widget-btn{position:fixed;bottom:24px;right:24px;z-index:9998;width:58px;height:58px;border-radius:50%;background:#25d366;box-shadow:0 4px 16px rgba(37,211,102,.5);display:flex;align-items:center;justify-content:center;cursor:pointer;transition:transform .2s}
#wa-widget-btn:hover{transform:scale(1.1)}
#wa-widget-btn svg{width:32px;height:32px;fill:#fff}
#wa-widget-popup{position:fixed;bottom:94px;right:24px;z-index:9997;width:300px;border-radius:16px;overflow:hidden;box-shadow:0 8px 32px rgba(0,0,0,.18);display:none;flex-direction:column;animation:waPop .2s ease}
#wa-widget-popup.open{display:flex}
@keyframes waPop{from{opacity:0;transform:translateY(12px)}to{opacity:1;transform:translateY(0)}}
.wa-popup-header{background:#25d366;padding:22px 18px 18px;text-align:center}
.wa-popup-header .wa-icon-wrap{width:62px;height:62px;background:#1ebe5d;border-radius:50%;display:flex;align-items:center;justify-content:center;margin:0 auto 12px;box-shadow:0 2px 8px rgba(0,0,0,.15)}
.wa-popup-header .wa-icon-wrap svg{width:36px;height:36px;fill:#fff}
.wa-popup-header p{color:#fff;font-size:1rem;font-weight:600;line-height:1.4;margin:0}
.wa-popup-body{background:#fff;padding:16px 18px;display:flex;align-items:center;gap:14px}
.wa-avatar{width:52px;height:52px;border-radius:50%;border:2px solid #eee;overflow:hidden;flex-shrink:0;display:flex;align-items:center;justify-content:center;background:#fff}
.wa-avatar img{width:100%;height:100%;object-fit:contain}
.wa-info{flex:1}
.wa-info strong{display:block;font-size:.95rem;color:#111}
.wa-info span{font-size:.82rem;color:#25d366;display:flex;align-items:center;gap:4px;margin-top:2px}
.wa-info span::before{content:'';width:8px;height:8px;background:#25d366;border-radius:50%;display:inline-block}
.wa-popup-footer{background:#fff;padding:0 18px 12px;text-align:right}
.wa-popup-footer a{font-size:.72rem;color:#25d366;text-decoration:none}
.wa-close-btn{position:fixed;bottom:24px;right:24px;z-index:9999;width:58px;height:58px;border-radius:50%;background:#25d366;box-shadow:0 4px 16px rgba(37,211,102,.5);display:none;align-items:center;justify-content:center;cursor:pointer;font-size:1.4rem;color:#fff;font-weight:700}
.wa-close-btn.open{display:flex}
</style>

<?php $wa_svg = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/></svg>'; ?>

<div id="wa-widget-popup">
  <div class="wa-popup-header">
    <div class="wa-icon-wrap"><?= $wa_svg ?></div>
    <p>Hemen Whatsapp Destek<br>Hattımızdan Rezervasyon<br>Oluşturun!</p>
  </div>
  <a href="https://wa.me/+<?= $wa_enc ?>?text=Merhaba%2C%20rezervasyon%20yapmak%20istiyorum." target="_blank" style="text-decoration:none">
    <div class="wa-popup-body">
      <div class="wa-avatar"><img src="<?= str_repeat('../', substr_count($_SERVER['PHP_SELF'], '/') - 1) ?>images/Avis_logo.svg.png" alt="Avis"></div>
      <div class="wa-info"><strong>Müşteri Temsilcisi</strong><span>Online</span></div>
    </div>
  </a>
  <div class="wa-popup-footer"><a href="https://wa.me/+<?= $wa_enc ?>" target="_blank">WhatsApp ile Yaz →</a></div>
</div>

<div id="wa-widget-btn" onclick="toggleWaWidget()" title="WhatsApp ile yazın"><?= $wa_svg ?></div>
<div class="wa-close-btn" id="wa-close-btn" onclick="toggleWaWidget()">✕</div>

<script>
function toggleWaWidget(){
  var popup=document.getElementById('wa-widget-popup'),btn=document.getElementById('wa-widget-btn'),close=document.getElementById('wa-close-btn'),isOpen=popup.classList.contains('open');
  popup.classList.toggle('open',!isOpen);
  btn.style.display=isOpen?'flex':'none';
  close.classList.toggle('open',!isOpen);
}
</script>
