<?php
$footerBase = dirname($_SERVER['SCRIPT_NAME'] ?? '/');
$isAdminPage = strpos($_SERVER['SCRIPT_NAME'] ?? '', '/admin/') !== false;
?>
<footer class="site-footer">
  <div class="footer-inner shell-wide">
    <div class="footer-brand">Almost Heaven Radio</div>
    <div class="footer-copy">Broadcasting from the heart of the hills.</div>
    <div class="footer-stats">
      <span><strong>Current Listeners:</strong> <span id="listener-count">--</span></span>
      <span><strong>Visitors Online:</strong> <span id="visitor-count">--</span></span>
    </div>
  </div>
</footer>
<script>
(function(){
  const base = <?= json_encode($isAdminPage ? '..' : '.') ?>;
  const pingUrl = base + '/api/ping.php';
  const onlineUrl = base + '/api/visitors_online.php';
  const listenerEl = document.getElementById('listener-count');
  const visitorEl = document.getElementById('visitor-count');
  fetch(pingUrl, {credentials:'same-origin'}).catch(()=>{});
  fetch(onlineUrl, {credentials:'same-origin'})
    .then(r => r.ok ? r.json() : Promise.reject())
    .then(d => { if (visitorEl) visitorEl.textContent = (d.online ?? d.visitors ?? '--'); })
    .catch(()=>{});
  if (listenerEl && !listenerEl.textContent.trim()) listenerEl.textContent = '--';
})();
</script>
</body>
</html>
