<?php
  include VIEWPATH.'includes/new_header.php';
  ?>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  
  <?php
  include VIEWPATH.'includes/new_top_menu.php';
  include VIEWPATH.'includes/new_menu_principal.php';
  ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    
    <?php 
    include 'includes/menu_facture.php';
    ?>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            
            <!-- /.card -->

            <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title">Facture</h3>
              </div>
              <!-- /.card-header -->
              <div class="card-body">
               



                <table id="example1" class="table table-bordered table-striped" style="width:100%">
        <thead>
            <tr>
                <th>#</th>
                <th>Client</th>
                <th>Status</th>
                <th>Date</th>
                <th>Montant</th>
                <th>Statut</th>
                <th>Paiement</th>
                <th>Historique</th>
            </tr>   
        </thead>
        <tbody>
          <?php

          $resultat=$this->Model->getRequete('SELECT `ID_VENTE`,`NUMERO_VENTE`,client_client.NOM, client_client.PRENOM, `DATE_ENREGISTREMENT`, vente_statut.DESCRIPTION,`DATE_COMMANDE`,`MONTANT_TOTAL`, MONTANT_PAYE, paiement_status.DESCRIPTION AS STATUS_PAIEMENT, vente_vente.ID_STATUS_PAIEMENT FROM `vente_vente` JOIN client_client ON client_client.ID_CLIENT = vente_vente.ID_CLIENT JOIN vente_statut ON vente_statut.ID_VENTE_STATUT = vente_vente.ID_VENTE_STATUT JOIN paiement_status ON paiement_status.ID_STATUS_PAIEMENT = vente_vente.ID_STATUS_PAIEMENT WHERE vente_vente.ID_VENTE_STATUT = 1 AND vente_vente.ID_STATUS_PAIEMENT = 2');

          foreach ($resultat as $key) 
         {
          // $commune= $this->Model->getRequete("SELECT `ID_VENTE_DETAILS`,syst_taille_bouteille.DESCRIPTION,`QUANTITE_COMMANDE`,`PRIX_UNITAIRE` FROM `vente_details` JOIN syst_taille_bouteille ON syst_taille_bouteille.ID_TAILLE_BOUTEILLE = vente_details.ID_TAILLE_BOUTEILLE WHERE `ID_VENTE`  = ".$key['ID_VENTE']."");

          $MONTANTRESTANT =$key['MONTANT_TOTAL']-$key['MONTANT_PAYE'];
          
          echo "<tr>
                <td>".$key['NUMERO_VENTE']."</td>
                <td>".$key['NOM']." ".$key['PRENOM']."</td>
                <td>".$key['DESCRIPTION']."</td>
                <td>".$key['DATE_COMMANDE']."</td>
                <td>".$key['MONTANT_TOTAL']."</td>
                <td>".$key['STATUS_PAIEMENT']."</td>
                <td>";
                echo'<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal'.$key['ID_VENTE'].'">
  Payer
</button>

<!-- Modal -->
<div class="modal fade" id="myModal'.$key['ID_VENTE'].'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title" id="myModalLabel">Paiement</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        
      </div>
      <div class="modal-body">
        <form role="form" action="'.base_url('facture/Facture/add').'" enctype="multipart/form-data" method="POST">
        <div class="card-body row">
        <div class="form-group col-lg-6">
                    <label for="exampleInputEmail1">Montant Total <spam class="text-danger">*</spam></label>
                    <input type="text" class="form-control" readonly="readonly" value="'.$key['MONTANT_TOTAL'].'">
                  
        </div>
        <div class="form-group col-lg-6">
                    <label for="exampleInputEmail1">Montant Restant <spam class="text-danger">*</spam></label>
                    <input type="text" class="form-control" readonly="readonly" value="'.$MONTANTRESTANT.'">
                
        </div>
        <div class="form-group col-lg-6">
                    <label for="exampleInputEmail1">Montant Paye <spam class="text-danger">*</spam></label>
                    <input type="number" class="form-control" id="MONTANT_PAIEMENT" max="'.$MONTANTRESTANT.'" name="MONTANT_PAIEMENT" required="required">
                    <input type="hidden" class="form-control" id="ID_VENTE" name="ID_VENTE" value="'.$key['ID_VENTE'].'">
                  </div> 

                  <div class="form-group col-lg-6">
                    <label for="exampleInputEmail1">Date Paiement <spam class="text-danger">*</spam></label>
                    <input type="date" class="form-control" id="DATE_PAIEMENT" name="DATE_PAIEMENT" required="required">
                  </div> 
                  <div class="card-footer">
                  <button type="submit" class="btn btn-primary">Enregistrer</button>
                  <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
                </div>
        </div>
        </form>
      </div>
     
    </div>
  </div>
</div></td>';

         echo "<td>";

         $historique= $this->Model->getRequete("SELECT DATE_PAIEMENT, MONTANT_PAIEMENT, paiement_status.DESCRIPTION FROM `paiement_paiement` JOIN paiement_status ON paiement_status.ID_STATUS_PAIEMENT = paiement_paiement.ID_STATUS_PAIEMENT WHERE ID_VENTE = ".$key['ID_VENTE']."");
          $hist = "<table class='table'><tr><td>Date</td><td>Montant</td><td>Status</td></tr>";
          foreach ($historique as $value) {
            $hist .= "<tr><td>".$value['DATE_PAIEMENT']."</td><td>".$value['MONTANT_PAIEMENT']."</td><td>".$value['DESCRIPTION']."</td></tr>";
          }
          $hist .= "</table>";
         
         echo'<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModalhisto'.$key['ID_VENTE'].'">
  Historique
</button>

<!-- Modal -->
<div class="modal fade" id="myModalhisto'.$key['ID_VENTE'].'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title" id="myModalLabel">Historique de Paiement</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        
      </div>
      <div class="modal-body">
        <div class="card-body row">
        '.$hist.'
        </div>
       
                  <div class="card-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
                </div>
        </div>
      </div>
     
    </div>
  </div>
</div>';
         
        echo" </td></tr>";
       }
          ?>
            
        </tbody>
        
    </table>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

  <?php
 include VIEWPATH.'includes/new_copy_footer.php';  
  ?>
  

  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->
