<?php
if(!empty($_SESSION['user_connect_id']))
{
    
      
 if(isset($_GET['id_consult'])){


      $id_consult=$_GET['id_consult'];
 
 ?>

 
<a href="index.php?p=add_ordonnance&id_consult=<?php echo $id_consult;?>"  class="btn btn-sm btn-success pull-right">Ajouter Ordonnance</a> 
 <button type="button" class="btn btn-danger btn-xs add_ord"  data="<?php echo $id_consult;?>" >Supprimer</button>

 </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 >N° Dossier </th>
                                           <th >Nom patient</th>
                                           <th >Medicament</th>
                                            <th >Quantite</th>
                                             <th >Posologie</th>
                                           <th >Date</th>

                                           <th style="width: 15%;"> Actions</th>
                                       </tr>
                                    </thead>

                                    <tbody>

       <?php
      
 if(isset($_GET['id'])){


      $id_patient=$_GET['id'];

        /*$req_sql_pat="SELECT *  FROM patients as p where  p.id='$id_patient' ";
     
       $query_pat=mysqli_query($con,$req_sql_pat);
        $row_pat = mysqli_fetch_array($query_pat);

*/
       $req_sql="SELECT *  FROM ordonnances as o,consultations as c, patients as p where c.id_patient=p.id and o.id_consult=c.id and o.id_consult='$id_patient'";}
     
       $query_consult=mysqli_query($con,$req_sql);

         while ($row_consult = mysqli_fetch_array($query_consult) ) {  ?>
         <tr>
          <td > <?php echo $row_consult['id_patient']; ?> </td>
          <td > <?php echo $row_consult['nom']." ".$row_consult['prenom']; ; ?> </td>
          <td > <?php echo $row_consult['nom_medicament']; ?> </td>
            <td > <?php echo $row_consult['quantite']; ?> </td>
              <td > <?php echo $row_consult['posologie']; ?> </td>
          <td > <?php $date=date_create($row_consult['date_creation']);  echo date_format($date,"d/m/Y H:i"); ?> </td>
          
      
          <td>
           <button type="button" class="btn btn-info btn-xs" data-toggle="modal"  data-target="#affiche<?php echo  $row_consult['id'];?>">Afficher</button>
             <button type="button" class="btn btn-warning btn-xs" data-toggle="modal"  data-target="#edit_consultation<?php echo  $row_consult['id'];?>">Modifier c</button>
         <button type="button" class="btn btn-danger btn-xs sup_consult"  data="<?php echo $row_consult['id'];?>" >Supprimer</button>
         <div class="modal fade" id="affiche<?php echo $row_consult['id'];?>" 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">Ordonnance  "<?php echo $row_consult["nom"]." ".$row_consult["prenom"];  ?> "</h4>
      </div>
      <div class="modal-body">
      <table class="table table-striped table-bordered table-hover" >
             <tr><th>Nom et prenom</th> <td > <?php echo $row_consult["nom"]." ".$row_consult["prenom"]; ?></td></tr>
             <tr><th>Medicament </th> <td > <?php echo $row_consult["nom_medicament"]; ?></td></tr>
             <tr><th>Poids </th> <td > <?php echo $row_consult["poids"]; ?></td></tr>
             <tr><th>Taille </th> <td > <?php echo $row_consult["taille"]; ?></td></tr>
             <tr><th>Temperature </th> <td > <?php echo $row_consult["temperature"]; ?></td></tr>
             <tr><th>Frequence cardiaque </th> <td > <?php echo $row_consult["frequence_cardiaque"]; ?></td></tr>
             <tr><th>Glycemie </th> <td > <?php echo $row_consult["glycemie"]; ?></td></tr>
             <tr><th>Diagnostic medical </th> <td > <?php echo $row_consult["diagnostic_medical"]; ?></td></tr>
              <tr><th>date d'ajout </th> <td > <?php $date=date_create($row_consult['date_creation']);  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>



    <!--------------------end  add consultation modal -------------------------------------------------------------------->



 </td>

          </tr>
          <?php  } ?>
     </tbody>
     </table>
                  </div>
          </div>
          </div>
<script type="text/javascript">
 $(document).ready(function() {
  $("#g_ordonnance").addClass("active");
 document.title = 'Liste des ordonnances ';

 $(".add_ord").click(function ()
{   var id_consult = $(this).attr('data');
$.get("actions/ordonnance/ajouter.php?id_consult="+id_consult,function()
    {    location.reload();
    });
});


  function fetch_data()  
    {  
        var id_patient= $("#id_patient").val();
        $.ajax({  
            url:"actions/fonct.php",  
            method:"POST",  
            data:{"id_doc":id_doc},
            success:function(data){  
        $('.table_auteurs ').html(data);  
            }  
        });  
    }

   // fetch_data();



    $("#ajouterOrdonance").click(function(){
               var id_patient= $("#id_patient").val();  
               var medicament= $("#medicament").val(); 
               console.log("medicament"+medicament);
               var quantite= $("#quantite").val(); 
               var posologie= $("#posologie").val()
                 $.ajax({  
                     url:"actions/ordonnance/ajouter.php",  
                     method:"POST", 
                     data:{
                      "id_patient":id_patient,
                    "medicament":medicament,
                     "quantite":quantite,
                      "posologie":posologie
                   
                     
                    },  
                     success:function(data)  
                     {  $("#ajouterOrdonance")[0].reset();
                           setTimeout(function() {
                        location.reload();
                      }, 2000);
                           
                          /*fetch_data();*/
            
        }
      });
    }); 

/****************edit consultation modal *************************/

 $("#editOrdonance").click(function(){
            var id= $(".id").val(); 
            var id_patient= $(".id_patient").val();  
            var motif= $(".motif").val(); 

           
               var poids= $(".poids").val(); 
               var taille= $(".taille").val(); 
               var temperature= $(".temperature").val(); 

               var frequence_cardiaque= $(".frequence_cardiaque").val(); 
               var glycemie= $(".glycemie").val(); 
               var diagnostic_medical= $(".diagnostic_medical").val(); 
              
                 $.ajax({  
                     url:"actions/consultation/modifier.php",  
                     method:"POST", 
                     data:{
                      "id":id,
                      "id_patient":id_patient,
                      "motif":motif,
                     "poids":poids,
                      "taille":taille,
                    "temperature":temperature,
                     "frequence_cardiaque":frequence_cardiaque,
                      "glycemie":glycemie,
                    "diagnostic_medical":diagnostic_medical   
                    },  
                     success:function(data) 
                     { 
                     setTimeout(function() {
              location.reload();
               }, 2000);//$("#editConsultation")[0].reset();
                     
                    /*fetch_data();*/
            
        }
      });
    }); 





$(".sup_consult").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_consult&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 }} ?>