<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Conexión corregida a la base en la raíz
$db = new SQLite3(dirname(__DIR__) . '/ibo_panel.db');

$query = "SELECT url FROM ads";
$results = $db->query($query);

$imageUrls = [];
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
    $imageUrls[] = $row['url'];
}

// HTML y JS idénticos...
$html = '<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Slideshow Fullscreen</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        html, body {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }

        body {
            background-color: transparent;
        }

        .slideshow-container {
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            background-color: transparent;
        }

        .slideshow-image {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            opacity: 0;
            transition: opacity 1s ease;
        }

        .slideshow-image.active {
            opacity: 1;
        }

        .indicator-container {
            position: fixed;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 100;
        }

        .indicator {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: rgba(150, 150, 150, 0.5);
            margin: 0 5px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .indicator.active {
            background-color: rgba(255, 255, 255, 0.9);
            transform: scale(1.2);
        }

        .indicator:hover {
            background-color: rgba(200, 200, 200, 0.8);
        }
    </style>
</head>
<body>';

$html .= '<div class="slideshow-container">';
foreach ($imageUrls as $index => $imageUrl) {
    $html .= "<img class=\"slideshow-image" . ($index === 0 ? ' active' : '') . "\" src=\"$imageUrl\" alt=\"Slideshow image $index\">";
}
$html .= '<div class="indicator-container">';
foreach ($imageUrls as $index => $imageUrl) {
    $html .= "<div class=\"indicator" . ($index === 0 ? ' active' : '') . "\" data-index=\"$index\"></div>";
}
$html .= '</div>';
$html .= '</div>';

$html .= '<script>
    document.addEventListener("DOMContentLoaded", function() {
        const slideshowImages = document.querySelectorAll(".slideshow-image");
        const indicators = document.querySelectorAll(".indicator");
        let currentSlide = 0;
        const transitionInterval = 5000;
        let slideshowInterval;

        function startSlideshow() {
            slideshowInterval = setInterval(nextSlide, transitionInterval);
        }

        function nextSlide() {
            currentSlide = (currentSlide + 1) % slideshowImages.length;
            updateSlide(currentSlide);
        }

        function updateSlide(slideIndex) {
            slideshowImages.forEach((image, index) => {
                image.classList.toggle("active", index === slideIndex);
            });

            indicators.forEach((indicator, index) => {
                indicator.classList.toggle("active", index === slideIndex);
            });
        }

        indicators.forEach(indicator => {
            indicator.addEventListener("click", function() {
                const newIndex = parseInt(this.getAttribute("data-index"));
                currentSlide = newIndex;
                updateSlide(currentSlide);
                clearInterval(slideshowInterval);
                startSlideshow();
            });
        });

        startSlideshow();

        document.querySelector(".slideshow-container").addEventListener("mouseenter", () => {
            clearInterval(slideshowInterval);
        });

        document.querySelector(".slideshow-container").addEventListener("mouseleave", () => {
            startSlideshow();
        });
    });
</script>';

$html .= '</body></html>';

echo $html;
?>
