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

if (strlen($_SESSION['adminlogin']) == 0) {
    header('location:index.php');
    exit();
} else {
    $msg = '';

    if (isset($_POST['submit'])) {
        $email = $_GET['email'];
        $password = $_POST['password'];

        // Hash the password (preferably with password_hash() instead of md5)
        $hashed_password = md5($password);

        $sql = "UPDATE users SET password = :password WHERE email = :email";
        $query = $dbh->prepare($sql);
        $query->bindParam(':password', $hashed_password, PDO::PARAM_STR);
        $query->bindParam(':email', $email, PDO::PARAM_STR);
        $query->execute();

        $msg = "Password Reset Successfully";
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .container {
            margin-top: 50px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-6">
                <div class="card shadow-lg border-0 rounded-lg mt-5">
                    <div class="card-header">
                        <h3 class="text-center font-weight-light my-4">Reset Password</h3>
                    </div>
                    <div class="card-body">
                        <?php if (!empty($msg)) { ?>
                            <div class="alert alert-success"><?php echo htmlentities($msg); ?></div>
                        <?php } ?>
                        <form method="POST">
                            <div class="mb-3">
                                <label class="small mb-1" for="password">New Password</label>
                                <input class="form-control py-4" id="password" name="password" type="password" placeholder="Enter new password" required />
                            </div>
                            <div class="mb-3">
                                <label class="small mb-1" for="confirm_password">Confirm Password</label>
                                <input class="form-control py-4" id="confirm_password" name="confirm_password" type="password" placeholder="Confirm new password" required />
                            </div>
                            <div class="d-flex align-items-center justify-content-center mt-4 mb-0">
                                <button type="submit" name="submit" class="btn btn-primary">Reset Password</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>

<?php } ?>
