<div class="player" src=""></div>

    <script>
        // Fungsi untuk mendapatkan parameter dari query string
        function getQueryParam(param) {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(param);
        }

        // Ambil ID dari query string
        const id = getQueryParam('id');

        // Jika ID ada, lakukan permintaan ke API
        if (id) {
            const apiUrl = `https://dxymaqdjfrtyqwnjgzre.supabase.co/rest/v1/match_schedules?select=stream_urlm3u81&id=eq.${id}`;
            const headers = {
                'apikey': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImR4eW1hcWRqZnJ0eXF3bmpnenJlIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTM4ODk0NzgsImV4cCI6MjAyOTQ2NTQ3OH0.RLUggthuRLJOYU5EyB3ozf8TE0pXjeM7bExy8N8htBk',
            };

            fetch(apiUrl, { headers })
                .then(response => response.json())
                .then(data => {
                    if (data.length > 0) {
                        const streamUrl = data[0].stream_urlm3u81;
                        const player = document.querySelector('.player');
                        player.setAttribute('src', streamUrl);
                        initializePlayer(player);
                    } else {
                        console.error('No data found for the given ID.');
                    }
                })
                .catch(error => console.error('Error fetching data:', error));
        } else {
            console.error('ID parameter is missing in the query string.');
        }

        function initializePlayer(player) {
            const clapprConfig = {
                source: player.getAttribute("src"),
                autoPlay: player.getAttribute("auto") === "true",
                plugins: [HelloWorld, DashShakaPlayback, LevelSelector],
                levelSelectorConfig: {
                    title: 'Quality',
                    labels: {
                        3: 'HD',
                        2: 'High',
                        1: 'Med',
                        0: 'Low',
                    },
                    labelCallback: function (playbackLevel, customLabel) {
                        return customLabel + playbackLevel.level.height + 'p';
                    }
                },
                width: "100%",
                height: "100%",
                mute: false,
            };
            const clappr = new Clappr.Player(clapprConfig);
            clappr.attachTo(player);
        }

        var HelloWorld = Clappr.UIContainerPlugin.extend({
            bindEvents() {
                this.listenTo(this.container, Clappr.Events.CONTAINER_PLAY, this.render);
            },
            render: function() {
                this.$el.html('<img src="https://cys1d925b2.snarlgnats.top/fzbola22-icon.png" alt="Watermark" style="height: 17px; width: auto;" />');
                this.$el.css({
                    'position': 'absolute',
                    'width': 'auto',
                    'filter': 'drop-shadow(2px 2px 2px #000)',
                    'height': '17px',
                    'bottom': '5px',
                    'opacity': '0.75',
                    'left': '50%',
                    'transform': 'translateX(-50%)',
                });
                this.container.$el.append(this.$el);
                return this;
            }
        });
    </script>