<?php
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
?>
<!DOCTYPE html>
<html>
<head>
    <title>Image and Video Slideshow</title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        img, video {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: fill;
        }
        .slide-indicator {
            display: inline-block;
            width: 10px;
            height: 10px;
            background-color: gray;
            border-radius: 50%;
            margin: 5px;
            cursor: pointer;
        }
        .active {
            background-color: white;
        }
    </style>
</head>
<body>
    <div id="slideshow"></div>
    <div id="slide-indicators"></div>

<script>
    var slideshow = document.getElementById("slideshow");

    fetch('qrcode.php')
        .then(response => response.json())
        .then(data => {
            var mediaUrls = data.map(obj => obj.AdUrl);
            loadSlides(mediaUrls);
        });

    function getMediaType(url) {
        var extension = url.split('.').pop();
        if (extension == "jpg" || extension == "jpeg" || extension == "png" || extension == "gif") {
            return "image";
        } else if (extension == "mp4") {
            return "video";
        } else {
            return null;
        }
    }

    function loadSlides(mediaUrls) {
        var i;
        for (i = 0; i < mediaUrls.length; i++) {
            var mediaType = getMediaType(mediaUrls[i]);
            var slideElement;
            if (mediaType == "image") {
                slideElement = document.createElement("img");
                slideElement.src = mediaUrls[i];
            } else if (mediaType == "video") {
                slideElement = document.createElement("video");
                slideElement.src = mediaUrls[i];
                slideElement.autoplay = true;
                slideElement.controls = false; // disable controls
                slideElement.muted = true; // mute video for autoplay
                slideElement.loop = false; // don't loop video
                slideElement.addEventListener('ended', function () {
                    setTimeout(showSlides, 0); // move to next slide when video ends
                });
            }
            slideshow.appendChild(slideElement);
        }
        showSlides();
    }

    var slideIndex = 0;

    function showSlides() {
        var i;
        var slides = slideshow.childNodes;
        for (i = 0; i < slides.length; i++) {
            if (slides[i].tagName == "IMG" || slides[i].tagName == "VIDEO") {
                slides[i].style.display = "none";
            }
        }
        slideIndex++;
        if (slideIndex > slides.length) {
            slideIndex = 1;
        }
        if (slides[slideIndex - 1].tagName == "IMG") {
            slides[slideIndex - 1].style.display = "block";
            setTimeout(showSlides, 4000); // Change slide every 8 seconds for images
        } else if (slides[slideIndex - 1].tagName == "VIDEO") {
            slides[slideIndex - 1].style.display = "block";
            slides[slideIndex - 1].play();
        }
    }
</script>

</body>
</html>
