<?php
include 'template/header.php';
?>

<?php if ($mobile): ?>
    <div style="margin-top: 50px;">
        <div>
            <img src="https://rajabotak.dataklmsad902.site/asset/images/banner-game-2.jpg?s=6ad8d77b69c3661f8ca7458816992e23"
                style="width: 100%;">
        </div>
        <?php include "template/menu-outer.php" ?>

        <?php if (!isset($_GET['s'])): ?>
            <div style="width: 95%; margin: 0 auto 20px auto;">
                <div
                    style="font-family: Teko-SemiBold; text-align: center; font-size: 24px; color: transparent !important; -webkit-background-clip: text; background-image: radial-gradient(ellipse farthest-corner at right bottom, #FEDB37 0%, #FDB931 8%, #9f7928 30%, #8A6E2F 40%, transparent 80%), radial-gradient(ellipse farthest-corner at left top, #e99c05 0%, #f4ae00 8%, #f8bf33 25%, #f8ce19 62.5%, #ffdc02 100%); padding: 10px 0;">
                    Pilih Game
                    <hr
                        style="background: linear-gradient(#000 0 0) padding-box, linear-gradient(to left, #FEDB37, #8A6E2F) border-box; border-top: 2px solid transparent; margin: 0 auto; width: 70px;">
                </div>

                <ul style="list-style-type: none; padding: 0; margin-bottom: 0; display: flex; flex-wrap: wrap;">
                    <?php
                    $q = mysqli_query($koneksi, "SELECT * FROM tb_provider WHERE provider_type = 'LC'");

                    while ($data = mysqli_fetch_assoc($q)):
                        $providerName = htmlspecialchars($data['provider_name']);
                        $providerImg = htmlspecialchars($data['provider_image']);
                        $slug = htmlspecialchars($data['slug']);
                        $status = $data['status'];
                        ?>
                        <li <?php if ($status == 1): ?> onclick="window.location='?s=<?= $slug ?>'" <?php else: ?>
                                onclick="alert('Provider ini sedang Maintenance!');" <?php endif; ?>
                            style="display: inline-block; width: 50%; padding: 0 5px;">
                            <img src="<?= $providerImg ?>" alt="<?= $providerName ?>"
                                onerror="this.src='https://via.placeholder.com/300x150/667eea/ffffff?text=<?= urlencode($providerName) ?>'"
                                style="width: 100%; margin-bottom: 6px;">
                        </li>
                    <?php endwhile; ?>
                </ul>
            </div>
        <?php else:
            $slug = $_GET['s'];
            $provider = mysqli_query($koneksi, "SELECT * FROM tb_provider where UPPER(slug) = UPPER('$slug')");
            $data_pr = mysqli_fetch_assoc($provider) ?>
            <div style="background-color:var(--secondary-color);">
                <div style="width: 90%; margin: 0 auto; padding: 10px 0;">
                    <div style="display: flex; padding: 4px 0 4px 10px;">
                        <div style="color: var(--primary-color); font-family: Teko-SemiBold; font-size: 20px; margin: auto auto auto 0;">
                            <?= htmlspecialchars(strtoupper($data_pr['provider_name'])); ?>
                        </div>

                        <div style="margin: auto 0 auto auto; position: relative; width: 45%;">
                            <i class="fa fa-search" aria-hidden="true"
                                style="position: absolute; top: 10px; left: 15px; height: 18px; width: 18px; margin: 0; color: var(--primary-color);"></i>
                            <input type="text" name="search" class="search" placeholder="Cari Permainan"
                                style="font-family: Poppins-Regular; font-size: 12px; width: 100%; background-color: transparent; color: #fff; border: 1px solid var(--primary-color); border-radius: 25px; padding: 6px 15px 6px 35px;">
                        </div>
                    </div>

                    <div>
                        <ul id="game_list"
                            style="list-style-type: none; padding: 20px 0 0 0; margin-bottom: 0; display: flex; flex-wrap: wrap;">
                            <?php
                            $data_game = mysqli_query($koneksi, "SELECT * FROM tb_game where UPPER(provider_name) = UPPER('$data_pr[provider_name]') ORDER BY (seq = 0), seq ASC");
                            if (mysqli_num_rows($data_game) > 0) {
                                while ($game = mysqli_fetch_assoc($data_game)) {
                                    $game_name = htmlspecialchars($game['game_name']);
                                    $game_code = htmlspecialchars($game['game_code']);
                                    $provider_code = htmlspecialchars($game['game_provider']);
                                    $image = htmlspecialchars($game['game_image']);
                                    $slug = htmlspecialchars($game['provider_name']);
                                    ?>
                                    <li onclick="window.open('play.php?game=<?= $game_code ?>&provider=<?= $provider_code ?>','_blank')"
                                        style="display: inline-block; padding: 0 8px; width: 33.3%; margin-bottom: 15px;">
                                        <div style="background-color: var(--primary-color); padding: 5px; border-radius: 4px;">
                                            <div style="width: 100%; height: 90px; position: relative; overflow: hidden;">
                                                <img src="<?= $image ?>" alt="<?= $game_name ?>"
                                                    style="position: absolute; box-sizing: border-box; border: medium; margin: auto; display: block; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;">
                                            </div>
                                            <div
                                                style="margin-top: 6px; font-size: 10px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
                                                <?= $game_name ?>
                                            </div>
                                        </div>
                                    </li>
                                    <?php
                                }
                            } ?>
                        </ul>
                    </div>
                </div>
            </div>
        <?php endif; ?>

    </div>
