<?php
require_once 'koneksi.php';
require_once 'classes/APICONFIG.php';

if (isset($_GET['provider'])) {

    header('Content-Type: application/json');

    $provider = mysqli_real_escape_string($koneksi, $_GET['provider']);

    $query = mysqli_query($koneksi, "
        SELECT gameid, gamename, image 
        FROM tb_gamelist 
        WHERE provider = '$provider'
        AND datatype = 'LC'
    ");

    $games = [];

    while ($row = mysqli_fetch_assoc($query)) {
        $games[] = [
            "game_code" => $row['gameid'],
            "game_name" => $row['gamename'],
            "banner"    => $row['image']
        ];
    }

    if (count($games) > 0) {
        echo json_encode([
            "status" => 1,
            "games"  => $games
        ]);
    } else {
        echo json_encode([
            "status" => 0,
            "message" => "Game tidak ditemukan"
        ]);
    }

    exit;
}

include_once 'header.php';
?>

<section class="relative bg-[#000134] min-h-screen">
    <div class="container mx-auto p-3 lg:pb-8 relative z-10">
        <nav class="flex mb-1 lg:mb-2">
            <ol class="flex items-center pb-1 overflow-x-scroll whitespace-nowrap opacity-scroll">
                <li class="inline-flex items-end pr-1">
                    <a class="text-xs border-b border-transparent hover:lg:border-primary transition-all duration-200 ease-in-out" href="<?php echo $alamat_website . 'home'; ?>">Home</a>
                </li>
                <li class="inline-flex items-end pr-1 group">
                    <div class="flex items-center">
                        <svg width="17" height="17" viewBox="0 0 24 24" fill="var(--base)" xmlns="http://www.w3.org/2000/svg">
                            <path d="m15 12 .354-.354.353.354-.353.354L15 12ZM9.354 5.646l6 6-.708.708-6-6 .708-.708Zm6 6.708-6 6-.708-.708 6-6 .708.708Z" fill="var(--base)"></path>
                        </svg>
                        <a class="text-xs pl-1 border-b border-transparent hover:lg:border-primary transition-all duration-200 ease-in-out" href="casino">Casino</a>
                    </div>
                </li>
            </ol>
        </nav>

        <div class="flex flex-wrap -mx-3 mt-3">
            <div class="flex flex-wrap -mx-2 mt-4 lg:mt-8">
                <!-- Pragmatic Casino -->
                <div class="w-1/3 sm:w-1/4 lg:w-1/6 px-[6px] lg:px-3 mb-3 lg:mb-4">
                    <button class="relative pb-3 w-full group overflow-hidden" onclick="fetchGames('PP_LIVE_PRO')">
                        <div class="w-full rounded-xl lg:rounded-2xl">
                            <figure class="relative overflow-hidden rounded-xl lg:rounded-2xl">
                                <img alt="Pragmatic Casino" src="https://cdn.databerjalan.com/assets/images/categories/v3/Pragmatic-Play-Casino.png" class="w-full group-hover:lg:scale-110 transition-all duration-300 ease-in-out">
                            </figure>
                        </div>
                    </button>
                </div>

                <!-- Evo Casino -->
                <div class="w-1/3 sm:w-1/4 lg:w-1/6 px-[6px] lg:px-3 mb-3 lg:mb-4">
                    <button class="relative pb-3 w-full group overflow-hidden" onclick="fetchGames('EVOLUTION')">
                        <div class="w-full rounded-xl lg:rounded-2xl">
                            <figure class="relative overflow-hidden rounded-xl lg:rounded-2xl">
                                <img alt="Evo Casino" src="https://cdn.databerjalan.com/assets/images/categories/v3/evo.png" class="w-full group-hover:lg:scale-110 transition-all duration-300 ease-in-out">
                            </figure>
                        </div>
                    </button>
                </div>
            </div>
        </div>

        <!-- Tempat Menampilkan Gambar Games -->
        <div id="gamesContainer" class="flex flex-wrap -mx-3 mt-6"></div>
    </div>
</section>

<script>
function fetchGames(providerCode) {
    fetch('?provider=' + providerCode) // Memanggil halaman ini sendiri dengan parameter
    .then(response => response.json())
    .then(data => {
        const gamesContainer = document.getElementById('gamesContainer');
        gamesContainer.innerHTML = ''; // Bersihkan konten sebelumnya

        if (data.status === 1) {
            data.games.forEach(game => {
                const gameDiv = document.createElement('div');
                gameDiv.classList.add('w-1/3', 'sm:w-1/4', 'lg:w-1/6', 'px-[6px]', 'lg:px-3', 'mb-3', 'lg:mb-4');

                const img = document.createElement('img');
                img.src = game.banner;
                img.alt = game.game_name;
                img.classList.add('w-full', 'rounded-xl', 'lg:rounded-2xl', 'transition-all', 'duration-300', 'ease-in-out');
                img.style.cursor = 'pointer';

                // Klik gambar game -> Kirim game_code & provider_code ke Playgame.php
                img.onclick = function() {
                    window.location.href = 'PlayGame.php?game_code=' + game.game_code + '&provider_code=' + providerCode; 
                };

                gameDiv.appendChild(img);
                gamesContainer.appendChild(gameDiv);
            });
        } else {
            gamesContainer.innerHTML = '<p class="text-white text-center w-full">Gagal mengambil data games.</p>';
        }
    })
    .catch(error => console.error('Error fetching games:', error));
}
</script>

<?php include_once 'footer.php'; ?>
