<?php
session_start();
$conn = new mysqli("localhost", "root", "", "blagajna");

$id = intval($_GET['id']);
$trx = $conn->query("SELECT * FROM transakcije WHERE id = $id")->fetch_assoc();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $opis = $_POST["opis"];
    $iznos = floatval($_POST["iznos"]);

    $stmt = $conn->prepare("UPDATE transakcije SET opis = ?, iznos = ? WHERE id = ?");
    $stmt->bind_param("sdi", $opis, $iznos, $id);
    $stmt->execute();

    header("Location: dashboard.php");
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Uredi transakciju</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5" style="max-width: 600px;">
  <div class="card shadow">
    <div class="card-body">
      <h3 class="mb-4">Uredi transakciju</h3>
      <form method="POST">
        <div class="mb-3">
          <label for="opis" class="form-label">Opis:</label>
          <input type="text" name="opis" class="form-control" id="opis" value="<?= htmlspecialchars($trx['opis']) ?>" required>
        </div>
        <div class="mb-3">
          <label for="iznos" class="form-label">Iznos (EUR):</label>
          <input type="number" step="0.01" name="iznos" class="form-control" id="iznos" value="<?= $trx['iznos'] ?>" required>
        </div>
        <div class="d-flex justify-content-between">
          <a href="dashboard.php" class="btn btn-secondary">Natrag</a>
          <button type="submit" class="btn btn-primary">Spremi promjene</button>
        </div>
      </form>
    </div>
  </div>
</div>
</body>
</html>