<?php else: ?>
    <div style="margin-top: 170px;">
        <div class="background-top" style="padding: 30px 0 50px 0;">

           

            <?php if (!isset($_GET['s'])): ?>

                <div style="width: 95%; margin: 0 auto; max-width: 1280px; padding-top: 20px;">
                    <div
                        style="margin-bottom: 20px; color: var(--primary-color); font-family: Teko-SemiBold; font-size: 24px; text-align: center;">
                        LIVE CASINO
                    </div>

                    <ul style="list-style-type: none; padding: 20px 0 0 0; margin: 0; display: flex; flex-wrap: wrap;">

                        <?php
                        // tampilkan provider slot
                        $providers = mysqli_query($koneksi, "SELECT * FROM tb_provider WHERE provider_type = 'LC'");

                        while ($data = mysqli_fetch_assoc($providers)):
                            $providerName = htmlspecialchars($data['provider_name']);
                            $providerImg = htmlspecialchars($data['provider_image']);
                            $slug = htmlspecialchars($data['slug']);
                            ?>
                            <li onclick="window.location='?s=<?= $slug ?>'"
                                style="display: inline-block; padding: 0 10px; width: 20%; margin-bottom: 20px; cursor: pointer;">
                                <img src="<?= $providerImg ?>" alt="<?= $providerName ?>" style="width: 100%; border-radius: 6px;">
                            </li>
                        <?php endwhile; ?>

                    </ul>
                </div>

                <!-- ========================================================= -->
                <!-- ======================= LIST GAME ======================== -->
                <!-- ========================================================= -->

            <?php else: ?>

                <?php
                // amankan slug
                $slug = mysqli_real_escape_string($koneksi, $_GET['s']);

                // ambil data provider
                $provider = mysqli_query(
                    $koneksi,
                    "SELECT * FROM tb_provider WHERE UPPER(slug) = UPPER('$slug')"
                );

                $data_pr = mysqli_fetch_assoc($provider);

                // jika slug tidak ditemukan
                if (!$data_pr) {
                    echo "<div style='text-align:center; color:#fff; padding:40px;'>Provider tidak ditemukan.</div>";
                    include 'template/footer.php';
                    exit;
                }

                $providerName = mysqli_real_escape_string($koneksi, $data_pr['provider_name']);
                ?>

                <div style="background-color:var(--secondary-color);">
                    <div style="width: 90%; margin: 0 auto; padding: 10px 0;">

                        <!-- Header Provider -->
                        <div style="display: flex; padding: 4px 0;">
                            <div style="color: var(--primary-color); font-family: Teko-SemiBold; font-size: 20px;">
                                <?= htmlspecialchars(strtoupper($data_pr['provider_name'])) ?>
                            </div>

                            <div style="margin: auto 0 auto auto; position: relative; width: 20%;">
                                <i class="fa fa-search" aria-hidden="true"
                                    style="position: absolute; top: 10px; left: 15px; color: var(--primary-color);"></i>
                                <input type="text" class="search" placeholder="Cari Permainan"
                                    style="font-family: Poppins-Regular; font-size: 14px; width: 100%; background-color: transparent; color: #fff; border: 1px solid var(--primary-color); border-radius: 25px; padding: 6px 15px 6px 35px;">
                            </div>
                        </div>

                        <!-- Daftar Game -->
                        <ul id="game_list"
                            style="list-style-type: none; padding: 20px 0 0 0; margin-bottom: 0; display: flex; flex-wrap: wrap;">
                            
                            <?php
                            $games = mysqli_query(
                                $koneksi,
                                "SELECT * FROM tb_game 
                             WHERE UPPER(provider_name) = UPPER('$providerName')
                             ORDER BY (seq = 0), seq ASC"
                            );

                            while ($game = mysqli_fetch_assoc($games)):
                                $game_name = htmlspecialchars($game['game_name']);
                                $game_code = htmlspecialchars($game['game_code']);
                                $provider_code = htmlspecialchars($game['game_provider']);
                                $image = htmlspecialchars($game['game_image']);
                                ?>
                                <li onclick="window.open('play.php?game=<?= $game_code ?>&provider=<?= $provider_code ?>','_blank')"
                                    style="display: inline-block; padding: 0 8px; width: 14.2%; margin-bottom: 15px;">
                                    <div style="background-color: var(--primary-color); padding: 5px; border-radius: 4px;">
                                        <div style="width: 100%; height: 160px; position: relative; overflow: hidden;">
                                            <img src="<?= $image ?>" alt="<?= $game_name ?>"
                                                style="position: absolute; box-sizing: border-box; border: medium; margin: auto; display: block; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;">
                                        </div>
                                        <div
                                            style="margin-top: 6px; font-size: 12px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
                                            <?= $game_name ?>
                                        </div>
                                    </div>
                                </li>
                            <?php endwhile; ?>

                        </ul>

                    </div>
                </div>

            <?php endif; ?>

        </div>
    </div>
<?php endif; ?>

<script>
document.addEventListener("DOMContentLoaded", function () {
    const searchInput = document.querySelector(".search");
    const gameList = document.querySelectorAll("#game_list li");

    if (searchInput) {
        searchInput.addEventListener("keyup", function () {
            const keyword = this.value.toLowerCase();

            gameList.forEach(function (item) {
                const title = item.innerText.toLowerCase();
                item.style.display = title.includes(keyword) ? "inline-block" : "none";
            });
        });
    }
});
</script>

<?php include 'template/footer.php' ?>