</div>

<!-- ./wrapper -->
<?php
  include VIEWPATH.'includes/new_script.php';
  ?>

<!-- Page specific script -->
<script>
  $(function () {
    $("#example1").DataTable({
      "responsive": true, "lengthChange": false, "autoWidth": false,
      "buttons": ["excel", "pdf", "print", "colvis"]
    }).buttons().container().appendTo('#example1_wrapper  .col-md-6:eq(0)');
   
  });
</script>

<script type="text/javascript">
  function changelist() {
document.getElementById("myForm").submit();
  }

  $('#reservation').daterangepicker({

        locale: {
        format: 'DD/MM/YYYY'
      }
    })
  $('.select').select2();

</script>
</body>
</html>
<script>
function modifierarti(param1) {
    // Utilisez les paramètres ici
    alert('ID Vente details : ' + param1);
    
    // var iD_VENTE_DETAILS = $(param1).val();
    // alert('ID Vente details : ' + iD_VENTE_DETAILS);
    var PRIX_UNITAIRE = $('#PRIX_UNITAIRE_'+ param1).val();
    var QUANTITE_LIVRE = $('#QUANTITE_COMMANDE_'+param1).val();

    alert('Prix unitaire : ' + PRIX_UNITAIRE);
    alert('Quantite livre : ' + QUANTITE_LIVRE);
    
    $.post('<?php echo base_url('vente/Vente/livraison')?>',
          {
            ID_VENTE_DETAILS:param1,
            PRIX_UNITAIRE:PRIX_UNITAIRE,
            QUANTITE_LIVRE:QUANTITE_LIVRE
          },
          function(data){
            alert(data);
            if (data == 1) {
              document.getElementById('PRIX_UNITAIRE_'+ param1).readOnly = true;
              document.getElementById('QUANTITE_COMMANDE_'+ param1).readOnly = true;
              document.getElementById('LINKS_'+ param1).onclick = function(event) {
                  event.preventDefault(); // Empêche l'action par défaut du lien
                  // alert('Ce lien est désactivé.');
              };
            }
            
            // var newone = data.split("|:|"); // Detail Enregistrement SA
            // var resultattab = newone[0];
            // var resultatprime = newone[1];

            // $('#newdiv').html(resultattab);
            // $('#PRIMETOTNEW').val(resultatprime);
          });
    
}
</script>

            