<footer class="bg-footer text-white mt-5 p-4 text-center">
  <div class="container">

    <!-- Top Row: Admin + Visitor Count + Back to Top -->
    <div class="d-flex justify-content-center align-items-center flex-wrap gap-4 mb-3">

      <div class="px-3 py-2 border border-light rounded bg-dark text-warning">
        🔐 <a href="/admin.php" class="text-warning text-decoration-none">Admin Login</a>
      </div>
      <div class="footer-live-stats px-3 py-2 rounded bg-dark text-white">
        <p>🎧 Current Listeners: <span id="currentListeners">--</span></p>
        <p>👀 Visitors Online: <span id="visitorsOnline">--</span></p>
      </div>

      <div class="px-3 py-2 border border-light rounded bg-dark">
        <a href="#"
           onclick="window.scrollTo({ top: 0, behavior: 'smooth' }); return false;"
           class="text-light text-decoration-none">
          ↑ Back to Top
        </a>
      </div>

    </div>

    <!-- Footer Info -->
    <p>HomeTimeRadio © 2025 All Rights Reserved</p>
    <p>
      Powered by <a href="https://abc-computing.com/" class="text-info">ABC Computing</a> •
      Hosted by <a href="https://broadcasturself.com" class="text-info">Broadcasturself</a>
    </p>
    <p class="mt-3">
      📡 Community Radio — Free for Non-Profit Community Groups, Clubs, and Organisations.
    </p>

  </div>
</footer>

<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="/assets/js/jquery.win.js"></script>

<script>
function updateTrack() {
  fetch('/live-track.php')
    .then(response => response.text())
    .then(html => {
      const el = document.getElementById('track-text');
      if (el) el.innerHTML = html;
    })
    .catch(() => {
      const el = document.getElementById('track-text');
      if (el) el.textContent = 'Error fetching track';
    });
}

function updateDJSchedule() {
  fetch('/live-djs.php')
    .then(response => response.json())
    .then(data => {
      const djNow = document.getElementById('dj-now');
      const hourNow = document.getElementById('hour-now');
      const djNext = document.getElementById('dj-next');
      const hourNext = document.getElementById('hour-next');
      const djAfter = document.getElementById('dj-after');
      const hourAfter = document.getElementById('hour-after');

      if (djNow) djNow.textContent = data.current.dj;
      if (hourNow) hourNow.textContent = data.current.hour;
      if (djNext) djNext.textContent = data.next.dj;
      if (hourNext) hourNext.textContent = data.next.hour;
      if (djAfter) djAfter.textContent = data.after.dj;
      if (hourAfter) hourAfter.textContent = data.after.hour;
    })
    .catch(() => {
      const djNow = document.getElementById('dj-now');
      const djNext = document.getElementById('dj-next');
      const djAfter = document.getElementById('dj-after');

      if (djNow) djNow.textContent = 'Error';
      if (djNext) djNext.textContent = 'Error';
      if (djAfter) djAfter.textContent = 'Error';
    });
}


async function updateFooterStats() {
  try {
    const [listenersRes, visitorsRes] = await Promise.all([
      fetch('/api/stats.php', { cache: 'no-store' }),
      fetch('/api/ping.php', { cache: 'no-store' })
    ]);

    const listenersData = await listenersRes.json();
    const visitorsData = await visitorsRes.json();

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

    if (listenersEl) listenersEl.textContent = listenersData && listenersData.ok ? listenersData.listeners : '--';
    if (visitorsEl) visitorsEl.textContent = visitorsData && visitorsData.ok ? visitorsData.online : '--';
  } catch (e) {
    const listenersEl = document.getElementById('currentListeners');
    const visitorsEl = document.getElementById('visitorsOnline');
    if (listenersEl) listenersEl.textContent = '--';
    if (visitorsEl) visitorsEl.textContent = '--';
  }
}

// Initial load
updateTrack();
updateDJSchedule();
updateFooterStats();

// Refresh timers (your comments said 60 seconds but track is 30s here)
setInterval(updateTrack, 30000);
setInterval(updateDJSchedule, 60000);
setInterval(updateFooterStats, 15000);
</script>