<div id="loading-container" style="text-align: center;">
        <div style="font-size: 15px; color: #4267b2;">Loading<span id="loading-dots">...</span></div>
    </div>
    <div id="matches-container" style="display: none;">
        <table>
            <thead>
                <tr>
                    <th>Time</th>
                    <th>Event</th>
                    <th>Teams</th>
                    <th>Score</th>
                    <th>Live</th>
                </tr>
            </thead>
            <tbody id="schedule"></tbody>
        </table>
    </div>

    <script>
        // Loading animation: Add dots after the word "Loading"
        const loadingDots = document.getElementById("loading-dots");
        let dotCount = 0;
        const loadingInterval = setInterval(() => {
            dotCount = (dotCount + 1) % 4; // Cycle between 0, 1, 2, 3 dots
            loadingDots.textContent = '.'.repeat(dotCount);
        }, 500);

        // Fetch match data from the API
        const fetchMatchData = async () => {
            try {
                const url = `https://1452proxy.nfsfree.workers.dev/?quest=https://hd.la2zo.eu.org/senarai/api.php`;

                const response = await fetch(url);
                if (!response.ok) {
                    throw new Error('Failed to fetch match data');
                }
                return response.json();
            } catch (error) {
                console.error('Error fetching match data:', error);
                throw error;
            }
        };

        // Display match schedule
        const displayMatches = async () => {
            try {
                const container = document.getElementById('matches-container');
                const scheduleTable = document.getElementById('schedule');
                const loadingContainer = document.getElementById("loading-container");

                const matches = await fetchMatchData();

                for (const match of matches) {
                    // Create a table row for each match
                    const row = document.createElement('tr');
                    row.setAttribute('data-match', match.match); // Use "match.match" as a unique identifier

                    // Check if the match is live
                    let scoreCellContent = match.score;
                    if (match.sid) {
                        scoreCellContent = `<span class="live-score">${match.score}</span>`;
                    }

                    row.innerHTML = `
                        <td>${match.ko}</td>
                        <td>${match.league}</td>
                        <td>${match.match}</td>
                        <td class="score">${scoreCellContent}</td>
                        <td><a href="https://gn.nobar.eu.org/CLICK" onclick="window.open('https://xemphongnha-kebang.blogspot.com/p/live?id=${match.id}')" target="_self">Watch</a></td>
                    `;
                    scheduleTable.appendChild(row);
                }

                // Show the match container and hide the loading text
                container.style.display = 'block';
                loadingContainer.style.display = 'none';

                // Start live score updates every second
                setInterval(updateLiveScores, 1000);
            } catch (error) {
                console.error('Error displaying matches:', error);
                document.getElementById("loading-container").innerHTML = `<div style="font-size: 15px; color: red;">Error loading data. Please reload the site.</div>`;
            } finally {
                clearInterval(loadingInterval); // Stop the loading animation
            }
        };

        // Update live scores periodically
        const updateLiveScores = async () => {
            try {
                const matches = await fetchMatchData();
                const rows = document.querySelectorAll('tr[data-match]');

                rows.forEach(row => {
                    const matchName = row.getAttribute('data-match');
                    const match = matches.find(m => m.match === matchName);

                    if (match) {
                        const scoreCell = row.querySelector('.score');
                        if (match.sid) {
                            // Apply red color to the score if live
                            scoreCell.innerHTML = `<span class="live-score">${match.score}</span>`;
                        } else {
                            scoreCell.textContent = match.score;
                        }
                    }
                });
            } catch (error) {
                console.error('Error updating live scores:', error);
            }
        };

        // Fetch and display matches on page load
        displayMatches();
    </script>