<?php
if(!empty($_SESSION['admin_connect_id']))
{


 ?>
<h4 class="title" ><i class="fa fa-users "></i> Liste des utilisateurs  
<a href="index.php?p=add_user"  class="btn btn-sm btn-success pull-right">Ajouter</a>
 </h4>
        <div class="row">
      		<div class="col-lg-12">
                  <div class="form-panel">
                    <table id="dataTables" class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                           <th >Nom / prénom</th>
                                           <th >Nom d'utilisateur</th>
                                           <th >Type</th>
                                            <th >Date de création</th>
                                          
                                            <th style="width:30%;"> Actions</th>

                                        </tr>
                                    </thead>

                                    <tbody>

       <?php

       $req_sql="SELECT *,users.id as id_userr FROM users ORDER BY id_userr desc";
      $query_users=mysqli_query($con,$req_sql);

         while ($row_user = mysqli_fetch_array($query_users) ) {  ?>
         <tr>
         
          <td > <?php echo $row_user['nom']." ".$row_user['prenom'] ?> </td>
          <td > <?php echo $row_user['username']  ?> </td>
          <td > <?php echo $row_user['type']  ?> </td>
          <td > <?php // Forlater la date  
                     $date=date_create($row_user['created_at']);
                     echo date_format($date,"d/m/Y H:i");

           ?> </td>
      
          <td>
<?php  if($row_user['etat']=='1' ){?>
          <button class="btn btn-warning  btn-xs desact_user " id="<?php  echo $row_user['id_userr']; ?>"> Désactiver</button>
           <?php }else {  ?>
          <button class="btn btn-primary  btn-xs act_user" id="<?php  echo $row_user['id_userr']; ?>">
          Activer</button>

         <?php   } ?>
           <button type="button" class="btn btn-info btn-xs" data-toggle="modal"  data-target="#affiche<?php echo $row_user['id_userr'];?>"> Afficher</button>
           <a href="index.php?p=edit_user&id_user=<?php echo $row_user['id_userr'];?>" class="btn btn-success btn-xs"> Modifier</a>
    
 <button type="button" class="btn btn-danger btn-xs sup_user"  data="<?php echo $row_user['id_userr'];?>" >Supprimer</button>
 <div class="modal fade" id="affiche<?php echo $row_user['id_userr'];?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Utilisateur  "<?php echo $row_user["username"]; ?> "</h4>
      </div>
      <div class="modal-body">
      <table class="table table-striped table-bordered table-hover" >
             <tr><th colspan="2">
             <img src="assets/img_user/<?php echo $row_user["img"]; ?>" style="width: 50%; display: block; margin: 0 auto" />

             </th></tr>
             <tr><th>Nom </th> <td > <?php echo $row_user["nom"]; ?></td></tr>
             <tr><th>Prénom </th> <td > <?php echo $row_user["prenom"]; ?></td></tr>
             <tr><th>Username </th> <td > <?php echo $row_user["username"]; ?></td></tr>
             <tr><th>Type </th> <td > <?php echo $row_user["type"]; ?></td></tr>
              
             <tr><th>date d'ajout </th> <td > <?php  echo date_format($date,"d/m/Y H:i"); ?></td></tr>
      </table>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
      </div>
    </div>
  </div>
</div>
 </td>

          </tr>
          <?php  } ?>
     </tbody>
     </table>
                  </div>
      		</div>
      		</div>
          
         
<script type="text/javascript">
$(document).ready(function() {

$("#g_users").addClass("active");
document.title = 'Liste des utilisateurs';

$(".sup_user").click(function ()
{   var id = $(this).attr('data');

swal({
  title: ' Voulez vous vraiment supprimer cet enregistrement ?',
  text: "Vous ne serez pas capable de revenir à cela!",
  type: 'warning',
  width: 600,
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Oui, supprimez-le !',
  cancelButtonText:  'Non'
}).then((result) => {
  if (result.value) {
//if click oui
$.get("actions/fonctions.php?type=sup_user&id="+id,function()
    {    
       setTimeout(function() {
              location.reload();
               }, 2000);
    });

    swal(

      'Supprimé !',
      'L\'enregistrement a été supprimé.',
      'success'
    ).then(function(){ 
   location.reload();
   })
  


  }
})

});

 $(".desact_user").click(function ()
{   var id = $(this).attr('id');
$.get("actions/fonctions.php?type=desact_user&id="+id,function()
    {    location.reload();
    });
});

$(".act_user").click(function ()
{   var id = $(this).attr('id');
$.get("actions/fonctions.php?type=act_user&id="+id,function()
    {    location.reload();
    });
});


} );
      </script>
<?php } ?>