<?php
session_start();
$conn = new mysqli("localhost", "root", "", "blagajna");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $korisnik = $_POST["korisnicko_ime"];
    $lozinka = $_POST["lozinka"];
    $stmt = $conn->prepare("SELECT * FROM poslovnice WHERE korisnicko_ime = ?");
    $stmt->bind_param("s", $korisnik);
    $stmt->execute();
    $rez = $stmt->get_result();
    $korisnik_data = $rez->fetch_assoc();
    if ($korisnik_data && password_verify($lozinka, $korisnik_data["lozinka"])) {
        $_SESSION["poslovnica_id"] = $korisnik_data["id"];
        $_SESSION["naziv"] = $korisnik_data["naziv"];
        $_SESSION["uloga"] = $korisnik_data["uloga"];
        if ($_SESSION['uloga'] === 'admin') {
            header("Location: admin.php");
        } elseif ($_SESSION['uloga'] === 'kazne') {
            header("Location: dashboard_kazne_sve_poslovnice.php");
        } else {
            header("Location: dashboard.php");
        }
    } else {
        echo "Neispravno korisnicko ime ili lozinka.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Prijava u blagajnu</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: 400px;">
    <div class="card shadow">
      <div class="card-body">
        <h3 class="card-title text-center mb-4">Prijava</h3>

        <?php if (isset($greska)): ?>
          <div class="alert alert-danger"><?= $greska ?></div>
        <?php endif; ?>

        <form method="POST">
          <div class="mb-3">
            <label for="korisnicko_ime" class="form-label">Korisničko ime</label>
            <input type="text" class="form-control" name="korisnicko_ime" id="korisnicko_ime" required>
          </div>
          <div class="mb-3">
            <label for="lozinka" class="form-label">Lozinka</label>
            <input type="password" class="form-control" name="lozinka" id="lozinka" required>
          </div>
          <div class="d-grid">
            <button type="submit" class="btn btn-primary">Prijava</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</body>
</html>
