<link rel="stylesheet" href="<?= base_url('<link rel="stylesheet" 
	href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap.min.css">') ?>">
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="row">
        <h3 style="text-align:center;">Configuration du jeu</h3>
        <div class="col-md-8 col-sm-8 col-xs-12">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title"><?= $this->lang->line('Activer/Désactiver'); ?></h3>
                    </div>
                    <div class="box-body" style="overflow-x:auto;">

                        <?php

                        $sql2 = $pays;
                        if($sql2!=false){
                            $numrows=$sql2->num_rows();
                            $num = 0;
                            if ($numrows > 0) {
                                ?>
                                <table id="validated" class="table table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th align='center'>N°</th>
                                        <th><?= $this->lang->line('Pays'); ?></th>
                                        <th align='center'><?= $this->lang->line('Statut'); ?></th>
                                        <th align='center'><?= $this->lang->line('Activer'); ?></th>
                                        <th align='center'><?= $this->lang->line('Désactiver'); ?></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                <?php
                                    foreach($sql2->result() as $row) {
                                        $num ++;
                                        //$member_code = randomString1(10);
                                        $libelle_status = $this->lang->line('Inactive');
                                        if($row->etat==1){
                                            $libelle_status = $this->lang->line('Active');
                                        }
                                        echo "<tr>
                                                  <td align='center'>$num</td>
                                                  <td >$row->name</td>
                                                  <td align='center'>$libelle_status</td>";
                                        if($row->etat==0 or $row->etat==null) {
                                            echo "<td align='center'><input type='checkbox' name='activer' class='chk' value='".$row->id."'></td>
                                                  <td align='center'><input type='checkbox' name='desactiver' value='".$row->id."' disabled></td>";
                                        }else{
                                            echo "<td align='center'><input type='checkbox' name='activer' value='".$row->id."' disabled></td>
                                                  <td align='center'><input type='checkbox' name='desactiver' value='".$row->id."' class='unchk'></td>";
                                        }
                                                
                                    }
                                ?>
                                    </tbody>
                                </table>
                                    <button type="button" class="btn btn-success" id="valider"><?= $this->lang->line('Activer/Désactiver'); ?></button>
                                <?php

                            }

                            else {
                                echo $this->lang->line('Aucun pays');
                            }
                        }else {
                            echo $this->lang->line('Aucun pays');
                        }
                        ?>
                    </div>
                </div>
        </div>
        <div class="col-md-4 col-sm-4 col-xs-12">
          <div class="box">
            <div class="box-header">
              <h3 class="box-title">Gestion des niveaux</h3>
            </div>
            <div class="box-body">
            <?php
                    $result=$paliers;
                    if($result!=false){
                        $numrows=$result->num_rows();
                        $i=0;
                        if ($numrows > 0) { 
                            foreach($result->result() as $row) {
                                $i++;
                ?>
              <div class="form-group">
                <label><?= $row->libelle; ?>:</label>
                <div class="input-group">
                  <div class="input-group-addon">
					  <i class="fa fa-flag-checkered"></i>
                  </div>
                  <input type="number" id="palier<?= $i; ?>" value="<?= $row->valeur; ?>" class="form-control">
                </div>
              </div>
              <?php
                        }
                    }
                }
              ?>
              <button type="button" id="modif-palier" class="btn btn-success">Valider</button>
              
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->

        </div>
        </div>
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div >
                <div class="box box-default collapsed-box">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?= $this->session->userdata('current_game_country_name'); ?></h3>
                        <div class="box-tools pull-right">
                            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-plus"></i>
                            </button>
                        </div>
                    </div>
                    <div class="box-body">
                    <?php
                                $result=$game_pays;

                                $numrows=$result->num_rows();

                                if ($numrows > 0) { ?>
                                    <?php foreach($result->result() as $row) {
                                        if($row->id!=$this->session->userdata('current_game_country')){
                                            echo "<button class='change-country btn btn-block btn-social btn-instagram' value=".$row->id.">".$row->name."</button>";
                                        }
                                    } ?>
                                <?php }else{
                                    echo "Aucun pays";
                                }
                                ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
        <div class="col-md-3 col-sm-6 col-xs-12">
            <div class="info-box">
                <span class="info-box-icon bg-aqua"><?= $q4 ?></i></span>

                <div class="info-box-content">
                <span class="info-box-text"><?= $this->lang->line('Utilisateurs'); ?></span>
                <span class="info-box-number"><?= $quota4; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
            </div>
            <!-- /.col -->
            <div class="col-md-3 col-sm-6 col-xs-12">
            <div class="info-box">
                <span class="info-box-icon bg-green"><?= $q3 ?></span>

                <div class="info-box-content">
                <span class="info-box-text"><?= $this->lang->line('Utilisateurs'); ?></span>
                <span class="info-box-number"><?= $quota3; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
            </div>
            <!-- /.col -->
            <div class="col-md-3 col-sm-6 col-xs-12">
            <div class="info-box">
                <span class="info-box-icon bg-yellow"><?= $q2 ?></i></span>

                <div class="info-box-content">
                <span class="info-box-text"><?= $this->lang->line('Utilisateurs'); ?></span>
                <span class="info-box-number"><?= $quota2; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
            </div>
            <!-- /.col -->
            <div class="col-md-3 col-sm-6 col-xs-12">
            <div class="info-box">
                <span class="info-box-icon bg-red"><?= $q1 ?></span>

                <div class="info-box-content">
                <span class="info-box-text"><?= $this->lang->line('Utilisateurs'); ?></span>
                <span class="info-box-number"><?= $quota1; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
            </div>
        </div>
        <div class="row">
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header">
              <h3 class="box-title">Utilisateurs ayant atteint les quotas</h3>

            </div>
            <!-- /.box-header -->
            <div class="box-body table-responsive no-padding" style="overflow-x:auto;">
              <table class="table table-hover">
                
                <?php
                    $result=$list;
                    if($result!=false){
                        $numrows=$result->num_rows();
                        $i=0;
                        if ($numrows > 0) { 
                ?>
                <tr>
                  <th>N°</th>
                  <th>Nom</th>
                  <th>Téléphone</th>
                  <th>Email</th>
                  <th>Résultat</th>
                </tr>
                        <?php foreach($result->result() as $row) {
                            $i++;
                            echo "<tr>
                                    <td>".$i."</td>
                                    <td>".$row->lastname."</td>
                                    <td>".$row->phone."</td>
                                    <td>".$row->email."</td>";
                                    if($row->total>=$q4){
                                        echo "<td><span class='label label-primary'>".$row->total." utilisateurs</span></td>";
                                    }elseif($row->total>=$q3 && $row->total<$q4){
                                        echo "<td><span class='label label-success'>".$row->total." utilisateurs</span></td>";
                                    }elseif($row->total>=$q2 && $row->total<$q3){
                                        echo "<td><span class='label label-warning'>".$row->total." utilisateurs</span></td>";
                                    }elseif($row->total>=$q1 && $row->total<$q2){
                                        echo "<td><span class='label label-danger'>".$row->total." utilisateurs</span></td>";
                                    }
                                    
                            echo "</tr>";
                        } ?>
                        <?php }else{
                        echo "Aucun utilisateur";
                        }
                    }else{
                        echo "Aucun utilisateur";
                    }
                    ?>
                
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
      </div>
    </section>
