<?php

require_once 'db.php';
$db = new Db();

if(isset($_POST['prijava']))
{
    $user = $_POST['korisnicko'];
    $pass = $_POST['lozinka'];
    
    $korisnik = $db->proveriKorisnika($user, $pass);
    if($korisnik)
    {
        session_start();
        setcookie("user", $user, time() + 86400 * 2);
        if(isset($_GET['zapamti']))
            setcookie ("pass", $pass, time() + 86400 * 2);
        $_SESSION['autorizovan'] = $korisnik['id'];
        header("Location: index.php");
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Polovni automobili</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

    <div class="container-fluid p-5 bg-primary text-white text-center">
    <h1>Polovni automobili</h1>
    <p>Najbolja ponuda polovnih automobila u Srbiji</p> 
    </div>

    <div class="container mt-5">
        <h2>Prijavite se</h2>
        <form method="POST" >
            <input type="text" name="korisnicko" id="korisnicko" placeholder="korisnicko ime" value="<?php if(isset($_COOKIE['user'])) echo $_COOKIE['user']; ?>"><br>
            <input type="password" name="lozinka" id="lozinka" placeholder="lozinka" value="<?php if(isset($_COOKIE['pass'])) echo $_COOKIE['pass']; ?>"><br>
            <input type="checkbox" name="zapamti">Zapamti<br> <br/>
            <input type="submit" class="btn btn-primary" value="Prijavi se" name="prijava">

        </form>
        <?php 
            if (isset($_POST['prijava']) && !$korisnik) {
                echo "<p style='color:red;'>Uneto nepostojeće korisnicko ime!</p>";
            }
        ?>
    </div>
</body>
</html>
