<?php include('../config.php'); 
if($_SESSION[SUPER_ADMIN]['id']!='') {	redirect('dashboard.php');	}					
$validate=new Validation();
if(isset($_POST['submitotp']))
{ 	
$validate->addRule($_POST['otp'],'num','OTP',true,6,6);
if($validate->validate() && count($stat)==0)
	{	
	 $iOtpExit =	$db->getVal("select token from  resetpassword_admin where emailid='".$_SESSION['reset_admin_emailid']."' and otpcode='".$_POST['otp']."' and status='0' and create_at='".date("Y-m-d")."'");
	 if($iOtpExit=='')
			{  
					$stat['error']			=	'Please Enter valid OTP code';
					$myObj['error'] 		= 	'1';
					$myObj['msg'] 		    = 	msg($stat);
					echo	$myJSON 		= 	json_encode($myObj);
					exit;	 
			}
		else {
					$aryData=array(					
 									'status'     	 	                =>	2,
									);  
					$flgIn = $db->updateAry("resetpassword_admin", $aryData , "where emailid='".$_SESSION['reset_admin_emailid']."'");				
	   
					$stat['success']		=	'OTP match  successfully for reset password.';
					$myObj['error'] 		= 	'0';
					$myObj['msg'] 			= 	$iOtpExit;
					echo	$myJSON 		= 	json_encode($myObj);
					exit;
			}
		} else {
					$stat['error'] 			= 	$validate->errors();
					$myObj['error'] 		= 	'1';
					$myObj['msg'] 			= 	msg($stat);
					echo	$myJSON 		= 	json_encode($myObj);
					exit;
		}
	}
