<?php

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

require "./config.php";
require "./common.php";

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

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

    $kms =[
      "identificador"  => $_POST['identificador'],
      "fecha"          => $_POST['fecha'],
      "kms"         => $_POST['kms'],
      "litros"      => str_replace(',', '.',$_POST['litros']),
      "descuento"      => str_replace(',', '.',$_POST['descuento']),
      "precioxlitro"      => str_replace(',', '.',$_POST['precioxlitro']),
      "importe"      => str_replace(',', '.',$_POST['importe'])
    ];

    $sql = "UPDATE carburante 
            SET identificador = :identificador, 
              fecha = :fecha, 
              kms = :kms, 
              litros = :litros,
              descuento = :descuento,
              precioxlitro = :precioxlitro,
              importe = :importe
            WHERE identificador = :identificador";
  
  $statement = $connection->prepare($sql);
  $statement->execute($kms);
  } catch(PDOException $error) {
      echo $sql . "<br>" . $error->getMessage();
  }
}
if (isset($_GET['identificador'])) {
  try {
    $connection = new PDO($dsn, $username, $password, $options);
    $identificador = $_GET['identificador'];

    $sql = "SELECT * FROM carburante WHERE identificador = :identificador";
    $statement = $connection->prepare($sql);
    $statement->bindValue(':identificador', $identificador);
    $statement->execute();
    
    $kms = $statement->fetch(PDO::FETCH_ASSOC);
  } catch(PDOException $error) {
      echo $sql . "<br>" . $error->getMessage();
  }
} else {
    echo "¡Algo ha salido mal!";
    exit;
}
?>

<?php require "./cabecera.php"; ?>

<?php if (isset($_POST['submit']) && $statement) : ?>
<blockquote><?php echo escape($_POST['fecha']); ?> successfully updated.</blockquote>
<?php endif; ?>


    <div class="app-wrapper">
	    <div class="container-xl">
			 <h1 class="app-page-title">Editar repostaje</h1>
          <hr class="mb-4">
        
          <div class="app-card-body">
            <form class="settings-form" method="post">

                <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
                				 
                <?php foreach ($kms as $key => $value) : ?>					
					
					 <div class="mb-3">
					 <label class="form-label" for="<?php echo $key; ?>"><?php echo ucfirst($key); ?></label>
                <input class="form-control" type="text" name="<?php echo $key; ?>" identificador="<?php echo $key; ?>"
                    value="<?php echo escape($value); ?>" <?php echo ($key === 'identificador' ? 'readonly' : null); ?>>
                </div>
                
					 <?php endforeach; ?>
                
                <input type="submit" class="btn app-btn-primary" name="submit" value="Enviar">
            </form>
          </div>

       </div>
    </div>


<?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>