<?php 
include('../config.php');
$validate=new Validation(); 
if(isset($_POST['addnewrecord']))
{ 
$validate->addRule($_POST['emailid'],'email','Email Id',true);
if($validate->validate() && count($stat)==0)
	{	
		$iUserId	=	$db->getRow("select fullname, id from  admin_login where emailid='".$_POST['emailid']."'");
		if($iUserId['id']=='')
			{  
					$stat['error']			=	'This email id do not exit.';
					$myObj['error'] 		= 	'1';
					$myObj['msg'] 			= 	msg($stat);
					echo	$myJSON 		= 	json_encode($myObj);
					exit;	 
			} else {
					$iLastInserId=$db->getVal("select id from  resetpassword_admin order by id desc limit 0, 1")+1;
					$iTokenNumber=rand(1000,10000000).$iLastInserId;
					$iOTPCode	=	randomnum(6);
					$aryData=array(	
									'token'     	 	         		=>	$iTokenNumber,
									'userid'     	 	         		=>	$iUserId['id'],
									'emailid'     	 	         		=>	$_POST['emailid'],
 									'status'     	 	                =>	0,
									'create_at'     	 	            =>	date('Y-m-d'),
									'otpcode'     	 	         		=>	$iOTPCode,
										);  
					$iLastInsert = $db->insertAry("resetpassword_admin",$aryData);
					//echo $db->getLastQuery();	 exit;
					$_SESSION['reset_admin_emailid']=$_POST['emailid'];
					
					
					$iMessage  = '<table style="width:100%;font-family:Roboto, sans-serif, arial;"><tr><td algin="center" align="center"><div style="width:100%;margin-top:30px;">';
					$iMessage  .= '<div style="width:100%;max-width:400px; background:#fff; border:1px solid #0000001c; padding:50px;">';
					$iMessage .= '<p style="text-align:center;"><img src="'.SITE_URL.'images/logo.png" style="width:150px"></p>';
					$iMessage .= '<p style="margin-top:50px;font-size:16px;text-align: left;">Hello '.$iUserId['username'].',</p>';
					$iMessage .= '<p style="font-size:16px;text-align:left;margin:0px;">We received a request to reset your aratb password.</p>';
					$iMessage .= '<p style="font-size:16px;text-align: left;margin:0px;margin-top:5px;">Enter the following password reset code:</p>';
					$iMessage .= '<p style="text-align:center;margin:50px 0px;"><a style="background: #ffedec;border: 1px solid #ff0b02;color: black;padding: 14px 20px;text-decoration: none;border-radius:4px;" >'.$iOTPCode.'</a></p>';
					$iMessage .= '<p  style="margin-top:50px;font-size:16px;text-align: left;">Thank you <br> aratb.com</p>';
					$iMessage .= '</div>';
					$iMessage .= '</div>';
					
					sendmailfromlocal($_POST['emailid'],  'Reset your password - aratb.com', $iMessage);
	
					$stat['success']		=	'Email sent successfully. Please check your mail for reset password.';
					$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%;
}
</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">
              <h3>Welcome Back :) </h3>
              <h6>Please enter your username and password:</h6>
              <?php echo msg($stat) ; ?> </div>
            <form class="form-horizontal" id="myForm" action="" onSubmit="forgotpass(event);" method="post">
              <div class="form-group ">
                <div class="col-xs-12 viwhead">Enter Email Id</div>
                <div class="col-xs-12 txtbox"> <i class="fa fa-envelope-o"></i>
                  <input class="form-control" type="email"  required placeholder="Enter Email Id" name="emailid">
                </div>
              </div>
               
              <div class="form-group">	
                <div class="col-xs-12 mt-20" id="showerror"></div>
                <div class="col-xs-12 mt-20">
                <input type="hidden" name="addnewrecord" value="addnewrecord" />
                  <button  class="btn-blue" type="submit" name="addnewrecord"><i class="fa fa-sign-in"></i> Log In</button>
                </div>
              </div>
            </form>
          </div>
        </div>
        <div class="col-md-4"></div>
      </div>
    </div>
  </div>
</div>
<script>
function forgotpass(e)
	{
		 e.preventDefault();
		var form 	= 	document.getElementById("myForm");
		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 {
					window.location.href = "forgot-passotp.php?action=genrateotp";
			}
		});
		XHR.open("POST", "<?php echo $FileName; ?>");
		XHR.send(FD);
	}
</script>
</body>
</html>