<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="main.css">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

	<script type="text/javascript">
		$( "#formBox" ).submit(function( event ) {
			$.ajax({
				type:'POST',
				url:'password_reset.php',
				data:$('#formBox').serialize(),
				success:function(response){}
			});
			var form=document.getElementById('formBox').reset();
			event.preventDefault();
		});
	</script> 

	<title>Reset Password</title>
</head>
<body>
	<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
	<div class="center"><img src="../images/ic_app_logo.png" width="100px" height="100px"></div>

	<div class="login-block">
		<h1>Reset Password</h1>
		<form id="formBox" method="post">
			<input type="password" value="" placeholder="New Password" name="password" id="password"/>
			<input type="password" value="" placeholder="Confirm Password" name="cpassword" id="c_password" />
			<button name="sub" id="btn">Submit</button>
		</form>
	</div>
</body>
</html>

<?php
include("../connect.php"); 
//require "password.php";
if(isset($_POST['sub'])){
	date_default_timezone_set('Asia/Kolkata');

	$current_timestamp  = date("Y-m-d")." ".date('H:i:s');
	$user_pass=$_POST['password'];
	$user_cpass=$_POST['cpassword'];
	$token=$_REQUEST['id'];
	$result = mysqli_query($con,"select * from tbl_manager where status='Active' and token = '$token'");
	if(mysqli_num_rows($result)>0){
		$row = mysqli_fetch_assoc($result); 
		$db_timestamp  = $row['token_datetime'];

		$timediff = strtotime($current_timestamp) - strtotime($db_timestamp);
		if($timediff > 86400){ 
			echo"<script>window.alert('Your link has Link Expired')</script>";
		}else{
			if(strlen($user_pass)>=6){
				if(!strcmp($user_pass, $user_cpass)==0){
					echo"<script>window.alert('Password not matched')</script>";
				}else{
					if(mysqli_num_rows($result)<=0){
						echo"<script>window.alert('Something went wrong')</script>";
					}else{
						$email  = $row['manager_email'];
						$uq1=mysqli_query($con,"UPDATE tbl_manager SET password = '$user_pass',token = '' where manager_email='$email'");	
						echo"<script>window.alert('Password updated successfully')</script>";
						echo "<script type='text/javascript'>location.href = 'http://www.btcover.appexperts.net/api/password_reset.php';</script>";
					}
				}
			}
			else{
				echo"<script>window.alert('Password minimum 6 character long')</script>";
			}
		}
	}else{
		echo"<script>window.alert('Your link has Link Expired')</script>";
	}	
}
?>
