<?php
	session_start();
	include 'prijava/connect.php';
    setlocale(LC_TIME,"hr_HR");
?>
<!DOCTYPE html>
<html>
<head>
    <?php
        define('meta', TRUE);
        include 'inc/head.php';
    ?>
    <script src="js/reg.js"></script>
    <title>Promjena lozinka - KrikStudio</title>
</head>
<body>
    <?php
        if(!isset($_SESSION['username'])) {
            echo '<script>';
            echo 'window.location.href="./"';
            echo '</script>';
        }

        $username = $_SESSION['username'];
        define('nav', TRUE);
        include('inc/nav.php');
    ?>
    
    <section class="change-pass">
        <div class="wrapper">
            <h2>PROMJENA LOZINKE</h2>
            <p>Što prije promijeni početnu lozinku.</p>
            <form method="POST" class="change-pass__form" action="" onsubmit="check(event)">
                <div class="pass">
                    <input type="password" id="newPassword" name="password" class="form-control" placeholder="Nova lozinka" required="">
                    <label><input type="checkbox" onclick="showPass2()"><i data-feather="eye" class="label"></i></label>
                </div>
                <div class="pass">
                    <input type="password" id="newPasswordRepeat" class="form-control" placeholder="Ponovi lozinku" onkeyup='check();' required="">
                    <label><input type="checkbox" onclick="showPass3()"><i data-feather="eye" class="label"></i></label>
                </div>

                <span id='message'></span>

                <div class="change-pass__form__submit"><input type="submit" id="login" name="change" value="AŽURIRAJ" /></div>

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

                        $query = "UPDATE user SET password = '$hashed_password' WHERE username = '$username'";
                        $result = mysqli_query($dbc,$query) or die('Error querying database.');
                        if($result) {
                            $register = true;
                        }

                        if($register == true) {
                            echo '<script>';
                            echo "Swal.fire(
                                'Lozinka je ažurirana!',
                                '',
                                'success',
                                ).then(function() {
                                    window.location.href = './promjena-lozinke';
                                });";
                            echo '</script>';
                        }
                    }
                    mysqli_close($dbc);
                ?>
            </form>
        </div>
    </section>

    <?php define('footer', TRUE);include 'inc/footer.php'; ?>
        
    <script>
        feather.replace();
    </script>
</body>
</html>