<?php
session_start();

if (!isset($_SESSION['otp']) || !isset($_SESSION['email'])) {
    header("Location: login.php");
    exit;
}

// Check if OTP expired
if (time() > $_SESSION['otp_expiry']) {
    echo "<script>alert('OTP has expired. Please request a new one.'); window.location.href='login.php';</script>";
    session_destroy(); // Clear session
    exit;
}

// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $entered_otp = trim($_POST['otp']);

    if ($entered_otp == $_SESSION['otp']) {
        // ✅ OTP matched — user is now authenticated
        $_SESSION['user'] = $_SESSION['email']; // set user session
       
       
       
       
        unset($_SESSION['otp']);
        unset($_SESSION['otp_expiry']);

        echo "<script>
                alert('OTP Verified Successfully!');
                setTimeout(function(){
                    window.location.href = '../accounts'; // Redirect to accounts page
                }, 2000); // Wait for 2 seconds before redirecting
              </script>";
        exit;
    } else {
        echo "<script>alert('Invalid OTP. Please try again.'); window.history.back();</script>";
        exit;
    }
}

require("header.php");
?>






 <!-- Account Section -->
    <section class="account-section position-relative">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-xl-6 col-lg-7 col-md-8">
                    <a href="../index.php" class="text-center d-block mb-3 mb-sm-4 auth-page-logo">
                        <img src="../img/LOGO.png" alt="logo">
                     <style>
                        #yt-widget .yt-listbox__col {
                            display: block !important; /* display: inline-block; */
                            list-style: none;
                            vertical-align: top;
                            /**/
                            max-width: 90% !important;
                        }
                        .yt-listbox {
                            min-width: 200px !important;
                            max-width: 500px !important;
                            max-height: 500px !important;
                            padding: 10px 10px !important;
                            overflow: auto !important;
                        }
                        #yt-widget.yt-state_bottom .yt-listbox {
                            bottom: unset !important;
                        }
                        .yt-listbox li {
                            display: block !important; 
                        }
                        .yt-servicelink {
                            display: none !important;
                        }
                    </style>
                    
                    
<div id="ytWidget"></div>

<script src="https://translate.yandex.net/website-widget/v1/widget.js?widgetId=ytWidget&pageLang=en&widgetTheme=light&autoMode=true" type="text/javascript"></script> </a>
                   
                   
                   
    <h2>Enter OTP</h2>
    <p>An OTP has been sent to your email: <strong><?php echo $_SESSION['email']; ?></strong></p>
    
    <form method="post"  class="mt-4">
        
         <div class="row">
             
             
         <div class="col-12">
                                <p style="color:sandybrown"></p>
                                <div class="form-group">
                            
        <label for="otp">Enter OTP:</label>
        <input type="text" name="otp" class="form-control form--control h-45" required>
         </div>
                            </div>
        
        
        
        
        <div class="col-12">
                            </div>
                            
                            
                            
                            <div class="col-12">
                               
                                
                                <button class="btn btn--base w-100" type="submit">Verify OTP</button>
                                
                            </div>
</div>
        
      
      
      
    </form>
      </div>
            </div>
        </div>
    </section>
    <?php require ("footer.php")?>
