<?php 
session_start();
include 'lib/dbcon.php';

if (isset($_REQUEST['mail'])) {


	$email = $_POST['email'];

	$email_query = "select * from admin where email='$email'";
	$query = mysqli_query($con,$email_query);
	if (mysqli_num_rows($query) > 0) {
		$row = mysqli_fetch_array($query);
		$username = $row['username'];
		//$token   = $userdata['token'];

		$token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM0123456789!$/()*';
        $token = str_shuffle($token);
        $token = substr($token, 0,10);
        $qry1 = "UPDATE `admin` SET token = '$token' WHERE username = '$username'";
         $run1 = mysqli_query($con,$qry1);

        $toEmail = $row['email'];
        $subject = 'Password Reset Notification';
        $body    = '<h2>Password Reset Request</h2>
                    <h4>Name '.$row["employe_name"].' '.$row["last_name"].'</h4>
                    <h4>In order to reset your password, please click on the link below : <br></h4>
                    <br>
                    <a href="http://localhost/etax/admin/reset-password.php?username='.$row['username'].'&&token='.$token.'">Click  here to reset your password</a>';

        // Email Headers
        $headers = "MIME-Version: 1.0" ."\r\n";
        $headers .="Content-Type:text/html;charset=UTF-8" . "\r\n";

        // Additional Headers
        $headers .= "From: " .'EMS'. "<".'navjeetsingh1811@gmail.com'.">". "\r\n";

        if (mail($toEmail, $subject, $body, $headers)) {

            $_SESSION['msg_scs'] = "Password Reset Link Sent Your Email Address";
           
            header('location:forgot.php');
        }
        else{

       $_SESSION['msg_err'] = "Password Reset Link Sent Your Email Address";
       header('location:fotgot.php');
        }

	}
	else {
		echo "email not match";
	}
	

	

	
}


?>