<?php
include '../functions.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>Syndash - Admin Template</title>
    <!--favicon-->
    <link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
    <!-- loader-->
    <link href="assets/css/pace.min.css" rel="stylesheet" />
    <script src="assets/js/pace.min.js"></script>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&amp;family=Roboto&amp;display=swap" />
    <!-- Icons CSS -->
    <link rel="stylesheet" href="assets/css/icons.css" />
    <!-- App CSS -->
    <link rel="stylesheet" href="assets/css/app.css" />
    <script type="text/javascript" src="../js/sweetalert.min.js"></script>
</head>

<body class="bg-forgot">
    <!-- wrapper -->
    <div class="wrapper">
        <div class="authentication-forgot d-flex align-items-center justify-content-center">
            <div class="card shadow-lg forgot-box">
                <div class="card-body p-md-5">
                    <div class="text-center">
                        <img src="assets/images/icons/forgot-2.png" width="140" alt="" />
                    </div>
                    <form action="" method="post">
                        <h4 class="mt-5 font-weight-bold">Reset Password</h4>
                        <div class="form-group mt-5">
                            <label>Password</label>
                            <input type="password" class="form-control form-control-lg radius-30" name="password" placeholder="Enter your new password" />
                        </div>
                        <div class="form-group">
                            <label>Confirm Password</label>
                            <input type="password" class="form-control form-control-lg radius-30" name="confirm password" placeholder="Confirm password" />
                        </div>
                        <button type="submit" class="btn btn-primary btn-lg btn-block radius-30" name="reset">Reset</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- end wrapper -->
</body>
</html>

<?php
if (isset($_POST['reset']) == true) {
    $password = sha1($_POST['password']);
    $confirm_password = sha1($_POST['confirm_password']);

    if (isset($_GET['token']) == true AND isset($_GET['email']) == true) {
        $email = urldecode($_GET['email']);
        $token = urldecode($_GET['token']);

        $sql = $conn->query("SELECT * FROM administrator WHERE email = '$email' AND reset_token = '$token' ");

        if ($sql->num_rows == 0 ) {
            echo "<script>swal({text:'Wrong Token Or Email Address', icon:'error'})</script>";
        }else{
            if ($password != $confirm_password) {
                echo "<script>swal({text:'Password Does Not Match', icon:'error'})</script>";
            }else{
                $conn->query("UPDATE administrator SET password = '$password' WHERE email = '$email' AND reset_token = '$token' ");
                $conn->query("UPDATE administrator SET reset_token = NULL WHERE email = '$email' ");
                echo "<script>swal({text:'Password Reset done successfully, Kindly login with your new password', icon:'success'})
                    .then(function(){
                        window.location = 'login'
                        })
                    </script>";
            }
        }
    }
}