<?php
session_start(); // Start session

// Check if OTP session data exists
if(isset($_SESSION['otp'])) {
    $otp = $_SESSION['otp'];
} else {
    // Redirect back to the forgot password page if OTP session data is missing
    header("Location: recover-password.php");
    exit();
}

// Check if form is submitted for OTP verification
if(isset($_POST['otp']) && isset($_POST['password']) && isset($_POST['confirm_password'])) {
    $entered_otp = $_POST['otp'];
    $new_password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    // Verify OTP
    if($entered_otp == $otp) {
        // OTP is correct

        // Check if new password and confirm password match
        if($new_password === $confirm_password) {
            // Update password in database (Replace this with your database update code)

            // Redirect user to login page or a success page
            header("Location: login.php");
            exit();
        } else {
            // Passwords do not match
            $error_message = "Passwords do not match.";
        }
    } else {
        // Incorrect OTP
        $error_message = "Invalid OTP.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OTP Verification and Password Reset</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 300px;
            max-width: 90%;
            text-align: center;
        }

        h2 {
            margin-bottom: 20px;
            color: #333333;
        }

        label {
            text-align: left;
            display: block;
            margin-bottom: 5px;
            font-weight: bold; /* Make label text slightly bolder */
        }

        input[type="text"],
        input[type="password"] {
            width: calc(100% - 20px);
            padding: 10px;
            margin: 5px 0 15px;
            border: 1px solid #cccccc;
            border-radius: 5px;
        }

        button[type="submit"] {
            width: calc(100% - 20px);
            padding: 10px;
            margin: 20px 0;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: #ffffff;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button[type="submit"]:hover {
            background-color: #0056b3;
        }

        .error-message {
            color: red;
        }
        body {
            background-image: url('https://source.unsplash.com/random'); /* Random background image from Unsplash */
            background-size: cover;
            background-position: center;
        }
    </style>
            <script>
    document.addEventListener('DOMContentLoaded', function() {
        const body = document.querySelector('body');
        
        // Function to generate a random color
        function getRandomColor() {
            const letters = '0123456789ABCDEF';
            let color = '#';
            for (let i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
        }

        // Change background color when the page loads
        body.style.background = `linear-gradient(to right, ${getRandomColor()}, ${getRandomColor()})`;
    });
</script>
</head>
<body>
    <div class="container">
        <h2>OTP Verification and Password Reset</h2>
        <?php
        if(isset($error_message)) {
            echo '<p class="error-message">' . $error_message . '</p>';
        }
        ?>
        <form action="" method="POST">
            <label for="otp">Enter OTP:</label>
            <input type="text" id="otp" name="otp" placeholder="Enter OTP" required><br>
            <label for="password">Enter New Password:</label>
            <input type="password" id="password" name="password" placeholder="Enter New Password" required><br>
            <label for="confirm_password">Confirm New Password:</label>
            <input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm New Password" required><br>
            <button type="submit">Reset Password</button>
        </form>
    </div>
</body>
</html>
