<?php
session_start();
include '../config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $conn->real_escape_string($_POST['email']);

    // Cek apakah email ada di database
    $query = "SELECT * FROM users WHERE email = '$email'";
    $result = $conn->query($query);

    if ($result->num_rows == 1) {
        $token = bin2hex(random_bytes(32)); // Generate reset token
        $updateQuery = "UPDATE users SET reset_token = '$token' WHERE email = '$email'";
        $conn->query($updateQuery);

        // Kirim email ke pengguna (Sederhana, bisa diimprove dengan library mailer)
        $resetLink = "https://spmi.itb-ru.ac.id/reset-password.php?token=$token";
        $subject = "Reset Password Anda";
        $message = "Klik link berikut untuk mereset password Anda: $resetLink";
        $headers = "From: sarji@itb-ru.ac.id";

        if (mail($email, $subject, $message, $headers)) {
            $_SESSION['success'] = "Email reset password telah dikirim.";
        } else {
            $_SESSION['error'] = "Gagal mengirim email.";
        }
    } else {
        $_SESSION['error'] = "Email tidak ditemukan.";
    }

    header("Location: forgot-password.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>SPMI ITB Riyadlul Ulum - Lupa Password</title>

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
        href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
        rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">

</head>

<body class="bg-gradient-primary">

    <div class="container">

        <!-- Outer Row -->
        <div class="row justify-content-center">

            <div class="col-xl-10 col-lg-12 col-md-9">

                <div class="card o-hidden border-0 shadow-lg my-5">
                    <div class="card-body p-0">
                        <!-- Nested Row within Card Body -->
                        <div class="row">
                            <div class="col-lg-6">
                                <div class="p-5">
                                    <img src="img/lupa password.webp" alt="Lupa Password" class="img-fluid">
                                </div>
                            </div>
                            <div class="col-lg-6">
                                <div class="p-5">
                                    <div class="text-center">
                                        <h1 class="h4 text-gray-900 mb-2">Lupa Password?</h1>
                                        <p class="mb-4">Masukan email Anda di bawah, kami akan mengirimkan link ke email Anda!</p>
                                    </div>
                                    <?php if (isset($_SESSION['error'])): ?>
                                        <p style="color: red;"><?php echo $_SESSION['error'];
                                                                unset($_SESSION['error']); ?></p>
                                    <?php endif; ?>

                                    <?php if (isset($_SESSION['success'])): ?>
                                        <p style="color: green;"><?php echo $_SESSION['success'];
                                                                    unset($_SESSION['success']); ?></p>
                                    <?php endif; ?>
                                    <form class="user" action="forgot-password.php" method="POST">
                                        <div class="form-group">
                                            <input type="email" name="email" class="form-control form-control-user"
                                                id="exampleInputEmail" aria-describedby="emailHelp"
                                                placeholder="Enter Email Address...">
                                        </div>
                                        <button type="submit" class="btn btn-primary btn-user btn-block">
                                            Reset Password
                                        </button>
                                    </form>
                                    <hr>
                                    <div class="text-center">
                                        <a class="small" href="login.php">Sudah punya akun? Login!</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

        </div>

    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin-2.min.js"></script>

</body>

</html>