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


      $id_patient=$_GET['id'];

 ?>

  <label>Consultations</label>
                         <select name="id_consult" id="id_consult" class="form-control" >
                        <option value="0">Sélectionner Consultation</option>
                        <?php
                          $req_sql_pat="SELECT *,c.id  FROM patients as p,consultations as c where p.id=c.id_patient and  p.id='$id_patient' ";
       $query_pat=mysqli_query($con,$req_sql_pat);
       
                       
                         while ($row_pat = mysqli_fetch_array($query_pat)) {
                          
                        ?>
                         <option value="<?php echo $row_pat['id']?>"><?php echo $row_pat['type_consult']?></option>
                     <?php }?>
                        </select> 


 <div id="box">
 <input type="text" id="id_consult1"></input>


<a href="index.php?p=add_ordonnance&id_consult=<?php echo $row_pat['id'];?>"  class="btn btn-sm btn-success pull-right">Ajouter Ordonnance</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 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_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'"; 
      $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="index.php?p=print_ord&id_ord=<?php echo $row_ord['id_ord'];?>&id_patient=<?php echo $row_pat['id_patient'] ;?> " class="btn btn-info btn-xs"> Print</a>
             <button type="button" class="btn btn-warning btn-xs" data-toggle="modal"  data-target="#edit_consultation<?php echo  $row_ord['id_ord'];?>">Modifier c</button>
         <button type="button" class="btn btn-danger btn-xs sup_consult"  data="<?php echo $row_ord['id_ord'];?>" >Supprimer</button>
     



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



 </td>

          </tr>
          <?php  } ?>
     </tbody>
     </table>
                  </div>
          </div>
          </div>
           </div>


<script type="text/javascript">
const el = document.getElementById('id_consult');

const box = document.getElementById('box');

el.addEventListener('change', function handleChange(event) {
  if (event.target.value != '0') {
    box.style.display = 'block';
     var id_consult = $(this).val();
   
     document.getElementById('id_consult1').value = id_consult;
    
 
  } else {
    box.style.display = 'none';
  }
});
</script>
<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_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();
   })
  


  }
})

});



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