<?php
  
/**
* Use an HTML form to create a new entry in the
* users table.
*
*/

require './config.php';
require './common.php';

$success = null;

if (isset($_POST["submit"])) {
  if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();

  try {
    $connection = new PDO($dsn, $username, $password, $options);
  
    $identificador = $_POST["submit"];

    $sql = "DELETE FROM carburante WHERE identificador = :identificador";

    $statement = $connection->prepare($sql);
    $statement->bindValue(':identificador', $identificador);
    $statement->execute();

    $success = "Repostaje borrado con éxito";

    $log_msg='BORRA REPOSTAJE-> ID: '.$identificador;

    escribe_log($log_msg);

  } catch(PDOException $error) {
    echo $sql . "<br>" . $error->getMessage();
  }
}

try {
  $connection = new PDO($dsn, $username, $password, $options);

  $sql = "SELECT * FROM carburante ORDER BY fecha DESC";

  $statement = $connection->prepare($sql);
  $statement->execute();

  $result = $statement->fetchAll();
} catch(PDOException $error) {
  echo $sql . "<br>" . $error->getMessage();
}

require "./cabecera.php";?>

    <div class="app-wrapper">
	    
	    <div class="app-content pt-3 p-md-3 p-lg-4">
		    <div class="container-xl">
			    
			    <div class="row g-3 mb-4 align-items-center justify-content-between">
				    <div class="col-auto">
			            <h1 class="app-page-title mb-0">Borrar repostajes</h1>
				    </div>
			    </div><!--//row-->			
				
				<div class="tab-content" id="orders-table-tab-content">
			        <div class="tab-pane fade show active" id="orders-all" role="tabpanel" aria-labelledby="orders-all-tab">
					    <div class="app-card app-card-orders-table shadow-sm mb-5">
						    <div class="app-card-body">
							    <div class="table-responsive">
                           <form method="post">
                             <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">							    
							        <table class="table app-table-hover mb-0 text-left">
										<thead>
											<tr>
												<th class="cell">#</th>
                        				<th class="cell">Fecha</th>
                        				<th class="cell">Vehículo</th>
                        				<th class="cell">Kilómetros</th>
                        				<th class="cell">Litros</th>
                        				<th class="cell">Descuento</th>
                        				<th class="cell">Precio/litro</th>
                        				<th class="cell">Importe</th>
                        				<th class="cell">Borrar</th>
											</tr>
										</thead>
										<tbody>
												<?php foreach ($result as $row) : ?>
                    						<tr>
                        					<td class="cell"><?php echo escape($row["identificador"]); ?></td>
                        					<td class="cell"><?php echo escape($row["fecha"]); ?></td>
                        					<td class="cell"><?php echo escape($row["vehiculo"]); ?></td>
                        					<td class="cell"><?php echo escape($row["kms"]); ?></td>
                        					<td class="cell"><?php echo escape(str_replace('.', ',',$row["litros"])); ?></td>
                        					<td class="cell"><?php echo escape(str_replace('.', ',',$row["descuento"])); ?></td>
                        					<td class="cell"><?php echo escape(str_replace('.', ',',$row["precioxlitro"])); ?></td>
                        					<td class="cell"><?php echo escape(str_replace('.', ',',$row["importe"])); ?></td>
                        					<td class="cell"><button type="submit" name="submit"
                                           value="<?php echo escape($row["identificador"]); ?>">Borrar</button></td>
                    						</tr>
                    						<?php endforeach; ?>
										</tbody>
									</table>
									</form>
						        </div><!--//table-responsive-->
						       
						    </div><!--//app-card-body-->		
						</div><!--//app-card-->
						
			        </div><!--//tab-pane-->
				</div><!--//tab-content-->
				
				
			    
		    </div><!--//container-fluid-->
	    </div><!--//app-content-->
	    	    
<?php require './templates/footer.php';?>
   <!-- Javascript -->          
    <script src="assets/plugins/popper.min.js"></script>
    <script src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>  
    
    
    <!-- Page Specific JS -->
    <script src="assets/js/app.js"></script> 
</body>
</html>

