<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>9Streams.xyz - Stream TV Shows and More online for free!</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css">
  <script src="/apis/dtd.js" defer></script>
  <script disable-devtool-auto src="https://cdn.jsdelivr.net/npm/disable-devtool"></script>

  <script>
    // Load and execute the external sidenav script.
    const headerScript = document.createElement("script");
    headerScript.src = "/config/sidenav.js";
    headerScript.onload = function() {
      console.log("Header script has been loaded and executed.");
    };
    document.head.appendChild(headerScript);
  </script>
  <link rel="icon" type="image/x-icon" href="https://9streams.xyz/9streams_logo.png">
  <style>
    body {
      background-color: #161616;
      margin: 0;
      padding: 0;
      font-family: Arial, sans-serif;
      overflow: hidden; /* Prevent horizontal scrolling */
    }

    /* Placeholder for the top nav bar */
    #topNavBarSpace {
      height: 60px; /* Adjust this height to match your nav bar */
    }

    #movieContainer {
      margin-left: 0;
      padding: 20px;
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
      grid-gap: 20px;
      position: absolute;
      top: 60px; /* Shift down to account for the top nav bar space */
      left: 0;
      right: 0;
      bottom: 0;
      overflow-y: scroll;
      z-index: 2;
    }

    .movieCard {
      position: relative;
      cursor: pointer;
      z-index: 2;
      transition: opacity 0.3s;
    }

    .movieCard:hover {
      opacity: 0.9;
    }

    /* Movie poster styles */
    .movieCover {
      width: 100%;
      height: auto;
      transition: transform 0.3s, filter 0.3s, border-radius 0.3s;
      border-radius: 10px;
    }

    .movieCard:hover .movieCover {
      transform: scale(1.1);
      filter: blur(5px);
    }

    /* Movie info card */
    .movieInfoCard {
      position: absolute;
      background-color: #131313;
      color: white;
      max-width: 300px;
      padding: 10px;
      border-radius: 5px;
      pointer-events: none;
      display: none;
      top: 0;
      left: -320px;
      transition: opacity 0.3s, left 0.3s;
      z-index: 1;
    }

    .movieCard:hover .movieInfoCard {
      display: block;
      opacity: 1;
      left: -10px;
    }

    .movieInfoCard h3 {
      font-size: 18px;
      margin: 0 0 5px 0;
    }

    .movieInfoCard p {
      font-size: 14px;
      margin: 0;
      max-height: 150px;
      overflow: hidden;
    }

    /* Star rating */
    .starRating {
      color: #f1c40f;
      font-size: 18px;
    }

    /* "Watch Now" button */
    .watchButton {
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 20px;
      padding: 5px 15px;
      text-align: center;
      cursor: pointer;
      transition: 0.3s;
      margin: 10px auto;
      display: block;
    }

    .watchButton:hover {
      background-color: #0056b3;
    }

    /* Pagination buttons */
    .pagination {
      text-align: center;
      position: fixed;
      bottom: 20px;
      left: 0;
      right: 0;
      background-color: rgba(34, 34, 34, 0.8);
      backdrop-filter: blur(10px);
      padding: 10px 0;
      z-index: 3;
    }

    .pagination button {
      background-color: transparent;
      color: white;
      border: none;
      font-size: 18px;
      cursor: pointer;
      margin: 0 5px;
      border-radius: 5px;
      padding: 5px 10px;
      position: relative;
      z-index: 3;
    }

    .pagination button:hover {
      background-color: #444;
    }

    .pagination button:disabled {
      color: #888;
      cursor: not-allowed;
    }

    .pagination button:disabled:hover {
      background-color: transparent;
    }

    /* Center the pagination buttons */
    .pagination-centered {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    /* Toggle button for side navigation */
    .menu-toggle {
      background-color: #333;
      color: white;
      border: none;
      font-size: 24px;
      cursor: pointer;
      border-radius: 0 10px 10px 0;
      position: fixed;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      padding: 10px 20px;
      z-index: 4;
    }

    .menu-toggle i {
      margin-right: 10px;
    }
  </style>
</head>
<body>

  <!-- Placeholder for the top nav bar -->
  <div id="topNavBarSpace"></div>

  <div id="movieContainer">
    <!-- TV show covers and info will be dynamically added here -->
  </div>

  <!-- Pagination buttons -->
  <div class="pagination pagination-centered">
    <button id="prevPage" class="pagination-button" disabled>
      <i class="fas fa-chevron-left"></i> Previous Page
    </button>
    <button id="nextPage" class="pagination-button">
      Next Page <i class="fas fa-chevron-right"></i>
    </button>
  </div>
  
  <script>
    const movieContainer = document.getElementById('movieContainer');
    let currentPage = 1;

    async function fetchTVShows(page) {
      try {
        const response = await fetch(`https://9streams.xyz/apis/tmdb.php?path=trending/tv/week?page=${page}&sort_by=popularity.desc`);
        const data = await response.json();
        const tvShows = data.results;

        // Clear the existing TV show cards
        movieContainer.innerHTML = '';

        tvShows.forEach(show => {
          const tvShowCard = document.createElement('div');
          tvShowCard.className = 'movieCard';

          const tvShowCover = document.createElement('img');
          tvShowCover.className = 'movieCover';
          tvShowCover.src = `https://image.tmdb.org/t/p/w500${show.poster_path}`;

          const tvShowInfoCard = document.createElement('div');
          tvShowInfoCard.className = 'movieInfoCard';
          tvShowInfoCard.innerHTML = `
            <h3>${show.name}</h3>
            <div class="starRating">${getStarRating(show.vote_average)}</div>
            <p>${show.overview.substring(0, 150)}${show.overview.length > 150 ? '...' : ''}</p>
            <button class="watchButton" onclick="window.location.href = 'https://9streams.xyz/tv/default?tv=${show.id}'">Watch Now</button>
          `;

          // Add click event to TV show card
          tvShowCard.addEventListener('click', () => {
            window.location.href = `https://9streams.xyz/tv/default?tv=${show.id}`;
          });

          tvShowCard.appendChild(tvShowInfoCard);
          tvShowCard.appendChild(tvShowCover);
          movieContainer.appendChild(tvShowCard);
        });

        // Update the state of the Previous Page button:
        document.getElementById('prevPage').disabled = (currentPage === 1);
      } catch (error) {
        console.error('Error fetching data:', error);
      }
    }

    // Initialize with trending TV shows on page 1
    fetchTVShows(currentPage);

    // Pagination: Next Page
    document.getElementById('nextPage').addEventListener('click', () => {
      currentPage++;
      fetchTVShows(currentPage);
    });

    // Pagination: Previous Page
    document.getElementById('prevPage').addEventListener('click', () => {
      if (currentPage > 1) {
        currentPage--;
        fetchTVShows(currentPage);
      }
    });

    // Helper function to generate star rating HTML
    function getStarRating(voteAverage) {
      const maxRating = 10;
      const starCount = 5;
      const rating = (voteAverage / maxRating) * starCount;

      let starsHtml = '';
      for (let i = 0; i < starCount; i++) {
        if (i < rating) {
          starsHtml += '<i class="fas fa-star"></i>';
        } else {
          starsHtml += '<i class="far fa-star"></i>';
        }
      }

      return starsHtml;
    }

    const sideNav = document.getElementById('sideNav');
    const menuToggle = document.getElementById('menuToggle');

    menuToggle.addEventListener('click', () => {
      sideNav.classList.toggle('show');
    });
  </script>
</body>
</html>
