<?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_retour_vente.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">Retour Vente</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>Details</th>
                <th></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` 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 WHERE vente_vente.ID_VENTE_STATUT = 2 AND vente_vente.DATE_RETOUR IS NULL');

          foreach ($resultat as $key) 
         {
          $commune= $this->Model->getRequete("SELECT `ID_VENTE_DETAILS`,syst_taille_bouteille.DESCRIPTION,`QUANTITE_LIVRE`,`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']."");

    $datas= '<table class="table">';
    $datas.='<tr>
                <td><b>Taille</b></td>
                <td class="text-center"><b>Prix Unitaire</b></td>
                <td class="text-center"><b>Quantite</b></td>
                <td class="text-center"><b>Action</b></td>
            </tr>';
    $totolpayabe =0;
    foreach($commune as $commun){
    $datas.= '<tr>
                <td>'.$commun["DESCRIPTION"].'</td>
                <td>
                '.$commun["PRIX_UNITAIRE"].'
                </td>
                <td><input type="number" class="form-control" id="QUANTITE_LIVRE_'.$commun['ID_VENTE_DETAILS'].'" name="QUANTITE_LIVRE" max="'.$commun["QUANTITE_LIVRE"].'" value="'.$commun["QUANTITE_LIVRE"].'">
                </td>
                <td>
                <a class="btn btn-danger btn-xs" href="#" onclick="modifierarti('.$commun['ID_VENTE_DETAILS'].')" id="LINKS_'.$commun['ID_VENTE_DETAILS'].'"  role="button">Retour Vente</a>
                </td>
            </tr>';
            
    }
    // $datas.='';
    $datas.= '</table>';
      
          
          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>";
                echo'<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal'.$key['ID_VENTE'].'">
  Details
</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">Details</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        
      </div>
      <div class="modal-body">
        '.$datas.'
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
      </div>
    </div>
  </div>
</div></td>';

                $modadcontent ='<ul class="navbar-nav ml-auto text-center">
      <li class="nav-item dropdown">
        <a class="nav-link" data-toggle="dropdown" href="#">
          <i class="fas fa-cog"></i>
        </a>
        <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
        ';
            $modadcontent.='<div class="dropdown-divider"></div>
          <a href="' . base_url('donnee/Client/index_update/' .$key['ID_VENTE']). '" class="dropdown-item">
            <!-- Message Start -->
            <div class="media">
              <div class="media-body">
                <h3 class="dropdown-item-title">
                  Modifier
                </h3>
                <p class="text-sm"> #'.$key['ID_VENTE'].'</p>
              </div>
            </div>
            <!-- Message End -->
          </a>';          
          
          $modadcontent.='<div class="dropdown-divider"></div>
          
          
        </div>
      </li>
      
    </ul>
  </nav>';
         echo "<td>".$modadcontent."</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 QUANTITE_LIVRE = $('#QUANTITE_LIVRE_'+param1).val();

    
    
    $.post('<?php echo base_url('vente/Retour_Vente/retourv')?>',
          {
            ID_VENTE_DETAILS:param1,
            QUANTITE_RETOURNE:QUANTITE_LIVRE
          },
          function(data){
            // alert(data);
            if (data == 1) {
              document.getElementById('QUANTITE_LIVRE_'+ 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>

            