<?php
$prefix = $prefix ?? ((strpos($_SERVER['PHP_SELF'] ?? '', '/admin/') !== false) ? '../' : '');
$apiPrefix = $prefix . 'api/';
$trackLoggerPath = __DIR__ . '/../track_logger.php';
?>
<footer class="site-footer thunder-footer">
  <div class="footer-inner">
    <div class="footer-left">
      <p>&copy; <?= date('Y') ?> Thunder Mountain Radio — All Rights Reserved</p>
      <p class="footer-tagline">Where lightning strikes and music roars.</p>
      <p class="footer-tagline">📡 Community Radio — Free for Non-Profit Community Groups, Clubs, and Organisations.</p>
    </div>
    <div class="footer-stats">
      <p><i class="fa-solid fa-headphones"></i> Current Listeners: <span id="currentListeners">--</span></p>
      <p><i class="fa-solid fa-eye"></i> Visitors Online: <span id="visitorsOnline">--</span></p>
    </div>
    <div class="footer-links">
      <a href="https://broadcasturself.com/" target="_blank" rel="noopener">Broadcasturself</a>
      <a href="https://abc-computing.com/" target="_blank" rel="noopener">ABC Computing</a>
    </div>
  </div>
  <?php if (!empty($_SESSION['admin_logged_in']) || !empty($_SESSION['user'])): ?>
  <div class="footer-admin-links">
    <a href="<?= htmlspecialchars($prefix) ?>admin/index.php"><i class="fa-solid fa-table-columns"></i> Return to Dashboard</a>
    <a href="<?= htmlspecialchars($prefix) ?>index.php"><i class="fa-solid fa-house"></i> Return to Main Site</a>
    <a href="<?= htmlspecialchars($prefix) ?>admin/logout.php"><i class="fa-solid fa-right-from-bracket"></i> Logout</a>
  </div>
  <?php endif; ?>
</footer>
<script src="<?= htmlspecialchars($prefix) ?>assets/js/site.js"></script>
<script>
(function(){
  const body=document.body, overlay=document.getElementById('navOverlay');
  const openBtn=document.querySelector('.storm-menu-toggle');
  const closeBtn=document.querySelector('.nav-close');
  const apiBase = <?= json.dumps('').replace('""','JSON.stringify($apiPrefix)') ?>;
  function openNav(){ body.classList.add('nav-open'); }
  function closeNav(){ body.classList.remove('nav-open'); }
  if(openBtn) openBtn.addEventListener('click', openNav);
  if(closeBtn) closeBtn.addEventListener('click', closeNav);
  if(overlay) overlay.addEventListener('click', closeNav);

  const listenersEl = document.getElementById('currentListeners');
  const visitorsEl = document.getElementById('visitorsOnline');

  async function loadListeners(){
    if(!listenersEl) return;
    try {
      const res = await fetch(apiBase + 'stats.php?ts=' + Date.now(), {cache:'no-store', credentials:'same-origin'});
      const data = await res.json();
      listenersEl.textContent = Number.isFinite(Number(data.listeners)) ? String(Number(data.listeners)) : '0';
    } catch (e) {
      listenersEl.textContent = '0';
    }
  }

  async function pingVisitors(){
    if(!visitorsEl) return;
    try {
      const res = await fetch(apiBase + 'ping.php?ts=' + Date.now(), {cache:'no-store', credentials:'same-origin'});
      const data = await res.json();
      visitorsEl.textContent = Number.isFinite(Number(data.online)) ? String(Number(data.online)) : '1';
    } catch (e) {
      visitorsEl.textContent = '1';
    }
  }

  loadListeners();
  pingVisitors();
  window.setInterval(loadListeners, 15000);
  window.setInterval(pingVisitors, 30000);
})();
</script>
</body>
</html>
<?php @include $trackLoggerPath; ?>
