<?php
	session_start();
	include 'prijava/connect.php';
    setlocale(LC_TIME,"hr_HR");
?>
<!DOCTYPE html>
<html>
<head>
    <?php
        define('meta', TRUE);
        include 'inc/head.php';
    ?>
    <title>Prijava - KrikStudio</title>
</head>
<body class="login-screen">
    <?php
        if(isset($_SESSION['username'])) {
            echo '<script>';
            echo 'window.location.href="./"';
            echo '</script>';
        } 
    ?>
    
    <section class="login">
        <div class="overlay"></div>
        <form method="POST" class="login__form" action="">
            <img src="./assets/krikstudio.svg" />
            <div class="login__form__box">
                <input type="text" id="username" name="username" class="form-control" placeholder="Korisničko ime" required="" autofocus>
            </div>
            <div class="login__form__box">
                <input type="password" id="password" name="password" class="form-control" placeholder="Lozinka" required="">
            </div>

            <div class="login__form__submit"><input type="submit" name="login" id="login" value="PRIJAVA" /></div>
        </form>
    </section>
    
    <?php
        if (isset($_POST['login'])) {
            $user = $_POST['username'];
            $pass = $_POST['password'];
            $sql = "SELECT username, password, admin, name FROM user WHERE username = ?";
            $stmt = mysqli_stmt_init($dbc);
            if (mysqli_stmt_prepare($stmt, $sql)) {
                mysqli_stmt_bind_param($stmt, 's', $user);
                mysqli_stmt_execute($stmt);
                mysqli_stmt_store_result($stmt);
            }
            mysqli_stmt_bind_result($stmt, $userName, $userPass, $userAdmin, $userFull);
            mysqli_stmt_fetch($stmt);

            if (password_verify($_POST['password'], $userPass) && mysqli_stmt_num_rows($stmt) > 0) {
                $login_success = true;
                $_SESSION['name'] = $userFull;
                $_SESSION['username'] = $userName;
                $_SESSION['admin'] = $userAdmin;
                echo '<script>';
                echo 'window.location.href="./"';
                echo '</script>';
            } else {
                $login_success = false;
            }
        }
    ?>

    <script>
        feather.replace();
    </script>
</body>
</html>
