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


      $id_consult=$_GET['id_consult'];
 
 ?>
 <div class="head-content col-sm-12">
   <div class="col-sm-1">
    <input type="button"  class="btn btn-sm btn-primary btn-block" value="Retour" onclick="history.go(-1)" >
    </div>
   <div class="col-sm-11">                       
<a href="index.php?p=add_ordonnance&id_consult=<?php echo $id_consult;?>"  class="btn btn-sm btn-success pull-right">Ajouter Ordonnance</a> 
  </div>
 </div>
 </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 style="width: 3%;">N° </th>
                                           <th >Nom patient</th>
                                           <th >Type Consultation</th>
                                           <th >Date</th>

                                           <th style=""> Actions</th>
                                       </tr>
                                    </thead>

                                    <tbody>

       <?php
      $req_sql_pat="SELECT *  FROM patients as p,consultations as c  where p.id=c.id_patient and  c.id='$id_consult' ";
     
       $query_pat=mysqli_query($con,$req_sql_pat);
        $row_pat = mysqli_fetch_array($query_pat);
   
     $req_sql_ord= "SELECT *,o.id_ord,o.date_modification FROM ordonnances as o,consultations as c, consultation_ordonnance as co  
     WHERE o.id_ord=co.id_ord and  c.id=co.id_consult and c.id='$id_consult' ORDER BY  o.date_modification DESC"; 
      $result_ord= mysqli_query($con,$req_sql_ord);




       

         while ($row_ord = mysqli_fetch_array($result_ord) ) {  ?>
         <tr>
         <td > <?php echo  $row_pat['id_patient']  ?> </td>
          <td > <?php echo $row_pat['nom']." ".$row_pat['prenom']; ?> </td>
          <td > <?php echo $row_ord['type_consult'] ; ?> </td>
         
          <td > <?php $date=date_create($row_ord['date_modification']);  echo date_format($date,"d/m/Y H:i"); ?> </td>
          
      
          <td>
  <a href="pages/ordonnance/file.php?id_ord=<?php echo $row_ord['id_ord'];?>&id_patient=<?php echo $row_pat['id_patient'] ;?> " class="btn btn-info btn-xs">Print</a>


            <a href="index.php?p=edit_ordonnance&id_ord=<?php echo $row_ord['id_ord'];?>&id_consult=<?php echo $id_consult;?>"  class="btn btn-xs btn-warning ">Modifier</a> 
 
                   <button type="button" class="btn btn-danger btn-xs sup_ord"  data="<?php echo $row_ord['id_ord'];?>" >Supprimer</button>
     



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



 </td>

          </tr>
          <?php  } ?>
     </tbody>
     </table>
                  </div>
          </div>
          </div>
<script type="text/javascript">
 $(document).ready(function() {
  $("#g_patient").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_ord").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_ord&id="+id,function()
    {   
       setTimeout(function() {
              location.reload();
               }, 2000);
    });

    swal(

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


  }
})

});




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