<?php
include("../includes/db_connection.php");

$email = mysqli_real_escape_string($conn,$_GET['email']);

if(isset($_POST['verify'])){

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

    $query = mysqli_query($conn,"
        SELECT * FROM associates 
        WHERE email='$email' 
        AND otp_code='$otp'
        AND email_verified = 0
    ");

    if(mysqli_num_rows($query) > 0){

        $data = mysqli_fetch_assoc($query);

        if(strtotime($data['otp_expiry']) >= time()){

            mysqli_query($conn,"
                UPDATE associates 
                SET email_verified=1,
                    otp_code=NULL,
                    otp_expiry=NULL
                WHERE email='$email'
            ");

            echo "<div class='alert alert-success'>OTP Verified Successfully</div>";

        } else {
            echo "<div class='alert alert-danger'>OTP Expired</div>";
        }

    } else {
        echo "<div class='alert alert-danger'>Invalid OTP</div>";
    }

}
?>
<!DOCTYPE html>
<html>
<head>
<title>Verify OTP</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" 
rel="stylesheet">
</head>

<body style="background:#f4f6f9;">

<div class="container d-flex justify-content-center align-items-center" 
style="min-height:100vh;">
<div class="card shadow-lg p-5" style="max-width:450px; width:100%; border-radius:15px;">

<h4 class="text-center mb-3">OTP Verification</h4>
<p class="text-center text-muted">Enter the 6-digit OTP sent to your email</p>

<?php if(!empty($message)) echo $message; ?>

<form method="POST">

<div class="mb-3">
<input type="text" name="otp" class="form-control text-center"
placeholder="Enter OTP"
maxlength="6"
required
style="font-size:20px; letter-spacing:5px;">
</div>

<button type="submit" name="verify" class="btn btn-primary w-100">
Verify OTP
</button>

</form>

<div class="text-center mt-3">
<a href="register.php" class="text-decoration-none">Back to Registration</a>
</div>

</div>
</div>

</body>
</html>