<style>
    body {
        background-size: cover;
        background-position: center;
        background-attachment: fixed;
        /* background-image: url(<?php echo $url; ?>/img/bg_juegos.jpg); */
        background-color: #939597;
    }
</style>



<div class="col-md-12">

    <div class="container">

        <?php echo $rersp; ?>

        <div class="row">

            <div class="col-md-12" align="center">
                <h1 style="color: #ffffff;">RANKING</h1>

            </div>

            <div class="col-md-6" align="center" style="color: #fff; ">

                <h3>Crucigrama</h3>
                <hr>
                <div class="table-responsive" style="background-color: #fff; border-radius: 12px; overflow: hidden; max-width: 800px; padding: 5px 30px;">


                    <table class="table">
                        <thead>
                            <tr>
                                <th scope="col">Posición</th>
                                <th scope="col">Participante</th>
                                <th scope="col">Puntaje</th>
                            </tr>
                        </thead>
                        <?php
                        $arrayRanking = array();

                        $count = 1;
                        $queryR1 = mysqli_query($connect, "SELECT SUM(porcentaje), SUM(tiempo), id_asistente  FROM Retos WHERE tipo_juego = 1 GROUP BY id_asistente  ");
                        while ($dataR1 = mysqli_fetch_array($queryR1)) {

                            $queryAsi = mysqli_query($connect, "SELECT * FROM Asistentes WHERE id = '" . $dataR1['id_asistente'] . "' ");
                            $dataAsi = mysqli_fetch_array($queryAsi);

                            $puntaje = ($dataR1["SUM(porcentaje)"] * 20 / ($dataR1["SUM(tiempo)"] / 3));

                            array_push($arrayRanking, array(
                                "id_asistente" => $dataR1['id_asistente'],
                                "asistente" => ($dataAsi["nombre"] . ' ' . $dataAsi["apellidos"]),
                                "puntaje" => $puntaje

                            ));

                            $count++;
                        }


                        usort($arrayRanking, 'sort_by_orden');
                        function sort_by_orden($a, $b)
                        {
                            return $b['puntaje'] - $a['puntaje'];
                        }

                        $count = 1;
                        ?>
                        <tbody>

                            <?php
                            foreach ($arrayRanking as $ranking) {
                                echo '
                            <tr>
                                <td>' . $count . '</td>
                                <td>' . $ranking["asistente"] . '</td>
                                <td>' . round($ranking["puntaje"]) . ' pts.</td>
                            <tr>
                            ';
                                $count++;
                            }

                            ?>

                        </tbody>

                    </table>
                </div>




            </div>

            <div class="col-md-6" align="center" style="color: #ffffff;">

                <h3>Busca la frase</h3>
                <hr>
                <div class="table-responsive" style="background-color: #fff; border-radius: 12px; overflow: hidden; max-width: 800px; padding: 5px 30px;">
                    <table class="table">
                        <tr>
                            <th scope="col">Posición</th>
                            <th scope="col">Participante</th>
                            <th scope="col">Puntaje</th>
                        </tr>
                        <?php
                        $arrayRanking = array();

                        $count = 1;
                        $queryR1 = mysqli_query($connect, "SELECT SUM(porcentaje), SUM(tiempo), id_asistente  FROM Retos WHERE tipo_juego = 2 GROUP BY id_asistente  ");
                        while ($dataR1 = mysqli_fetch_array($queryR1)) {

                            $queryAsi = mysqli_query($connect, "SELECT * FROM Asistentes WHERE id = '" . $dataR1['id_asistente'] . "'");
                            $dataAsi = mysqli_fetch_array($queryAsi);

                            $puntaje = ($dataR1["SUM(porcentaje)"] * 10 / ($dataR1["SUM(tiempo)"] / 3));

                            array_push($arrayRanking, array(
                                "id_asistente" => $dataR1['id_asistente'],
                                "asistente" => ($dataAsi["nombre"] . ' ' . $dataAsi["apellidos"]),
                                "puntaje" => $puntaje

                            ));

                            $count++;
                        }


                        usort($arrayRanking, 'sort_by_orden');


                        $count = 1;
                        foreach ($arrayRanking as $ranking) {
                            echo '
                            <tr>
                                <td>' . $count . '</td>
                                <td>' . $ranking["asistente"] . '</td>
                                <td>' . round($ranking["puntaje"]) . ' pts.</td>
                            <tr>
                            ';
                            $count++;
                        }

                        ?>

                    </table>
                </div>



            </div>

            <div class="col-md-12 paginas" align="center" id="ranking" style="display: none; ">

                <h3 style=" color: #ffffff; margin-top: 20px;">Trivia</h3>

                <!-- <div align="center">
                    <img src="<?php echo $url; ?>/img/img_wd/bt_back.png" width="130" style="padding: 10px" onClick="Home()" class="bt_interface">
                </div> -->

                <div id="cont_ranking" style="background-color: #fff; border-radius: 12px; overflow: hidden; max-width: 800px; padding: 5px 30px;">
                </div>


            </div>

            <a href="/stand/5" class="btn btn_juegos">Volver</a>
        </div>

    </div>

</div>

<script>
    var api = '<?php echo $api; ?>';

    function RankingGeneral() {
        $("#ranking").show();

        jQuery.ajax({
                url: api + "ranking_desafio.php",
                type: 'post',
                data: {
                    id: 0
                },
            }).done(function(resp) {
                $("#cont_ranking").html(resp);
            })
            .fail(function(resp) {
                console.log(resp);
            })
            .always(function(resp) {});
    }

    RankingGeneral();
</script>