<?php
	session_start();
	include 'prijava/connect.php';
    date_default_timezone_set("Europe/Zagreb");
?>
<!DOCTYPE html>
<html>
<head>
    <?php
        define('meta', TRUE);
        include 'inc/head.php';
    ?>
    <script src="js/reg.js"></script>
    <title>Registracija - KrikStudio</title>
</head>
<body>
    <?php
        if(!isset($_SESSION['username']) || $_SESSION['admin'] == 0) {
            echo '<script>';
            echo 'window.location.href="./"';
            echo '</script>';
        }

        define('nav', TRUE);
        include('inc/nav.php');
    ?>

    <section class="registration">
        <div class="wrapper">
            <a class="back-btn" href="korisnici"><i data-feather="arrow-left"></i> Povratak</a>
            <h2>REGISTRACIJA NOVOG KORISNIKA</h2>
            <form method="POST" class="registration__form" action="" name="reg">
                <input type="text" id="name" name="name" class="form_name" placeholder="Ime" required/>

                <input  type="text" id="username" name="username"  class="form_username" placeholder="Korisničko ime" required/>

                <div class="registration__form-pass">
                    <input type="password" id="password" name="password" class="form-control" placeholder="Početna lozinka" required="">
                    <label><input type="checkbox" onclick="showPass1()"><i data-feather="eye" class="label"></i></label>
                </div>

                <div class="registration__form__submit"><input type="submit" name="register" id="register" value="REGISTRACIJA" /></div>
            </form>
        </div>
    </section>

    <?php
        if ($dbc && isset($_POST['register'])) {
            $name = $_POST['name'];
            $username = $_POST['username'];
            $password = $_POST['password'];
            $hashed_password = password_hash($password, CRYPT_BLOWFISH);
            $register = false;

            $sql = "SELECT * FROM user WHERE username = ?";
            $stmt = mysqli_stmt_init($dbc);
            if (mysqli_stmt_prepare($stmt, $sql)) {
                mysqli_stmt_bind_param($stmt, 's', $username);
                mysqli_stmt_execute($stmt);
                mysqli_stmt_store_result($stmt);
            }
            if (mysqli_stmt_num_rows($stmt) > 0) {
                echo "<p style='color:#EA212D;font-weight:bold;padding-bottom:10px'>Korisničko ime već postoji!</p>";
            } else {
                $sql = "INSERT INTO user (name, username, password) VALUES (?, ?, ?)";
                $stmt = mysqli_stmt_init($dbc);
                if (mysqli_stmt_prepare($stmt, $sql)) {
                    mysqli_stmt_bind_param($stmt, 'sss', $name, $username, $hashed_password);
                    mysqli_stmt_execute($stmt);
                    $register = true;
                    if($register == true) {
                        echo '<script>';
                        echo "Swal.fire(
                            'Novi korisnik je dodan!',
                            '',
                            'success',
                            ).then(function() {
                                window.location.href = './korisnici';
                            });";
                        echo '</script>';
                    }
                }
            }
        }
        mysqli_close($dbc);
    ?>
        
    <?php define('footer', TRUE);include 'inc/footer.php'; ?>

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