elseif(isset($_POST['subpass']))
{ 	
$validate->addRule($_POST['newpass'],'','New Password',true,6,14);
$validate->addRule($_POST['renewpass'],'','Re Enter Password',true,6,14);
if($validate->validate() && count($stat)==0)
	{	
	if($_POST['newpass']!=$_POST['renewpass']) { 
					$stat['error']			=	'Re-enter password do not match';
					$myObj['error'] 		= 	'1';
					$myObj['msg'] 		    = 	msg($stat);
					echo	$myJSON 		= 	json_encode($myObj);
					exit;	 
		} else {
			
			$iUserId =	$db->getVal("select userid from  resetpassword_admin where emailid='".$_SESSION['reset_admin_emailid']."' and token='".$_POST['token']."' and status='2' and create_at='".date("Y-m-d")."'");
			if($iUserId=='')
				{
							$stat['error']			=	'This link is already expired.';
							$myObj['error'] 		= 	'1';
							$myObj['msg'] 			= 	msg($stat);
							echo	$myJSON 		= 	json_encode($myObj);
							exit;
				} else {
							$aryData=array(					
										'password'                 =>	md5($_POST['newpass']),
										   );  
						   $flgIn = $db->updateAry("admin_login", $aryData , "where id='".$iUserId."'");
					
							$aryData=array(					
										'status'                 =>	1,
										 );  
							$flgIn = $db->updateAry("resetpassword_admin", $aryData , "where token='".$_POST['token']."'");
							unset($_SESSION['reset_admin_emailid']); 			
							$stat['success']		=	'Your Password have been updated successfully.';
							$myObj['error'] 		= 	'0';
							$myObj['msg'] 			= 	msg($stat);
							echo	$myJSON 		= 	json_encode($myObj);
							exit;
				}
			}
	} else {
				$stat['error'] 			= 	$validate->errors();
				$myObj['error'] 		= 	'1';
				$myObj['msg'] 		= 	msg($stat);
				echo	$myJSON 		= 	json_encode($myObj);
				exit;
	}
}
?>
<!DOCTYPE html>
<html>
<head>
<?php include('inc.meta.php'); ?>
<style>
.alert-error {
	color: #a94442;
	background-color: #f2dede;
	border-color: #ebccd1;
}
body, html {
    height:100%;
}
</style>
<style>
.panel-heading {
    border: none!important;
    padding:30px 0px 20px 0px;
    text-align:center;
}
.loginpanel {
    background-color: #fff;
    padding:30px 25px 50px 25px;
    border: solid 1px #ddd;
    width: 100%;
   
    margin-top: 30px;
    background-size:100%;
    background-repeat: no-repeat;
    border-radius: 7px;
}
.loginpanel .form-group {
    margin-bottom: 30px;
}
.loginpanel .form-control {
	height:42px;
	padding:0px 30px;
	font-size: 15px;
	border: 1px solid #e3e3e3;
}
.panel-heading h3 {
	font-weight: 600;
	font-size:20px;
	padding: 0px;
	margin: 0px;
	margin-top: 20px;
	    color: black;
}
.panel-heading h6 {
	font-weight: 400;
	font-size: 14px;
	padding: 0px;
	margin: 0px;
	margin-top:0px;
    margin-bottom:15px;
}
.alert-error {
	color: #a94442;
	background-color: #f2dede;
	border-color: #ebccd1;
}
.sig {
	text-align: Center;
	font-size: 16px;
	margin-top: 0;
	color: #00000073;
}
.btn-forpass {
    float: right;
    margin-bottom: 20px;
    font-weight: 500;
	font-size: 15px;
	text-decoration: underline;
}
.account{
    text-align:center;
    margin-bottom: 20px;
    font-weight: 500;
	font-size: 16px;
    margin-top: 25px;
}
.btn-blue {
    background: #6658dd;
    border: 1px solid #6658dd;
    padding: 10px 25px;
    font-size: 16px;
    color: #fff;
    
    width: 100%;
    border-radius:7px;
}
.btn-blue:hover{
    background: #00138d;
	border: 1px solid #303f9f;

}
.viwhead {
    font-size: 16px;
	font-weight: 400;
    color: #8c8c8c;
}
.txtbox {
    position: relative;
}
.txtbox i{
    position: absolute;
    top: 14px;
    left: 15px;
    font-size:16px;
	color: #b5b5b5;
}
.account-pages-log-bg{
  background: linear-gradient(0deg, #00000014, #00000017), url(../uploads/login-bg.jpg)!important;
    background-position: center !important;
    background-repeat: no-repeat !important;
    background-size: cover !important;
    height: 100%;
}
.logtxt {
	position:relative;
}
.logtxt i {
	position: absolute;
	bottom: 14px;
	left: 8px;
	font-size: 14px;
	color:#4a4a4a;
}
</style>
</head>
<body class="account-pages-log-bg">
<div class="account-pages">
<div class="clearfix"></div>
<div class="wrapper-page ">
  <div class=" card-box" style="background:transparent; border:none">
    <div class="panel-body">
      <div class="col-md-4"></div>
      <div class="col-md-3">
        <p style="text-align:center"><img src="../uploads/logo.png"></p>
        <div class="loginpanel">
          <div class="panel-heading">
            <div class="container-fluid pt-40 pb-60">
              <?php if($_GET['action']=='genrateotp')  { 
	$iUserId =	$db->getVal("select userid from  resetpassword_admin where emailid='".$_SESSION['reset_admin_emailid']."' and status='0' and create_at='".date("Y-m-d")."'");
	if($iUserId==''){	 ?>
              <div class="container">
                <div class="row ">
                  <div class="col-lg-12 col-md-12 col-sm-12">
                    <h2 class="linkexpiry">This url do not exit or link expired</h2>
                  </div>
                   <div class="col-lg-12 col-md-12 col-sm-12 pt-35">
                  <a href="index.php" >Click here to login?</a>
                  </div>
                </div>
              </div>
              <?php  } else { ?>
              <div class="loginsect">
                <form action="" method="post" id="myotp" autocomplete="off" onSubmit="subotp(event);">
                  <div class="col-lg-12 col-md-12 col-sm-12">
                    <h2 class="pghead">Enter OTP Code</h2>
                    <p class="pgsunhead">Please enter one time code sent on your email id.</p>
                  </div>
                  <div class="col-lg-12 col-md-12 col-sm-12x">
                    <p class="logtxt"><i class="fa fa-envelope-o"></i>
                      <input  type="number" class="form-control" name="otp" placeholder="Enter OTP..." maxlength="6" minlength="6">
                    </p>
                  </div>
                  <div class="col-lg-12 col-md-12 col-sm-12 pt-15">
                    <input type="hidden" name="submitotp" value="submitotp" />
                    <div id="showerror"></div>
                    <button type="submit" class="btn btn-logins mt-10"> <i class="fa fa-paper-plane-o"></i> Submit</button>
                  </div>
                  <div class="col-lg-12 col-md-12 col-sm-12 pt-35">
                  <a href="index.php" style="text-align:center">Click here to login?</a>
                  </div>
                </form>
              </div>
              <?php }  }  
elseif($_GET['action']=='newpass') { 
	$iUserId =	$db->getVal("select userid from  resetpassword_admin where emailid='".$_SESSION['reset_admin_emailid']."' and token='".$_GET['token']."' and status='2' and create_at='".date("Y-m-d")."'");
	if($iUserId==''){	 ?>
              <div class="container">
                <div class="row ">
                  <div class="col-lg-12 col-md-12 col-sm-12">
                    <h2 class="linkexpiry">This url do not exit or link expired</h2>
                  </div>
                   <div class="col-lg-12 col-md-12 col-sm-12 pt-35">
                  <a href="index.php" >Click here to login?</a>
                  </div>
                </div>
              </div>
              <?php  } else { ?>
              <div class="loginsect">
                <form action="" method="post" id="mypass" autocomplete="off" onSubmit="subnewpass(event);">
                  <div class="col-lg-12 col-md-12 col-sm-12">
                    <h2 class="pghead">Update your password</h2>
                    <p class="pgsunhead">Please enter new password to reset your password</p>
                  </div>
                  <div class="col-lg-12 col-md-12 col-sm-12x">
                    <p class="logtxt"><i class="ti-lock"></i>
                      <input  type="password" class="form-control" name="newpass" placeholder="Enter New Password">
                    </p>
                  </div>
                  <div class="col-lg-12 col-md-12 col-sm-12x">
                    <p class="logtxt"><i class="ti-lock"></i>
                      <input  type="password" class="form-control" name="renewpass" placeholder=" Re-Enter New Password">
                    </p>
                  </div>
                  <div class="col-lg-12 col-md-12 col-sm-12 pt-15">
                    <input type="hidden" name="subpass" value="subpass" />
                    <input type="hidden" name="token" value="<?php echo $_GET['token']; ?>" />
                    <div id="showerror"></div>
                    <button type="submit" class="btn btn-logins mt-10"> <i class="fa fa-paper-plane-o"></i> Submit</button>
                  </div>
                  <div class="col-lg-12 col-md-12 col-sm-12 pt-35">
                  <a href="index.php" >Click here to login?</a>
                  </div>
                </form>
              </div>
              <?php } }  ?>
            </div>
          </div>
        </div>
        <div class="col-md-4"></div>
      </div>
    </div>
  </div>
</div>
</div>
<script>
function subotp(e)
	{	e.preventDefault();
		var form 	= 	document.getElementById("myotp");
		var XHR 	= 	new XMLHttpRequest();
		var FD 		= 	new FormData( form );                
		XHR.addEventListener( "load", function(event) {
		var ResData 	= 	this.response;
		//alert(ResData);
		var JsonData 	= 	JSON.parse(ResData);
		//alert(JsonData.error);
		 if(JsonData.error==1)
			{
				 document.getElementById("showerror").innerHTML= JsonData.msg;
			} else {
					window.location.href = "forgot-passotp.php?action=newpass&token="+JsonData.msg;
			}
		});
		XHR.open("POST", "<?php echo $FileName; ?>");
		XHR.send(FD);
	}
</script>
<script>
function subnewpass(e)
	{	e.preventDefault();
		var form 	= 	document.getElementById("mypass");
		var XHR 	= 	new XMLHttpRequest();
		var FD 		= 	new FormData( form );                
		XHR.addEventListener( "load", function(event) {
		var ResData 	= 	this.response;
		//alert(ResData);
		var JsonData 	= 	JSON.parse(ResData);
		//alert(JsonData.error);
		 if(JsonData.error==1)
			{
				 document.getElementById("showerror").innerHTML= '<p style="color:red">'+JsonData.msg+'</p>';
			} else {
					document.getElementById("mypass").reset();
					document.getElementById("showerror").innerHTML= '<p style="color:green">'+JsonData.msg+'</p>';
				//	window.location.href = "otppage?action=newpass&token";
			}
		});
		XHR.open("POST", "<?php echo $FileName; ?>");
		XHR.send(FD);
	}
</script>
</body>
</html>