<?php	

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;


	if (empty($_POST['auth'])&&empty($_POST['reg'])&&!empty($_POST['fusername'])) {
		if (empty($_POST['fusername']) || empty($_POST['email'])) {
			if (empty($_POST['fusername'])) {
				echo getPanel("danger","Missing Field","Username is empty.");
			}
			if (empty($_POST['email'])) {
				echo getPanel("danger","Missing Field","Email is empty.");
			}
		}
		elseif(checkEmail($_POST['fusername'],$_POST['email'])==false){
				echo getPanel("danger","Error","Username and Email didnt match.");			
		}
		else {
			$username=$_POST['fusername'];
			$email=$_POST['email'];
			$code = random_str(8);
            require_once $mailerpath;
            $mail = new PHPMailer(true);
            $mail->isSMTP();
            $mail->Host       = 'mail.smtp2go.com';
            $mail->Port       = 587;
            $mail->SMTPSecure = 'tls';
            $mail->SMTPAuth   = true;
            $mail->Username   = 'noreply@catseyexi.com';
            $mail->Password   = '072TDs8S73a6';
            $mail->SetFrom('noreply@catseyexi.com', 'CatsEyeXI Support');
            $mail->addAddress($email, $username);
            $mail->IsHTML(true);
$mail->Subject = 'Password Reset';
$mail->Body    = '
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Welcome to CatsEyeXI!</title>
  <style>
    body {
      font-family: "Arial", sans-serif;
      background-color: #f4f4f4;
      color: #333;
      margin: 0;
      padding: 0;
    }

    .container {
      max-width: 600px;
      margin: 20px auto;
      padding: 20px;
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }

    h1 {
      color: #333;
    }

    p {
      line-height: 1.6;
    }

    .btn-container {
      margin-top: 20px;
    }

    .btn {
      display: inline-block;
      padding: 10px 20px;
      font-size: 16px;
      text-align: center;
      text-decoration: none;
      background-color: #3498db;
      color: #fff;
      border-radius: 5px;
      transition: background-color 0.3s ease;
    }

    .btn:hover {
      background-color: #2980b9;
    }
  </style>
</head>
<body>
  <div class="container">
    <center><img src="https://www.catseyexi.com/images/NewLogo.png" width="250"</img></center>
    <h1>CatsEyeXI - Forgot Password</h1>
    <p>Dear Adventurer,</p>
    <p>We received a request to reset your password. Please use the link below to login and update your password:</p>

    <p>Your new password is: <strong>'.$code.'</strong></p>
    
    <div class="btn-container">
        <a href="http://www.catseyexi.com/login.php?p=account" class="btn">Login</a>
    </div>

    <p>Remember to update your password after logging in for security purposes.</p>

    <p>If you did not request this password reset, please disregard this e-mail.</p>

    <p>Best Regards,<br>Your CatsEyeXI Team</p>
  </div>
</body>
</html>';
$mail->AltBody = 'Please Login at http://www.catseyexi.com/login.php?p=account Your New Password is:'. $code.' update your password after logging in';

if(!$mail->send()) {
			echo getPanel("danger","Forgot Password Incomplere","There was an issue with mail process preventing forgot password");
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
			echo getPanel("success","Password Sent","Please check your email for your new password.");
			updatePassword($code,$username);
}

		}
	}
	echo '
	<div class="col-4 col-sm-4"><center>
	</div>
	<div class="w-100"></div>
	<div class="row">
	<div class="col-1 col-sm-1">
	</div>
	<div class="col-5 col-sm-5"><center>
	<img src="images/NewArtLogo.png"  width=80%><br>
	</div>
	<div class="col-4 col-sm-4"><center>
	</center><div class="row p-3 border border-success rounded-4" id="login-form"><h1><center>Forgot Password</center></h1>
	<form method="POST" action="login.php?p=forgot">
	<div class="mb-3 mt-3">
	Username
	<input type="text" class="form-control" id="fusername" placeholder="Username" name="fusername"">
	Email
	<input type="text" class="form-control" id="email" placeholder="Email" name="email">


	<button type="submit" class="btn btn-outline-info">Forgot Password</button>
	</form>
	</div></div></div></div>
	';
	
	?>
