<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/includes/api_helpers.php';
require_once dirname(__DIR__) . '/includes/uploads.php';

$db = gpc_db();
$rows = $db->query('SELECT image_path FROM banners WHERE active = 1 ORDER BY sort_order ASC, id ASC')->fetchAll();

$urls = [];
foreach ($rows as $r) {
    $path = gpc_upload_public_path((string) $r['image_path']);
    if ($path !== null) {
        $urls[] = gpc_public_url($path);
    }
}

header('Content-Type: text/html; charset=utf-8');
if ($urls === []) {
    echo '<html><body style="margin:0;background:#000"></body></html>';
    exit;
}

echo '<!DOCTYPE html><html><head><meta charset="utf-8"><style>
body{margin:0;background:#000;overflow:hidden}
img{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;display:none}
img.on{display:block}
</style></head><body>';

foreach ($urls as $i => $url) {
    $cls = $i === 0 ? ' class="on"' : '';
    echo '<img src="' . htmlspecialchars($url, ENT_QUOTES) . '" alt=""' . $cls . '>';
}

echo '<script>
var imgs=document.querySelectorAll("img"),i=0;
if(imgs.length>1){setInterval(function(){
  imgs[i].classList.remove("on");
  i=(i+1)%imgs.length;
  imgs[i].classList.add("on");
},5000);}
</script></body></html>';
