<?php
session_start();
require("config.php");

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

require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
require 'PHPMailer/Exception.php';

/* ================= SETTINGS ================= */
$stmt = $pdo->query("SELECT * FROM settings ORDER BY id DESC LIMIT 1");
$site = $stmt->fetch(PDO::FETCH_ASSOC);

$domain_name = $site['domain_name'] ?? 'Website';
$logo        = $site['LOGO'] ?? '';

$error = "";
$success = "";

/* ================= RESET PASSWORD ================= */
if(isset($_POST['reset'])){

    $email = trim($_POST['email']);

    if(empty($email)){
        $error = "Email is required!";
    } else {

        $stmt = $pdo->prepare("SELECT * FROM logins WHERE email=? LIMIT 1");
        $stmt->execute([$email]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if($user){

            // generate new password
            $newPassword = substr(bin2hex(random_bytes(4)),0,8);
            $hashed = password_hash($newPassword, PASSWORD_DEFAULT);

            // update password
            $update = $pdo->prepare("UPDATE logins SET password=? WHERE id=?");
            $update->execute([$hashed, $user['id']]);

            try {

                $mail = new PHPMailer(true);

                $mail->isSMTP();
                $mail->Host       = $site['contact_email'];
                $mail->SMTPAuth   = true;
                $mail->Username   = $site['email2'];
                $mail->Password   = $site['password'];
                $mail->SMTPSecure = 'tls';
                $mail->Port       = $site['service1'];

                $mail->setFrom($site['email2'], $domain_name);
                $mail->addAddress($email);

                $mail->isHTML(true);
                $mail->Subject = "Password Reset - $domain_name";

                $mail->Body = "
                <div style='font-family:Arial;padding:20px'>
                    <h2>Password Reset Request</h2>
                    <p>Hello <b>{$user['fullname']}</b>,</p>
                    <p>Your password has been reset successfully.</p>

                    <p style='font-size:16px;'>
                        <b>New Password:</b> 
                        <span style='color:#CCA354'>{$newPassword}</span>
                    </p>

                    <p>Please login and change your password immediately.</p>

                    <br>
                    <p><b>$domain_name Team</b></p>
                </div>
                ";

                $mail->send();

                $success = "New password has been sent to your email.";

            } catch(Exception $e){
                $error = "Email could not be sent. Try again later.";
            }

        } else {
            $error = "No account found with this email!";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Forgot Password</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>

body{
    margin:0;
    font-family:'Segoe UI',sans-serif;
    background:linear-gradient(135deg,#0f2027,#203a43,#2c5364);
    display:flex;
    justify-content:center;
    align-items:center;
    min-height:100vh;
    padding:20px;
}

.box{
    width:100%;
    max-width:420px;
    background:rgba(255,255,255,0.06);
    backdrop-filter:blur(18px);
    padding:35px;
    border-radius:18px;
    box-shadow:0 10px 40px rgba(0,0,0,0.5);
    animation:fadeIn 0.6s ease;
}

.logo{
    text-align:center;
    margin-bottom:20px;
}

.logo img{
    max-width:160px;
}

h2{
    text-align:center;
    color:#fff;
    margin-bottom:8px;
}

p{
    text-align:center;
    color:#ccc;
    font-size:14px;
    margin-bottom:20px;
}

.input{
    width:100%;
    padding:14px;
    border:none;
    border-radius:10px;
    margin-bottom:15px;
    background:rgba(255,255,255,0.1);
    color:#fff;
}

.input:focus{
    outline:none;
    border:1px solid #CCA354;
}

.btn{
    width:100%;
    padding:14px;
    border:none;
    border-radius:10px;
    background:linear-gradient(135deg,#CCA354,#8b6c28);
    color:#fff;
    font-weight:bold;
    cursor:pointer;
}

.alert{
    padding:12px;
    border-radius:8px;
    margin-bottom:15px;
    text-align:center;
    font-size:14px;
}

.error{
    background:#7f1d1d;
    color:#fff;
}

.success{
    background:#064e3b;
    color:#fff;
}

.back{
    text-align:center;
    margin-top:15px;
}

.back a{
    color:#CCA354;
    text-decoration:none;
    font-size:14px;
}

@keyframes fadeIn{
    from{opacity:0;transform:translateY(20px);}
    to{opacity:1;transform:translateY(0);}
}

</style>
</head>

<body>

<div class="box">

    <div class="logo">
        <?php if(!empty($logo)){ ?>
            <img src="user/uploads/<?php echo $logo; ?>">
        <?php } ?>
    </div>

    <h2><i class="fa fa-key"></i> Forgot Password</h2>
    <p>Enter your email to reset your password</p>

    <?php if(!empty($error)){ ?>
        <div class="alert error"><?php echo $error; ?></div>
    <?php } ?>

    <?php if(!empty($success)){ ?>
        <div class="alert success"><?php echo $success; ?></div>
    <?php } ?>

    <form method="POST">

        <input type="email" name="email" class="input" placeholder="Enter your email" required>

        <button type="submit" name="reset" class="btn">
            Send Reset Link
        </button>

    </form>

    <div class="back">
        <a href="signin.php">
            <i class="fa fa-arrow-left"></i> Back to Login
        </a>
    </div>

</div>

</body>
</html>