<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="visitor-count px-3 py-2 border border-light rounded bg-dark text-light">
        👀 Visitors so far:
        <?php
          // FIX: Only start session if not already active
          if (session_status() === PHP_SESSION_NONE) {
            session_start();
          }

          $counterFile = __DIR__ . '/counter.txt';

          // Ensure counter file exists
          if (!file_exists($counterFile)) {
            file_put_contents($counterFile, "0", LOCK_EX);
          }

          // Only count once per session
          if (!isset($_SESSION['counted'])) {
            $_SESSION['counted'] = true;

            $count = (int) @file_get_contents($counterFile);
            $count++;

            file_put_contents($counterFile, (string)$count, LOCK_EX);
          } else {
            $count = (int) @file_get_contents($counterFile);
          }

          echo number_format($count);
        ?>
      </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';
    });
}

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

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