<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Eventos Deportivos Movistar Plus Elcano.top</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .table-title {
            font-size: 1.2em;
            font-weight: bold;
            margin-top: 20px;
        }
        #loading, #error {
            text-align: center;
            padding: 20px;
        }
        #loading { color: #888; }
        #error { color: red; }
    </style>
</head>
<body>
    <h1>Eventos Deportivos Movistar Plus</h1>
    <div id="loading">Cargando eventos...</div>
    <div id="error"></div>
    <div id="events-container"></div>

    <script>
        function parseEvents(htmlContent) {
            const parser = new DOMParser();
            const doc = parser.parseFromString(htmlContent, 'text/html');
            const eventsByDate = {};
            
            // Find all date sections
            const dateSections = doc.querySelectorAll('.fecha-bar');
            
            dateSections.forEach(dateSection => {
                const currentDate = dateSection.textContent.trim();
                eventsByDate[currentDate] = [];

                // Find the parent element that contains this date
                const parentSection = dateSection.closest('.brick.info-bares');
                
                // Find all category sections within this date's section
                const categorySections = parentSection.querySelectorAll('.content-bares > li');

                categorySections.forEach(section => {
                    if (section.querySelector('.fecha-bar')) return;

                    const category = section.querySelector('.box-medium')?.textContent || 'Categoría Desconocida';
                    const eventListItems = section.querySelectorAll('li');

                    eventListItems.forEach(eventItem => {
                        const eventLink = eventItem.querySelector('a');
                        const timeBar = eventItem.querySelector('.time-bar');

                        if (eventLink && timeBar) {
                            eventsByDate[currentDate].push({
                                categoria: category,
                                evento: eventLink.textContent.trim(),
                                canal: timeBar.childNodes[0].textContent.trim(),
                                hora: timeBar.querySelector('span')?.textContent || 'Hora no especificada'
                            });
                        }
                    });
                });
            });

            return eventsByDate;
        }

        function renderEventsTable(eventsByDate) {
            const container = document.getElementById('events-container');

            Object.entries(eventsByDate).forEach(([date, events]) => {
                if (events.length === 0) return;

                const dateTitle = document.createElement('div');
                dateTitle.className = 'table-title';
                dateTitle.textContent = date;
                container.appendChild(dateTitle);

                const table = document.createElement('table');
                const thead = document.createElement('thead');
                thead.innerHTML = `
                    <tr>
                        <th>Hora</th>
                        <th>Categoría</th>
                        <th>Evento</th>
                        <th>Canal</th>
                    </tr>
                `;
                table.appendChild(thead);

                const tbody = document.createElement('tbody');
                events.forEach(event => {
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>${event.hora}</td>
                        <td>${event.categoria}</td>
                        <td>${event.evento}</td>
                        <td>${event.canal}</td>
                    `;
                    tbody.appendChild(row);
                });

                table.appendChild(tbody);
                container.appendChild(table);
            });
        }

        async function fetchSportsEvents() {
            const proxyUrl = 'https://api.codetabs.com/v1/proxy/?quest=';
            const targetUrl = 'https://www.movistarplus.es/deportesendirectobar';
            
            try {
                const response = await fetch(proxyUrl + targetUrl);
                const htmlContent = await response.text();
                
                const events = parseEvents(htmlContent);
                renderEventsTable(events);
                
                document.getElementById('loading').style.display = 'none';
            } catch (error) {
                const errorDiv = document.getElementById('error');
                errorDiv.textContent = `Error: ${error.message}`;
                document.getElementById('loading').style.display = 'none';
            }
        }

        document.addEventListener('DOMContentLoaded', fetchSportsEvents);
    </script>
</body>
</html>