<?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_carburant.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">Liste de la consomations d&eacute;j&agrave; enregistr&eacute;</h3>
              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <!-- class="form-inline" -->
                <form action="<?php echo base_url('voiture/Mettre_Carburant/listing');?>" id="myForm" method="post" >
                <div class="row">

                    <div class="form-group col-md-3 col-sm-3 col-xs-3">
                <label for="exampleInputName2">Periode:</label>
                      
                      <input type="text" class="form-control float-right" name="PERIODE" value="<?php echo $selectdate; ?>" id="reservation">  
                    </div>

                    <div class="form-group col-md-3 col-sm-3 col-xs-3">
                        <label for="exampleInputName2">Voiture:</label>
                        <select class="form-control select" name="VEHICULE_ID" id="VEHICULE_ID" style="width: 100%;">
                        <option value=""> </option>
                        <?php 
                        $VEHICULE_ID=$this->Model->getList('voiture_vehicules',array());
                        foreach ($VEHICULE_ID as $key) {
                          if ($VEHICULE_IDS == $key['VEHICULE_ID'] ) {
                            echo "<option value=".$key['VEHICULE_ID']." selected>".$key['PLAQUE_VEHICULE']."</option>";
                          }
                          else{
                            echo "<option value=".$key['VEHICULE_ID'].">".$key['PLAQUE_VEHICULE']."</option>";
                          } 
                        }
                        ?>
                      </select>
                  </div>

                    <div class="form-group col-md-3 col-sm-3 col-xs-3">
                      <label for="exampleInputName2">Station:</label>
                      <select class="form-control select" name="STATION_ID" id="STATION_ID" style="width: 100%;">
                                      <option value=""> </option>
                                      <?php 
                                      $STATION_ID=$this->Model->getList('voiture_station',array());
                                        foreach ($STATION_ID as $key) {
                                        if ($STATION_IDS == $key['STATION_ID'] ) {
                                          echo "<option value=".$key['STATION_ID']." selected>".$key['STATION_NOM']."</option>";
                                        }
                                        else{
                                          echo "<option value=".$key['STATION_ID'].">".$key['STATION_NOM']."</option>";
                                        }
                                        }
                                      ?>
                                    </select>
                    </div>
                    <div class="col-md-3 col-sm-3 col-xs-3">
                      <br>
                      <!-- <br> -->
                      <a class="btn btn-primary btn-xs" href="#" onclick="changelist()" role="button"><i class="fa fa-search" aria-hidden="true"></i> Recherche </a> &nbsp;

                  <a class="btn btn-primary btn-xs" href="<?php echo base_url('voiture/Mettre_Carburant/listing');?>" role="button"> <i class="fa fa-undo" aria-hidden="true"></i> Réinitialiser </a> &nbsp;
                  <a class="btn btn-primary btn-xs" href="#" onclick="exportlist()" role="button"><i class="fa fa-download" aria-hidden="true"></i> Excel </a>
                    </div>

                  </div>

                
               

                
                </form>
                <div class="text-center"><?php echo $title;?></div>



                <table id="mytable" class="table table-bordered table-striped" style="width:100%">
        <thead>
            <tr>
                <th>Num</th>
                <th>Plaque</th>
                <th>Date</th>
                <th>KM</th>
                <th>Station</th>
                <th>Quantité</th>
                <th>Direction</th>
                <th>Montant</th>
                <th>Options</th>
            </tr>
        </thead>
        <tbody>
          <?php

          // echo $conditions; 

          // AND CONSOMATION_DATE LIKE "2021-07%"
          $resultat=$this->Model->getRequete('SELECT car.CONSOMATION_ID, car.PATH_FACTURE, car.IS_PAYE, car.DATE_INSERSION, veh.PLAQUE_VEHICULE, car.CONSOMATION_DATE, car.KM_A_APROVISIONNEMENT, BUJUMBURA_TIME, NAVIGATEUR, OPERATING_SYSTEM, INTERNET_IP, sta.STATION_NOM, car.QUANTITE_RECU, CASE WHEN car.PROVENANCEOUDESTINATION = 1 THEN "en provenance de" WHEN car.PROVENANCEOUDESTINATION = 2 THEN "à destination de " END as PROVENANCE, car.MONTANT_CONSOMATION, car.PRIX_UNITAIRE, car.NUM_RECU, car.NUM_GENERATED, car.PATH_FACTURE, uses.NOM as USNOM, uses.PRENOM as USPRENOM, chau.NOM as CHNOM, chau.PRENOM as CHPRENOM FROM voiture_carburant_consomation as car JOIN voiture_vehicules as veh ON veh.VEHICULE_ID = car.VEHICULE_ID JOIN voiture_station as sta on sta.STATION_ID = car.STATION_ID  LEFT JOIN config_user as us ON us.ID_USER = car.USERSAVER LEFT JOIN config_user as uses on uses.ID_USER = us.ID_USER LEFT JOIN rh_employe as chau on chau.ID_EMPLOYE = car.CHAUFFEUR_ID WHERE ID_STATUS = 1 '.$conditions.' '.$condvehicle.' '.$condstation.' ');

          $totals=$this->Model->getRequeteOne('SELECT SUM(car.QUANTITE_RECU) AS QUANTITE_RECU, SUM(car.MONTANT_CONSOMATION) AS MONTANT_CONSOMATION FROM voiture_carburant_consomation as car JOIN voiture_vehicules as veh ON veh.VEHICULE_ID = car.VEHICULE_ID JOIN voiture_station as sta on sta.STATION_ID = car.STATION_ID LEFT JOIN config_user as us ON us.ID_USER = car.USERSAVER LEFT JOIN config_user as uses on uses.ID_USER = us.ID_USER LEFT JOIN rh_employe as chau on chau.ID_EMPLOYE = car.CHAUFFEUR_ID WHERE ID_STATUS = 1 '.$conditions.' '.$condvehicle.' '.$condstation.' ');

          $totalsess=$this->Model->getRequeteOne('SELECT SUM(car.QUANTITE_RECU) AS QUANTITE_RECU FROM voiture_carburant_consomation as car JOIN voiture_vehicules as veh ON veh.VEHICULE_ID = car.VEHICULE_ID JOIN voiture_station as sta on sta.STATION_ID = car.STATION_ID LEFT JOIN config_user as us ON us.ID_USER = car.USERSAVER LEFT JOIN config_user as uses on uses.ID_USER = us.ID_USER LEFT JOIN rh_employe as chau on chau.ID_EMPLOYE = car.CHAUFFEUR_ID WHERE ID_STATUS = 1 AND car.PRIX_UNITAIRE = 3250 '.$conditions.' '.$condvehicle.' '.$condstation.' ');

          $totalsmaz=$this->Model->getRequeteOne('SELECT SUM(car.QUANTITE_RECU) AS QUANTITE_RECU FROM voiture_carburant_consomation as car JOIN voiture_vehicules as veh ON veh.VEHICULE_ID = car.VEHICULE_ID JOIN voiture_station as sta on sta.STATION_ID = car.STATION_ID LEFT JOIN config_user as us ON us.ID_USER = car.USERSAVER LEFT JOIN config_user as uses on uses.ID_USER = us.ID_USER LEFT JOIN rh_employe as chau on chau.ID_EMPLOYE = car.CHAUFFEUR_ID WHERE ID_STATUS = 1 AND car.PRIX_UNITAIRE = 3450 '.$conditions.' '.$condvehicle.' '.$condstation.' ');


          foreach ($resultat as $key) 
         {
          $date=date_create($key['CONSOMATION_DATE']);
          $ndate = date_format($date,"d/m/Y");


          $datet=date_create($key['DATE_INSERSION']);
          $ndatet = date_format($datet,"H:i d/m/Y ");

          if ($key['IS_PAYE'] == 0) {
            if ($options != 2 and $options != 3 and $options != 4 ) {
              $pay = '';
            $textpay = 'Facture';
            }
            else{
              $pay = '';
            $textpay = 'Facture';
            }
            
          }
          else{
            $pay = '';
            $textpay = 'Facture';
          }

          $modalun = '<div class="modal fade" id="modal-default'.$key['NUM_GENERATED'].'">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Detail de '.$key['NUM_GENERATED'].' ('.$textpay.')</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">

              <table class="table">
              <tr>
              <td>Plaque</td><td><b>'.$key['PLAQUE_VEHICULE'].'</b></td>
              <td>Date</td><td><b>'.$ndate.'</b></td>
              </tr>
              <tr>
              <td>Consomation</td><td colspan="3"><b>'.number_format($key['QUANTITE_RECU'], 2,',',' ').' Litres soit '.number_format($key['MONTANT_CONSOMATION'], 2,',',' ').' BIF &agrave; '.number_format($key['PRIX_UNITAIRE'], 2,',',' ').' BIF le litre</b></td>
              </tr>
              <tr>
              <td>Km approvisionnement</td><td><b>'.$key['KM_A_APROVISIONNEMENT'].'</b></td>
              <td>Station</td><td><b>'.$key['STATION_NOM'].' ('.$key['PROVENANCE'].')</b></td>
              </tr>
              <tr>
              <td>Chauffeur </td><td colspan="3"><b>'.$key['CHNOM'].' '.$key['CHPRENOM'].'</b> </td>
              </tr>
              <tr>
              <td>Enregistrée par</td><td colspan="3"><b>'.$key['USNOM'].' '.$key['USPRENOM'].' &agrave; '.$ndatet.'</b> </td>
              </tr>
              <tr>
              <td>Metadata</td><td colspan="3"><b>'.$key['BUJUMBURA_TIME'].' - '.$key['NAVIGATEUR'].' - '.$key['OPERATING_SYSTEM'].' - '.$key['INTERNET_IP'].'</b> </td>
              </tr>

              </table>
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              '.$pay.'
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>';

      $modaldeux = '<div class="modal fade" id="modal-delet'.$key['NUM_GENERATED'].'">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Suppimer ou Annuler de '.$key['NUM_GENERATED'].'</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
            Les actions que vous allez faire sont irreversible!
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
              <a class="btn btn-warning" href="'.base_url('voiture/Mettre_Carburant/annuler/'.$key['CONSOMATION_ID'].'').'">Annuler Enregistrement</a>
              <a type="button" href="'.base_url('voiture/Mettre_Carburant/suppimer/'.$key['CONSOMATION_ID'].'').'" class="btn btn-danger">Suppimer Enregistrement</a>
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>';

      $PATH_FACTURE =  str_replace(" ","_",$key['PATH_FACTURE']);
  

      $modaltrois = '<div class="modal fade" id="modal-doc'.$key['NUM_GENERATED'].'">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Document de '.$key['NUM_GENERATED'].'</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
             
        <embed src="'.$nouveau_url.'/uploads/document_carburant/'.$PATH_FACTURE.'#toolbar=0" type="application/pdf"   scrolling="auto" height="500px" width="100%">
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>';

      
          
          echo "<tr>
                <td>".$modaltrois." ".$modaldeux." ".$modalun." ".$key['NUM_GENERATED']."</td>
                <td>".$key['PLAQUE_VEHICULE']."</td>
                <td>".$ndate."</td>
                <td>".$key['KM_A_APROVISIONNEMENT']."</td>
                <td>".$key['STATION_NOM']."</td>
                <td><div class='text-right'>".number_format($key['QUANTITE_RECU'], 2,',',' ')." Litres</div></td>
                <td></td>
                <td><div class='text-right'>".number_format($key['MONTANT_CONSOMATION'], 2,',',' ')."</div></td>
            ";
           // if($this->mylibrary->get_permission('Mettre_Carburant') ==1) {
          $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">
        <a href="#" class="dropdown-item" data-toggle="modal" data-target="#modal-default'.$key['NUM_GENERATED'].'">
            <div class="media">
              <div class="media-body">
                <h3 class="dropdown-item-title">
                  Details
                </h3>
                <p class="text-sm">Detail de '.$key['NUM_GENERATED'].'</p>
              </div>
            </div>
          </a>';
          $modadcontent.='<div class="dropdown-divider"></div>
          <a  href="#" class="dropdown-item" data-toggle="modal" data-target="#modal-doc'.$key['NUM_GENERATED'].'">
            <!-- Message Start -->
            <div class="media">
              <div class="media-body ">
                <h3 class="dropdown-item-title">
                  Document
                </h3>
                <p class="text-sm">Document de '.$key['NUM_GENERATED'].'</p>
              </div>
            </div>
            <!-- Message End -->
          </a>';
          if ($options != 2 and $options != 3 and $options != 4 ) {
            $modadcontent.='<div class="dropdown-divider"></div>
          <a href="' . base_url('voiture/Mettre_Carburant/modifier/' . $key['CONSOMATION_ID']) . '" class="dropdown-item">
            <!-- Message Start -->
            <div class="media">
              <div class="media-body">
                <h3 class="dropdown-item-title">
                  Modifier
                </h3>
                <p class="text-sm">Modification de '.$key['NUM_GENERATED'].'</p>
              </div>
            </div>
            <!-- Message End -->
          </a>';
          }
          
          $modadcontent.='<div class="dropdown-divider"></div>
          <a  href="#" class="dropdown-item" data-toggle="modal" data-target="#modal-delet'.$key['NUM_GENERATED'].'">
            <!-- Message Start -->
            <div class="media">
              <div class="media-body ">
                <h3 class="dropdown-item-title">
                  Suppimer - Annuler
                </h3>
                <p class="text-sm">Suppimer ou annuler le '.$key['NUM_GENERATED'].'</p>
              </div>
            </div>
            <!-- Message End -->
          </a>';
          $modadcontent.='<div class="dropdown-divider"></div>
          
        </div>
      </li>
      
    </ul>
  </nav>';
  // }
  // else
  // {
 // $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">
 //        <a href="#" class="dropdown-item" data-toggle="modal" data-target="#modal-default'.$key['NUM_GENERATED'].'">
 //            <div class="media">
 //              <div class="media-body">
 //                <h3 class="dropdown-item-title">
 //                  Details
 //                </h3>
 //                <p class="text-sm">Detail de '.$key['NUM_GENERATED'].'</p>
 //              </div>
 //            </div>
 //          </a>';  
  // }
         echo "<td>".$modadcontent."</td></tr>";
       }
          ?>
            
            
            
            
        </tbody>
        <tfoot>
            <tr>
                <th>TOTAL</th>
                <th colspan="2" class="text-right">Mazout: <?php echo number_format($totalsmaz['QUANTITE_RECU'], 2,',',' ')?></th>
                <th colspan="2" class="text-right">Essence: <?php echo number_format($totalsess['QUANTITE_RECU'], 2,',',' ')?></th>
                <!-- <th></th> -->
                <!-- <th></th> -->
                <th colspan="2" class="text-right">Total Litre: <?php echo number_format($totals['QUANTITE_RECU'], 2,',',' ')?></th>
                <!-- <th></th> -->
                <th colspan="2" class="text-right">Montant: <?php echo number_format($totals['MONTANT_CONSOMATION'], 2,',',' ')?></th>
                <!-- <th></th> -->
            </tr>
        </tfoot>
    </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() {
    // alert('aa');

document.getElementById("myForm").submit();



  }

  $('#reservation').daterangepicker({

        locale: {
        format: 'DD/MM/YYYY'
      }
    })
  $('.select').select2();
function exportlist() {
            document.getElementById("myForm").action = '<?php echo base_url();?>voiture/Exports/export_carburant_ravi';
            document.getElementById("myForm").submit();
            document.getElementById("myForm").action = "<?php echo base_url('voiture/Mettre_Carburant/listing');?>";
}
</script>
</body>
</html>
