<?php
require_once("header.php");

if(isset($_REQUEST['data']))
{
    $data = base64_decode($_REQUEST['data']);
}
if(isset($_REQUEST['act']))
{
    $data = base64_decode($_REQUEST['data']);
    $_SESSION[$data] = "Reset Password Link Expired...";
    echo '<script>window.location="reset-password.php?data='.base64_encode($data).'"</script>';
}
if(isset($_REQUEST['id']))
{
	$id = base64_decode($_REQUEST['id']);
	$role = base64_decode($_REQUEST['role']);
    $ex = $obj->login_info($id,$role);
    if(mysqli_num_rows($ex) >0)
    {
        $row = mysqli_fetch_array($ex);
        if($row['Forgot_pass_status'] == 0)
        {
            $_SESSION[$data] = "Reset Password Link Expired.. Please Try Again..";
        }
        
    }
}

?>
<?php

$uppercase = preg_match('@[A-Z]@', $_POST['password']);
$lowercase = preg_match('@[a-z]@', $_POST['password']);
$number    = preg_match('@[0-9]@', $_POST['password']);
$spec_char = preg_match("#\W+#", $_POST['password']);


	if(isset($_POST['updatepassword']))
	{
        $_SESSION['error_msg'] = '';
        $_SESSION[$data] = '';
		
		$newpassword = $_POST['password'];
		$cpassword = $_POST['cpassword'];
		$id = $_POST['id'];
		$role = $_POST['role'];

        if(!$uppercase || !$lowercase || !$number || !$spec_char || strlen($_POST['password']) <= 8)
        {
            $_SESSION['error_msg'] = "Enter Strong Password...";
        }
		elseif($newpassword == $cpassword)
		{
			$query = $obj->reset_password($newpassword,$id,$role);
			if($query)
			{
				$_SESSION[$data] = "Password change successfully..";
                echo '<script>window.location="index.php"</script>';
			}
			else
			{
				$_SESSION['error_msg'] = "Error in password reset... Plz try again..";
			}
		}
		else
		{
			$_SESSION['error_msg'] = "Password does not match..";
		}
		
	}
?>

<body class="no-footer">
    <div id="layoutAuthentication">
        <div id="layoutAuthentication_content">
            <main>
                <div class="container">
                    <div class="row justify-content-center">
                        <div class="col-lg-5">
                            <div class="card shadow-lg border-0 rounded-lg mt-4">
                            	<?php
                            	if($_SESSION[$data] != '')
                            	{ ?>
                            		<div class="card-header"><h4 class="text-center" style="color:#00ff00"><?php echo $_SESSION[$data];?></h4></div>
                            	<?php }
                            	else
                            	{
                            	?>

                                <div class="card-header"><h3 class="text-center font-weight-light">Reset Password</h3></div>
                                <div class="card-body">
                                	
                                    <form method="POST">
										<p><font color="red"><?php echo $_SESSION['error_msg'];?></font></p>
										<input type="hidden" name="id" value="<?php echo $id;?>">
										<input type="hidden" name="role" value="<?php echo $role;?>">
                                        <div class="form-group">
                                            <label class="small mb-1">New Password</label>
                                            <input class="form-control py-4" type="password" placeholder="Enter New Password" name="password" value="<?php echo $newpassword;?>" />
                                        </div>
										
										<div class="form-group">
                                            <label class="small mb-1">Confirm Password</label>
                                            <input class="form-control py-4" type="password" placeholder="Enter Confirm Password" name="cpassword" value="<?php echo $cpassword;?>" />
                                        </div>
                                        
                                        <div class="form-group d-flex align-items-center justify-content-between mt-4 mb-0">
                                            <button class="btn btn-primary" type="submit" name="updatepassword">Update password</button>
                                             
                                    </div>   
                                    </form>
                                </div>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
        
    </div>
	

<?php require_once("footer.php");?>

<!--  After 5 min link expired.... -->
<script src="reset_pass_link.js"></script>