</div>
<!-- jQuery 2.2.4 -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<!-- Bootstrap 3.3.7 -->
<script src="<?= base_url('bower_components/bootstrap/dist/js/bootstrap.min.js') ?>"></script>
<!-- DataTables -->
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap.min.js"></script>
<!-- Slimscroll -->
<script src="<?= base_url('bower_components/jquery-slimscroll/jquery.slimscroll.min.js') ?>"></script>
<!-- FastClick -->
<script src="<?= base_url('bower_components/fastclick/lib/fastclick.js') ?>"></script>
<!-- AdminLTE App -->
<script src="<?= base_url('dist/js/adminlte.min.js') ?>"></script>
<!-- AdminLTE for demo purposes -->
<script src="<?= base_url('dist/js/demo.js') ?>"></script>

<script>
    $(function() {
        $('#validated').DataTable();
    });

    $('#modif-palier').click(function(){
        var n1 = $('#palier1').val();
        var n2 = $('#palier2').val();
        var n3 = $('#palier3').val();
        var n4 = $('#palier4').val();
        $.ajax({
                    url:'<?= base_url('Gestion/modifierPalier')?>',
                    type: 'post',
                    data: {n1: n1,n2:n2,n3:n3,n4:n4},
                    dataType: 'json',
                    success: function(data){
                        if(data==='completed'){
                            alert('Les valeurs des paliers ont été modifiés');
                            location.reload(true);
                        }else{
                            alert("Une erreur s'est produite");
                            location.reload(true);
                        }
                    }
                });
    });

    $('.change-country').click(function(){
        var id_pays = $(this).val();
        $.post("<?= base_url('Gestion/change_country')?>",
            {
                pays: id_pays
            },
            function(data, status){
                console.log(typeof data);
                if(data==1){
                    window.location.replace("<?= base_url('Gestion/game')?>");
                }else{
                    alert("Une erreur s'est produite");
                }
            });
    });

</script>
<script>
    $(document).ready(function(){

        var id_ville = 0;

        $("#valider").click(function() {
            activeGame();
        });

        function activeGame(){

            var chkArray = [];
            $(".chk:checked").each(function() {
                chkArray.push($(this).val());
            });
            var selected;
            selected = chkArray.join(',') ;

            var unchkArray = [];
            $(".unchk:checked").each(function() {
                unchkArray.push($(this).val());
            });
            var selectedForDisable;
            selectedForDisable = unchkArray.join(',') ;

            if(selected.length > 0 && selectedForDisable.length === 0){
                $.ajax({
                    url:'<?= base_url('Gestion/activerGame')?>',
                    type: 'post',
                    data: {pays: chkArray},
                    dataType: 'json',
                    success: function(data){
                        if(data==='completed'){
                            alert('Le jeu a été activé pour ce(s) pays');
                            location.reload(true);
                        }else{
                            alert("Une erreur s'est produite");
                            location.reload(true);
                        }
                    }
                });
            } else if (selectedForDisable.length > 0 && selected.length === 0){
                $.ajax({
                    url:'<?= base_url('Gestion/desactiverGame')?>',
                    type: 'post',
                    data: {pays: unchkArray},
                    dataType: 'json',
                    success: function(data){
                        if(data==='completed'){
                            alert('Le jeu a été désactivé pour ce(s) pays');
                            location.reload(true);
                        }else{
                            alert("Une erreur s'est produite");
                            location.reload(true);
                        }
                    }
                });
            } else if (selectedForDisable.length > 0 && selected.length > 0){
                $.ajax({
                    url:'<?= base_url('Gestion/activer_desactiverGame')?>',
                    type: 'post',
                    data: {dpays: unchkArray,apays: chkArray},
                    dataType: 'json',
                    success: function(data){
                        if(data==='completed'){
                            alert('Le jeu a été activé/désactivé pour ce(s) pays');
                            location.reload(true);
                        }else{
                            alert("Une erreur s'est produite");
                            location.reload(true);
                        }
                    }
                });
            }else{
                alert("Veuillez cocher des pays");
            }
            }

    });
</script>
