<?php
// 2026/api/banner_webview_rev.php

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$jsonPath = "db/banner_rev_" . $id . ".json";
$banners = [];

if ($id > 0 && file_exists($jsonPath)) {
    // Decodifica o array de banners do JSON
    $banners = json_decode(file_get_contents($jsonPath), true) ?: [];
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <style>
        /* RESET TOTAL PARA WEBVIEW */
        * { 
            margin: 0; 
            padding: 0; 
            box-sizing: border-box; 
            -webkit-touch-callout: none; 
            -webkit-user-select: none; 
            user-select: none; 
        }

        html, body {
            width: 100%;
            height: 100%;
            background-color: transparent !important;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            position: fixed;
        }

        /* CONTAINER DO SLIDER */
        .slider-container {
            width: 100%;
            height: 100%;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
            overflow: hidden;
        }

        /* ESTILO DA IMAGEM HORIZONTAL */
        .banner-img {
            position: absolute;
            width: 100%;
            height: 100%;
            /* object-fit: cover garante que preencha o espaço horizontal da webview */
            object-fit: cover; 
            opacity: 0;
            transition: opacity 1.2s ease-in-out; /* Transição suave de fade */
            pointer-events: none;
        }

        /* CLASSE PARA EXIBIR O BANNER ATIVO */
        .banner-img.active {
            opacity: 1;
        }
    </style>
</head>
<body>

    <div class="slider-container" id="slider">
        <?php if (!empty($banners)): ?>
            <?php foreach ($banners as $index => $b): ?>
                <img src="<?php echo htmlspecialchars($b['url']); ?>?v=<?php echo time(); ?>" 
                     class="banner-img <?php echo ($index === 0) ? 'active' : ''; ?>" 
                     alt="Banner">
            <?php endforeach; ?>
        <?php else: ?>
            <?php endif; ?>
    </div>

    <script>
        const images = document.querySelectorAll('.banner-img');
        let currentIndex = 0;

        if (images.length > 1) {
            setInterval(() => {
                // Remove a classe ativa da imagem atual
                images[currentIndex].classList.remove('active');
                
                // Vai para a próxima imagem (volta ao zero no final)
                currentIndex = (currentIndex + 1) % images.length;
                
                // Adiciona a classe ativa na nova imagem
                images[currentIndex].classList.add('active');
            }, 5000); // 5 segundos de intervalo
        }
    </script>

</body>
</html>
