<?php

require "../functions.php";

$query = "SELECT * FROM provider_permainan";

if (isset($_POST["cari"])) {
    $query = cari_permainan($_POST["nama_permainan"]);
    $data_permainan = $query;
    cek_waktu($data_permainan);
} else {
    $data_permainan = query($query);
    cek_waktu($data_permainan);
}

if (isset($_GET["Id"])) {
    foreach ($array_provider as $provider) {
        if ($_GET["Id"] == $provider["Provider"]) {
            $nama_provider = $provider["Nama_Provider"];
            $nama_gambar = $provider["Nama_Gambar"];
            $gambar_brand = $provider["Gambar_Brand"];
        }
    }
}

// Untuk Menghitung RTP Dari Terbesar Sampai Terkecil //

// $array_baru = [];
// $data_permainan_baru = [];

// foreach ($data_permainan as $data) {
//     $array_baru[] = $data["RTP"];
// }

// rsort($array_baru);

// foreach ($array_baru as $angka) {
//     foreach($data_permainan as $data) {
//         if (!in_array($data, $data_permainan_baru)) {
//             if ($angka == $data["RTP"]) {
//                 $data_permainan_baru[] = $data;
//             }
//         }
//     }
// }

// $data_permainan = $data_permainan_baru;

// -------------------------------------------------- //

?>

<?php if ($data_permainan != null) : ?>
    <div class="provider_rtp" id="provider_rtp">
        <div class="rtp_list">
        
        <?php foreach ($data_permainan as $data) : ?>
            <?php if ($_GET["Id"] == $data["Provider"]) : ?>
                <?php $pola = uraikan_teks_pola($data["Pola"]);  ?>
                <div class="rtp">
                    <div class="gambar_rtp">
                        <img src="<?php print $nama_gambar; ?><?php print $data["Nama_Gambar"]; ?>" alt="" class="gambar">
                        <p><?php print $data["Nama_Permainan"]; ?></p>
                    </div>

                    <div class="pola_rtp">
                        <div class="pola">
                            <div class="bagian_judul">
                                <p>Pola Slot</p>
                                <img src="https://img.viva88athenae.com/<?php print $gambar_brand ?>" alt="">
                            </div>

                            <div class="bagian_isi">
                                <table>
                                    <?php $i = 0; ?>
                                    <?php while ($i < 3) : ?>
                                        <tr>
                                            <td>
                                                <p class="judul"><?php print $pola[$i][0]; ?></p>
                                            </td>

                                            <?php if ($_GET["Id"] == "Pragmatic_Play") : ?>
                                                
                                                <td>
                                                    <?php if ($pola[$i][1] == "Off") : ?>
                                                        <p class="dot_merah">⍟</p>
                                                        <?php else : ?>
                                                            <p class="dot_hijau">⍟</p>
                                                    <?php endif; ?>
                                                </td>

                                                <td>
                                                    <?php if ($pola[$i][1] == "Off") : ?>
                                                        <p class="dot_hijau">⍟</p>
                                                        <?php else : ?>
                                                            <p class="dot_merah">⍟</p>
                                                    <?php endif; ?>
                                                </td>

                                                <td>
                                                    <?php if ($pola[0][1] == "Off") : ?>
                                                        <p class="dot_merah">⍟</p>
                                                        <?php else : ?>
                                                            <p class="dot_hijau">⍟</p>
                                                    <?php endif; ?>
                                                </td>

                                                <?php else : ?>
                                                    <td class="kecil">
                                                        <?php if ($pola[$i][1] == "Off") : ?>
                                                            <p class="dot_merah">Turbo</p><p class="dot_merah">⍟</p>
                                                            <?php else : ?>
                                                                <p class="dot_hijau">Turbo</p><p class="dot_hijau">⍟</p>
                                                        <?php endif; ?>
                                                    </td>
                                            <?php endif; ?>
                                        </tr>
                                        <?php $i++; ?>
                                    <?php endwhile; ?>
                                </table>
                            </div>

                            <div class="bagian_rtp">
                                <div class="waktu">
                                    <p>Jam: <?php print $data["Jam_Sebelum"]; ?> - <?php print $data["Jam_Sesudah"]; ?></p>
                                </div>

                                <div class="percent">
                                    <p><?php print $data["RTP"]; ?>%</p>
                                    <?php if ($data["RTP"] > 75 && $data["RTP"] < 99) : ?>
                                        <div class="percent_bar great" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="<?php print $data["RTP"]; ?>" style="width: <?php print $data["RTP"]; ?>%;"></div>
                                        <?php elseif ($data["RTP"] == 99) : ?>
                                            <div class="percent_bar super_great" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="<?php print $data["RTP"]; ?>" style="width: <?php print $data["RTP"]; ?>%;"></div>
                                        <?php else : ?>
                                            <div class="percent_bar medium" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="<?php print $data["RTP"]; ?>" style="width: <?php print $data["RTP"]; ?>%;"></div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>

        </div>
    </div>
    <?php else : ?>
        <div class="permainan_tidak_ada">
            <p>Permainan yang anda cari tidak tersedia, silahkan hubungi admin untuk menyediakan permainan yang anda cari, Terima Kasih.</p>
        </div>
<?php endif; ?>