<?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_reparation.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 des reparations d&eacute;j&agrave; enregistr&eacute;</h3>
              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <!-- class="form-inline" -->
                <form action="<?php echo base_url('voiture/Reparation/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/Reparation/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>Garage</th>
                <th>Chauffeur</th>
                <th>Montant</th>
                <th>Options</th>
            </tr>
        </thead>
        <tbody>
          <?php

          // echo $conditions; 

          // AND CONSOMATION_DATE LIKE "2021-07%"
          $resultat=$this->Model->getRequete('SELECT rep.REPARATION_ID, rep.DATE_INSERSION, veh.PLAQUE_VEHICULE, rep.CONSOMATION_DATE, BUJUMBURA_TIME, NAVIGATEUR, OPERATING_SYSTEM, INTERNET_IP, gar.GARAGE_NOM, rep.MONTANT_CONSOMATION, rep.NUM_RECU, rep.NUM_GENERATED, uses.NOM AS USNOM, uses.PRENOM AS USPRENOM, chau.NOM AS CHNOM, chau.PRENOM AS CHPRENOM FROM voiture_reparation AS rep JOIN voiture_vehicules AS veh ON veh.VEHICULE_ID = rep.VEHICULE_ID JOIN voiture_garage AS gar ON gar.GARAGE_ID = rep.GARAGE_ID LEFT JOIN config_user AS us ON us.ID_USER = rep.USERSAVER LEFT JOIN config_user AS uses ON uses.ID_USER = us.ID_USER LEFT JOIN rh_employe AS chau ON chau.ID_EMPLOYE = rep.CHAUFFEUR_ID WHERE ID_STATUS = 1 '.$conditions.' '.$condvehicle.' '.$condstation.' ');

          
        

          $TMONTANT_CONSOMATION = 0;
          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 ");

          $TMONTANT_CONSOMATION += $key['MONTANT_CONSOMATION'];

          $details=$this->Model->getRequete('SELECT voiture_piece.PIECE_NOM, voiture_reparation_details.REPARATION_FAITE, voiture_reparation_details.MONTANT_UNITAIRE FROM voiture_reparation_details JOIN voiture_piece ON voiture_piece.PIECE_ID = voiture_reparation_details.PIECE_ID WHERE voiture_reparation_details.REPARATION_ID = '.$key['REPARATION_ID'].' ');
          $piece="";
          foreach ($details as $detail) {
            $piece.= "<tr>
            <td colspan='2'>".$detail['PIECE_NOM']."</td>
            <td colspan='2'>".$detail['REPARATION_FAITE']."</td>
            <td colspan='2'>".$detail['MONTANT_UNITAIRE']."</td>
            </tr>";
          }


        
            $pay = '';

          $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'].' - le '.$ndate.' - voiture '.$key['PLAQUE_VEHICULE'].'</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>Garage</td><td colspan="2"><b>'.$key['GARAGE_NOM'].' </b></td>
              <td>Chauffeur </td><td colspan="2"><b>'.$key['CHNOM'].' '.$key['CHPRENOM'].'</b> </td>
              
              </tr>
              <td>Enregistrée par</td><td colspan="5"><b>'.$key['USNOM'].' '.$key['USPRENOM'].' &agrave; '.$ndatet.'</b> </td>

              <tr>
              </tr>
              
              
              <tr>
              
              <td>Metadata</td><td colspan="5"><b>'.$key['BUJUMBURA_TIME'].' - '.$key['NAVIGATEUR'].' - '.$key['OPERATING_SYSTEM'].' - '.$key['INTERNET_IP'].'</b> </td>
              </tr>
              <tr>
              <td colspan="2">Piece</td>
              <td colspan="2">Reparation faite</td>
              <td colspan="2">Montant</td>
              </tr>
              '.$piece.'
              <tr>
              <td colspan="5">TOTAL</td>
              <td>'.number_format($key['MONTANT_CONSOMATION'], 2,',',' ').'</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/Reparation/annuler/'.$key['REPARATION_ID'].'').'">Annuler Enregistrement</a>
              <a type="button" href="'.base_url('voiture/Reparation/suppimer/'.$key['REPARATION_ID'].'').'" class="btn btn-danger">Suppimer Enregistrement</a>
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>';



      
          
          echo "<tr>
                <td>".$modaldeux." ".$modalun." ".$key['NUM_GENERATED']."</td>
                <td>".$key['PLAQUE_VEHICULE']."</td>
                <td>".$ndate."</td>
                <td>".$key['GARAGE_NOM']."</td>
                <td>".$key['CHNOM']." ".$key['CHPRENOM']."</td>
                <td><div class='text-right'>".number_format($key['MONTANT_CONSOMATION'], 2,',',' ')."</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">
        <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>';
          // if ($options != 2 and $options != 3 and $options != 4 ) {
          //   $modadcontent.='<div class="dropdown-divider"></div>
          // <a href="' . base_url('voiture/Reparation/modifier/' . $key['REPARATION_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 colspan="5">TOTAL</th>
                <th colspan="" class="text-right"><?php echo number_format($TMONTANT_CONSOMATION, 2,',',' ')?> </th>
                <th colspan="" class="text-right"></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();?>stock/Exports/export_carburant_ravi';
            document.getElementById("myForm").submit();
            document.getElementById("myForm").action = "<?php echo base_url('voiture/Reparation/listing');?>";
}
</script>
</body>
</html>
