<?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">Forgot Password?</h4>
						<p class="text-muted">Enter your registered email ID to reset the password</p>
						<div class="form-group mt-5">
							<label>Email id</label>
							<input type="text" class="form-control form-control-lg radius-30" name="email" placeholder="example@user.com" />
						</div>
						<button type="submit" class="btn btn-primary btn-lg btn-block radius-30" name="send">Send</button>
						<a href="login" class="btn btn-link btn-block"><i class='bx bx-arrow-back mr-1'></i>Back to Login</a>
					</form>
				</div>
			</div>
		</div>
	</div>
	<!-- end wrapper -->
</body>
</html>

<?php
if (isset($_POST['send']) == true) {
	$email = $_POST['email'];
	$token = md5(mt_rand());

	if (empty($email) == true) {
		echo "<script>swal({text:'Please enter your email address', icon:'error'})</script>";
	}else{
		$sel_mail = $conn->query("SELECT * FROM administrator WHERE email = '$email' ");
		if ($sel_mail->num_rows == 0) {
			echo "<script>swal({text:'Email not found', icon:'error'})</script>";
		}else{
			$value = mysqli_fetch_array($sel_mail);
			$username = $value['username'];
	        $to = $email;
	        $email_code = urlencode($email);
	        $token_code = urlencode($token);

	        $subject = "PASSWORD RESET ON ".strtoupper(sitename)." ADMIN";
	        $msg = "<html>
	        <head>
	          <title>Reset Your Password on ".sitename." ADMIN</title>
	        </head>
	        <body>
	        <h2 class='text-center'><b>Reset Password</b></h2>
	           <a href='".sitelink."/dashone/reset-password?email=".$email."&token=".$token."'><p>Click Here</a></p>
	         <br>
	         <p>Someone recently requested to reset your account password from ".sitename." ADMIN. Click the link above or open it in your browser to complete the password reset </p>
	         <p>If the link does not work, try copying the URL below directly into your browser address bar.</p>
	         <p>".sitelink."/dashone/reset-password?email=".$email."&token=".$token."</p>
	         If this request is not from you , Ignore this Message


	         Sincerely, 

	         ".strtoupper(sitename)." TEAM

	         <i>This message was generated automatically, <br> If you need help or have questions, email</i>
	         <a href='mailto: ".sitemail."'>".sitemail."</a>
	        </body>
	        </html>
	        ";
	        // echo $msg;

	        // Additional headers
         	$header = 'From: TRUSTCHASEFINANCE <support@trustchasefinance.com>' . "\r\n";
         	$header .= 'Content-type: text/html; charset=iso-8859-1';

	        $query = $conn->query("UPDATE administrator SET reset_token = '$token' WHERE email = '$email' ");
	        $send = mail($to, $subject, $msg, $header);
	        if (isset($send) == true) {
	            echo "<script>swal({text:'Password Reset Link sent successfully, kindly check your Inbox and follow the steps there. Thanks', icon:'success'})</script>";
	        }else{
	            echo "<script>swal({text:'An Error Occurred! Contact Support'})</script>";
	        }
		}
	}
}