<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>9Streams.xyz - Search TV Shows</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
         <script src="/apis/dtd.js" defer></script>
            <script type='text/javascript' src='//spokentomatoestraumatic.com/29/44/32/294432dcc07bb2f46fa506e1e78d45e4.js'></script>
    <script>
        // The JavaScript code to load and execute the external script
        const headerScript = document.createElement("script");
        headerScript.src = "https://9streams.xyz/config/sidenav.js";
        headerScript.onload = function() {
            console.log("Header script has been loaded and executed.");
            // Add any additional logic here if needed
        };
        document.head.appendChild(headerScript);
    </script> 
        

    
  <link rel="icon" type="image/x-icon" href="https://9streams.xyz/9streams_logo.png">
    <style>
        body {
            background-color: #161616;
            color: #ffffff;
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            box-sizing: border-box;
            overflow-x: hidden;
        }
            
            ::-webkit-scrollbar {
            width: 10px; /* Adjust the width */
        }

        /* Customize the thumb */
        ::-webkit-scrollbar-thumb {
            background: #525252; /* Change the thumb color */
            border-radius: 10px; /* Add rounded corners */
        }

        /* Customize the track */
        ::-webkit-scrollbar-track {
            background: #0d0d0d; /* Change the track color */
        }::-webkit-scrollbar {
            width: 10px; /* Adjust the width */
        }

        /* Customize the thumb */
        ::-webkit-scrollbar-thumb {
            background: #525252; /* Change the thumb color */
            border-radius: 10px; /* Add rounded corners */
        }

        /* Customize the track */
        ::-webkit-scrollbar-track {
            background: #0d0d0d; /* Change the track color */
        }

        .title {
            font-size: 36px;
            font-weight: bold;
            margin-bottom: 20px;
            text-align: center;
            width: 100%;
            max-width: 800px;
        }

        .search-container {
            text-align: center;
            width: 100%;
            max-width: 800px;
        }

        .search-bar-container {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            width: 100%;
            max-width: 800px;
        }

        .search-bar {
            width: 100%;
            max-width: 600px;
            padding: 15px;
            border-radius: 20px;
            border: 2px solid #ffffff;
            background-color: transparent;
            color: #ffffff;
            font-size: 18px;
            outline: none;
            transition: border-color 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
            box-sizing: border-box;
        }

        .search-bar:hover {
            border-color: #433382;
            box-shadow: 0 0 15px rgba(97,86,155, 0.5);
        }

        .search-button {
            padding: 15px 30px;
            border: none;
            border-radius: 20px;
            background-color: #433382;
            color: #ffffff;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s, color 0.3s;
        }

        .search-button:hover {
            background-color: #61569b;
        }

        .tabs {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            flex-wrap: nowrap;
            gap: 10px;
        }

        .tab {
            margin: 0;
            padding: 10px 20px;
            border-radius: 20px;
            cursor: pointer;
            background-color: #333;
            color: #aaa;
            transition: background-color 0.3s, color 0.3s;
            text-align: center;
            flex-shrink: 1;
            font-size: 14px;
            white-space: nowrap;
        }

        .tab.active, .tab:hover {
            background-color: #fff;
            color: #000;
        }

        .results-heading {
            font-size: 32px;
            margin-bottom: 10px;
            text-align: center;
            width: 100%;
            max-width: 800px;
        }

        .results-count {
            font-size: 18px;
            color: #aaa;
            margin-bottom: 10px;
            text-align: center;
            width: 100%;
            max-width: 800px;
        }

        .note-bar {
            font-size: 14px;
            color: #aaa;
            text-align: center;
            max-width: 800px;
            margin-bottom: 30px;
        }

        .results-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 20px;
            width: 100%;
            max-width: 1200px;
            box-sizing: border-box;
        }

        .card {
            background-color: #222;
            border-radius: 20px;
            padding: 20px;
            width: 250px;
            height: 400px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
            cursor: pointer;
            transition: transform 0.3s;
            overflow: hidden;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .card:hover {
            transform: scale(1.05);
        }

        .card img {
            width: 100%;
            height: 60%;
            object-fit: cover;
            border-radius: 10px;
            margin-bottom: 15px;
        }

        .card-title {
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 10px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
        }

        .card-description {
            font-size: 14px;
            color: #ccc;
            margin-bottom: 10px;
            height: 50px;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .card-genres {
            font-size: 14px;
            color: #777;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .pagination {
            margin-top: 30px;
            display: flex;
            justify-content: center;
            gap: 10px;
            width: 100%;
            max-width: 800px;
        }

        .page-btn {
            padding: 10px 20px;
            border-radius: 20px;
            background-color: #333;
            color: #aaa;
            cursor: pointer;
            transition: background-color 0.3s, color 0.3s;
        }

        .page-btn:hover {
            background-color: #fff;
            color: #000;
        }

        .page-btn.disabled {
            background-color: #555;
            color: #777;
            cursor: not-allowed;
        }

        @media (max-width: 600px) {
            .search-bar {
                width: calc(100% - 110px); /* Adjust width to fit better on small screens */
            }

            .search-button {
                font-size: 16px;
                padding: 15px 20px;
            }

            .tabs {
                flex-direction: row;
                overflow-x: auto;
                overflow-y: hidden;
                white-space: nowrap;
            }

            .tab {
                flex: 1 0 auto;
                font-size: 12px;
                padding: 8px 10px;
            }

            .results-heading, .results-count {
                font-size: 24px;
            }

            .card {
                width: 100%;
                max-width: 300px; /* Adjust card width for mobile */
            }
        }
    </style>
</head>
<body>
    <div class="title">What TV Show do you wanna watch today?</div>

    <div class="search-container">
        <div class="search-bar-container">
            <input id="searchInput" class="search-bar" type="text" placeholder="Search for TV shows...">
            <button class="search-button">Search</button>
        </div>
        <div class="tabs">
            <div id="moviesTab" class="tab">Movies</div>
            <div id="tvTab" class="tab active">TV Shows</div>
            <div id="animeTab" class="tab">Anime</div>
            <div id="mangaTab" class="tab">Manga</div>
        </div>
    </div>

    <div id="resultsHeading" class="results-heading"></div>
    <div id="resultsCount" class="results-count"></div>
    <div class="note-bar">
        Note: You might see random search results showing niche or unpopular TV shows which might not actually be available for streaming.
    </div>
    <div id="resultsContainer" class="results-container"></div>

    <div id="pagination" class="pagination"></div>

    <script>
        const searchInput = document.getElementById('searchInput');
        const searchButton = document.querySelector('.search-button');
        const resultsHeading = document.getElementById('resultsHeading');
        const resultsCount = document.getElementById('resultsCount');
        const resultsContainer = document.getElementById('resultsContainer');
        const pagination = document.getElementById('pagination');
        const tabs = document.querySelectorAll('.tab');
        let currentTab = 'tv';
        let genreMap = {};
        let currentPage = 1;
        let totalPages = 1;
        let currentQuery = '';

        async function fetchGenres() {
            const response = await fetch('https://9streams.xyz/apis/tmdb.php?path=genre/tv/list');
            const data = await response.json();
            genreMap = data.genres.reduce((acc, genre) => {
                acc[genre.id] = genre.name;
                return acc;
            }, {});
        }

        tabs.forEach(tab => {
            tab.addEventListener('click', function() {
                tabs.forEach(t => t.classList.remove('active'));
                this.classList.add('active');

                if (this.id === 'moviesTab') {
                    window.location.href = '/search/movie';
                } else if (this.id === 'tvTab') {
                    window.location.href = '/search/tv';
                } else if (this.id === 'animeTab') {
                    window.location.href = '/search/anime';
                } else if (this.id === 'mangaTab') {
                    window.location.href = '/search/manga';
                }
            });
        });

        searchInput.addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                currentQuery = this.value;
                currentPage = 1;
                searchShows(currentQuery, currentPage);
            }
        });

        searchButton.addEventListener('click', function() {
            currentQuery = searchInput.value;
            currentPage = 1;
            searchShows(currentQuery, currentPage);
        });

        async function searchShows(query, page) {
            if (!query) return;
            const response = await fetch(`https://api.themoviedb.org/3/search/tv?api_key=441962d66631fd0862b9e9b2120ed1dd&query=${query}&page=${page}`);
            const data = await response.json();
            totalPages = data.total_pages;
            displayResults(query, data.results, data.total_results, page, totalPages);
        }

        function displayResults(query, results, totalResults, page, totalPages) {
            resultsHeading.textContent = `Search Results for "${query}":`;
            resultsCount.textContent = `${totalResults} results found.`;
            resultsContainer.innerHTML = '';

            results.forEach(result => {
                const card = document.createElement('div');
                card.className = 'card';
                card.onclick = () => window.location.href = `/tv/default?tv=${result.id}`;

                const imageUrl = result.poster_path
                    ? `https://image.tmdb.org/t/p/w500${result.poster_path}`
                    : 'https://developers.elementor.com/docs/assets/img/elementor-placeholder-image.png';

                const genreNames = result.genre_ids.map(id => genreMap[id]).join(', ') || 'Unknown';

                card.innerHTML = `
                    <img src="${imageUrl}" alt="${result.name}">
                    <div class="card-title">${result.name}</div>
                    <div class="card-description">${result.overview.length > 100 ? result.overview.slice(0, 100) + '...' : result.overview}</div>
                    <div class="card-genres">${genreNames}</div>
                `;
                resultsContainer.appendChild(card);
            });

            renderPagination(page, totalPages);
        }

        function renderPagination(page, totalPages) {
            pagination.innerHTML = '';

            const prevBtn = document.createElement('div');
            prevBtn.className = `page-btn ${page === 1 ? 'disabled' : ''}`;
            prevBtn.textContent = 'Previous';
            prevBtn.onclick = () => {
                if (page > 1) {
                    currentPage--;
                    searchShows(currentQuery, currentPage);
                }
            };
            pagination.appendChild(prevBtn);

            const nextBtn = document.createElement('div');
            nextBtn.className = `page-btn ${page === totalPages ? 'disabled' : ''}`;
            nextBtn.textContent = 'Next';
            nextBtn.onclick = () => {
                if (page < totalPages) {
                    currentPage++;
                    searchShows(currentQuery, currentPage);
                }
            };
            pagination.appendChild(nextBtn);
        }

        fetchGenres();
    </script>
</body>
</html>
