<?php
if(!empty($_SESSION['user_connect_id']))
{
if(!isset($_GET['id_ord'])){
  include("pages/404.php");
 }
 else
 {


    $id_ord=mysqli_real_escape_string($con,htmlspecialchars(trim($_GET['id_ord']))) ;
    $id_consult=mysqli_real_escape_string($con,htmlspecialchars(trim($_GET['id_consult']))) ;
    $req_get="SELECT * 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  o.id_ord='$id_ord'";
    $query_get=mysqli_query($con,$req_get);

    $row= mysqli_fetch_array($query_get); 
    $id_patient= $row['id_patient'];


    $req_get_patient="SELECT * FROM patients where id='$id_patient'";
    $query_get_patient=mysqli_query($con,$req_get_patient);
    $row_patient= mysqli_fetch_array($query_get_patient);
    ?>
    <h4 class="title"><i class="fa fa-building"></i> Modifier L'ordonnance du patient " <?php echo $row_patient['nom']." ".$row_patient['prenom']; ?> "</h4>
      <div class="row">
                  <div class="col-lg-12">
                      <div class="form-panel">
                   	    <form id="form" method="" class="form-horizontal" action=""  enctype="multipart/form-data">
              	         <div class="form-group">
                          <div class="col-sm-3 col-xs-12">
                          <label>patient</label>
        
                    
                       
                     
                  <select name="id_patient" class="form-control" disabled>
                     <?php 
                 
                  $req_get_p="SELECT *  FROM patients as p, consultations as c where c.id_patient=p.id and c.id='$id_consult' ";}
                  $query_get_p=mysqli_query($con,$req_get_p);

                  $row_p= mysqli_fetch_array($query_get_p); ?>


                    <option value="<?php echo $row_p['id'] ?>" ><?php echo $row_p['nom']." ".$row_p['prenom'] ; ?></option>
                    </select>


               
                                        </div>
                                        </div> 
                                   
                                <div class="form-group" id="div_aut">
                           <div class="col-sm-9 col-xs-12 table-wrapper">

                          <div class="table-title">
                                          <div class="row col_droite">
                                              <div class="col-sm-6">
                                <h3>Medicaments</h3>
                               </div>
                               <div class="col-sm-6 ">
                                     <a href="#addMedicament" class="btn btn-success" data-toggle="modal"><i class="material-icons">&#xE147;</i> <span>Ajouter Medicament</span></a>
                                 
                               </div>
                                          </div>
                                      </div>

                           <div class="col-sm-6">
                         
                               </div>
                           <table class="table table-striped table-hover table_medicaments">
                          <?php 
                          $req_sql= "SELECT *,o.id_ord FROM ordonnances as o ,medicaments as m,  
                          ordonnance_medicament as om WHERE o.id_ord=om.id_ord and  m.id_medicament=om.id_med and o.id_ord='$id_ord '"; 
                           $resultats= mysqli_query($con,$req_sql) or die('Query privacy_stars failed: ' . mysqli_error($con));?>

                                          <thead>
                                              <tr>
                            
                                                  <th>Medicament</th>
                                                  <th>Quantité</th>
                                                  <th>Posologie</th>
                                              </tr>
                                          </thead>

                                                              <tbody>

                              
                                        

                                              <tr>
                                               <?php 
                                                  while($row_med = mysqli_fetch_array($resultats)){?>
                               <tr><td >  <?php echo $row_med['nom_med'] ?>  </td>
                                      <td >  <?php echo $row_med['quantite'] ?>  </td>
                                      <td >  <?php echo $row_med['posologie'] ?>  </td>
                                 <td>
      

                        <button type="button" class="btn btn-danger btn-xs osup_med"    data="<?php echo $row_med['id_med'] ?>" >Supprimer</button>
                            </td>
                                              </tr>
                                              <?php  } ?>
                                              <tr>

                                         
                                              </tr> 
                                
                                         
                                          </tbody>

                                      </table>

                          </div>
                          </div>
                   

                  <div class="form-group">
                   <input type="hidden"   class="form-control"  id="id_consult" name="id_consult" value="<?php echo $id_consult; ?>">
                    <input type="hidden" id="id_ord" name="id_ord" value="<?php echo $id_ord; ?>"> 
                             <div class="col-sm-1 col-xs-12">
                                        <input type="button" class="btn btn-secondary btn-block" style="padding: 12px 12px !important;margin: 30px 0px;" value="Retour" onclick="history.go(-1)">
                             </div>
                             <div class="col-sm-1 col-xs-12">
                 
                                <input type="submit" id="Modifier"  class="btn btn-primary btn-block" value="Modifier">
                 </div>
               <div class="col-sm-1 col-xs-12">
                <a href="index.php?p=print_ord&id_ord=<?php echo $id_ord;?>&id_patient=<?php echo $row_p['id_patient'] ;?> " class="btn btn-info btn-block print"> Imprimer</a>
            

                 </div>
                   
                               
                         </div>
                    <div class="input-group message col-sm-6" id="mess">

                  </div>
              	    </form>

                     <!-- add medicament -->
 <div id="addMedicament" class="modal fade">
                    <div class="modal-dialog">
                     <div class="modal-content">
                      <form id="ajoutMedicament"  method="" class="form-horizontal" action=""  enctype="multipart/form-data">
                      <input type="hidden" class="form-control"  name="id_ord" id="id_ord" value="<?php $id_ord?>">
                      <div class="modal-header">      
                        <h4 class="modal-title">Ajouter Medicament</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                       </div>
                       <div class="modal-body">     
                        <div class="form-group modalForm">
                         
                         <label>Medicament</label>
                         <select name="id_medicament" id="id_medicament" class="form-control" >
                        <option value="0">Sélectionner Médicament</option>
                        <?php
                       $reqq= "SELECT * FROM medicament";
                        $q_medicaments  = mysqli_query($con,$reqq);
                         while ($medicament = mysqli_fetch_array($q_medicaments)) {
                        ?>
                         <option value="<?php echo $medicament['NOM_DE_MARQUE']?>"><?php echo $medicament['NOM_DE_MARQUE']?></option>
                     <?php }?>
                        </select> 
                        
                        </div>
                        <div class="form-group modalForm">
                         <label>Quantité</label>
                         <input type="text" class="form-control"  name="quantite" id="quantite" >
                        </div>
                         <div class="form-group modalForm">
                         <label>Posologie</label>
                         <input type="text" class="form-control"  name="posologie" id="posologie" >
                        </div>
                            
                       </div>
                       <div class="modal-footer">
                        <input type="hidden" id="id_consult" name="id_consult" value="<?php echo $id_consult; ?>"> 
                       <input type="hidden" id="id_ord" name="id_ord" value="<?php echo $id_ord; ?>"> 
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                        <input type="submit" class="btn btn-success " data-dismiss="modal" id="ajouterMedicament" value="Ajouter">
                          

                             <div class="input-group message col-sm-6" id="mes">
      
                       </div>
                      </form>
                     </div>
                    </div>
                   </div>




                  </div>
        </div>
 	</div>



    <script type="text/javascript" src="assets/js/jquery.validate.js"></script>
    <script type="text/javascript">
    $(document).ready( function () {
    $("#g_patient").addClass("active");
    document.title = 'Modifier Ordonnance ';



    
       $(document).on('click', '.osup_med', function(){  
   var id_med = $(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_med&id_med="+id_med);
    swal(
      'Supprimé !',
      'L\'enregistrement a été supprimé.',
      'success'
    ).then(function(){ 
           
     fetch_data();          
 
   })
  }
})
 });

      function fetch_data()  
    {  
        var id_consult= $("#id_consult").val();
        var id_ord= $("#id_ord").val(); 
        $.ajax({  
            url:"actions/fonct_medicament.php",  
            method:"POST",  
            data:{"id_consult":id_consult,
                "id_ord":id_ord
                 },
            success:function(data){  
        $('.table_medicaments').html(data);  
            }  
        });  
    }

    fetch_data();

    $("#ajouterMedicament").click(function(){
            var id_medicament= $("#id_medicament").val();  
            var quantite= $("#quantite").val(); 
            var posologie= $("#posologie").val(); 
               var id_consult= $("#id_consult").val(); 
                var id_ord= $("#id_ord").val(); 
               
       

                 $.ajax({  
                     url:"actions/medicament/ajouter.php",  
                     method:"POST", 

                     data:{"id_medicament":id_medicament,
                     "quantite":quantite,
                     "posologie":posologie,
                     "id_consult":id_consult,
                     "id_ord":id_ord

                    },  
                     success:function(data)  
                     { $("#ajoutMedicament")[0].reset();
                     
                    fetch_data();
            
        }
      });
    }); 


    $("#form").validate({
    submitHandler: function(form) {
    $.ajax({
     url: 'actions/ordonnance/modifier.php',
     type: "POST",
     data: new FormData(form),
     contentType: false,
     cache: false,
     processData: false,
     success: function(data){
     if(data == 1){
              $("#mess").html('<div class="alert alert-success">  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>   <strong>Modification avec succès </strong> </div>')  ;
                   setTimeout(function() {
                   location.reload();
                   }, 2000);
        } else  {
         $("#mess").html('<div class="alert alert-danger">  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>   <strong>Erreur.</strong> </div>')  ;

         }
    },
     error: function(){}
     });
      } ,
                    rules: {
                   nom: "required",
                   nomar: "required",
                                   },
                    messages: {
                            nom: "Entrez le nom ",
                                nomar: "Entrez le nom en arab"
                               },
                    errorElement: "em",
                    errorPlacement: function ( error, element ) {

                        error.addClass( "help-block" );

                        element.parents( ".col-sm-6" ).addClass( "has-feedback" );

                        if ( element.prop( "type" ) === "checkbox" ) {
                            error.insertAfter( element.parent( "label" ) );
                        } else {
                            error.insertAfter( element );
                        }

                    },

                    highlight: function ( element, errorClass, validClass ) {
                        $( element ).parents( ".col-sm-6" ).addClass( "has-error" ).removeClass( "has-success" );
                        $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
                    },
                    unhighlight: function ( element, errorClass, validClass ) {
                        $( element ).parents( ".col-sm-6" ).addClass( "has-success" ).removeClass( "has-error" );
                        $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
                    }
                } );







            } );


    </script>
    <?php
    }

else{  

 include("../404.php");  } ?>