/home/awneajlw/www/codestechvista.com/verify-otp.php
<?php
session_start();

// Include required files
require_once 'includes/email.php';
require_once 'includes/auth.php';
require_once 'config/database.php';

$error = '';
$success = '';
$email = $_SESSION['registration_email'] ?? '';

// Handle OTP verification
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['otp'])) {
        $enteredOTP = sanitizeInput($_POST['otp']);
        
        if (empty($enteredOTP)) {
            $error = 'Please enter the verification code.';
        } elseif (strlen($enteredOTP) !== 6) {
            $error = 'Verification code must be 6 digits.';
        } else {
            // Dual verification system: Database + Session fallback
            error_log("Verifying OTP: Email=$email, OTP=$enteredOTP");
            
            $verified = false;
            
            // Try database verification first
            if (verifyOTP($email, $enteredOTP)) {
                $verified = true;
                error_log("OTP verification successful via database for email: $email");
            } 
            // Fallback to session verification if database fails
            elseif (isset($_SESSION['registration_otp']) && 
                    isset($_SESSION['otp_generated_time']) && 
                    $_SESSION['registration_otp'] === $enteredOTP) {
                
                // Check if OTP is not expired (10 minutes)
                $otpAge = time() - $_SESSION['otp_generated_time'];
                if ($otpAge <= 600) { // 10 minutes = 600 seconds
                    $verified = true;
                    error_log("OTP verification successful via session fallback for email: $email");
                    // Clear used OTP from session
                    unset($_SESSION['registration_otp']);
                    unset($_SESSION['otp_generated_time']);
                } else {
                    error_log("Session OTP expired for email: $email, age: $otpAge seconds");
                }
            }
            
            if ($verified) {
                $_SESSION['email_verified'] = true;
                header('Location: shop-registration-form.php');
                exit();
            } else {
                error_log("OTP verification failed for email: $email, OTP: $enteredOTP");
                $error = 'Invalid or expired verification code. Please try again.';
            }
        }
    } elseif (isset($_POST['resend_otp'])) {
        // Handle resend OTP request
        if (!empty($email)) {
            $newOTP = generateOTP();
            $_SESSION['registration_otp'] = $newOTP;
            $_SESSION['otp_generated_time'] = time();
            
            // Save new OTP to database
            $otpSaved = saveOTPToDatabase($email, $newOTP);
            
            if ($otpSaved) {
                // Send new OTP email
                $emailSent = sendOTPEmail($email, $newOTP);
                
                if ($emailSent) {
                    $success = 'New verification code sent to your email.';
                } else {
                    $success = "New verification code has been generated. Please check your email.";
                }
            } else {
                $error = 'Failed to generate new verification code. Please try again.';
            }
        } else {
            $error = 'Session expired. Please start registration again.';
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>VERIFY OTP - OPTI SLIP</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #169D53;
            min-height: 100vh;
            margin: 0;
            padding: 0;
            position: relative;
            overflow-x: hidden;
        }
        
        /* Back Button */
        .back-btn {
            position: absolute;
            top: 30px;
            left: 30px;
            color: white;
            font-size: 24px;
            text-decoration: none;
            z-index: 10;
        }
        
        .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            padding: 40px 20px;
            position: relative;
        }
        
        /* Logo Section */
        .logo-section {
            text-align: center;
            /*margin-bottom: 40px;*/
        }
        
        .logo-container {
            /*margin-bottom: 20px;*/
        }
        
        .logo-image {
            width: 100%;
            height: 200px;
            object-fit: contain;
        }
        
        /* .logo-text {
            color: white;
            font-size: 28px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 3px;
            margin-bottom: 30px;
        } */
        
        /* Main Content */
        .main-content {
            background: #169D53;
            color: white;
            text-align: center;
            max-width: 500px;
            width: 100%;
        }
        
        .description-text {
            color: white;
            font-size: 14px;
            line-height: 1.6;
            margin-bottom: 40px;
            opacity: 0.9;
        }
        
        /* OTP Input */
        .otp-input {
            width: 100%;
            padding: 15px 20px;
            border: 2px solid rgba(255,255,255,0.3);
            border-radius: 12px;
            font-size: 24px;
            text-align: center;
            letter-spacing: 8px;
            font-weight: bold;
            background: rgba(255,255,255,0.1);
            color: white;
            margin-bottom: 30px;
        }
        
        .otp-input::placeholder {
            color: rgba(255,255,255,0.9) !important;
            letter-spacing: 6px !important;
            font-weight: bold !important;
            font-size: 24px !important;
        }
        
        .otp-input::-webkit-input-placeholder {
            color: rgba(255,255,255,0.9) !important;
            letter-spacing: 6px !important;
        }
        
        .otp-input::-moz-placeholder {
            color: rgba(255,255,255,0.9) !important;
            letter-spacing: 6px !important;
        }
        
        .otp-input:focus {
            outline: none;
            border-color: rgba(255,255,255,0.5);
            background: rgba(255,255,255,0.15);
        }
        
        /* Proceed Button */
        .proceed-btn {
            width: 100%;
            padding: 18px 20px;
            background: white;
            color: #169D53;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 20px;
        }
        
        .proceed-btn:hover {
            background: rgba(255,255,255,0.9);
            transform: translateY(-2px);
        }
        
        /* Resend Link */
        .resend-link {
            color: rgba(255,255,255,0.8) !important;
            text-decoration: underline !important;
            font-size: 14px !important;
            cursor: pointer !important;
            display: inline-block !important;
            margin-top: 15px !important;
            visibility: visible !important;
            opacity: 1 !important;
        }
        
        .resend-link:hover {
            color: white !important;
            text-decoration: underline !important;
        }
        
        .text-center {
            text-align: center !important;
            margin-top: 15px !important;
        }
        
        /* Responsive */
        @media (max-width: 768px) {
            .container {
                padding: 20px 15px;
            }
            
            .logo-image {
                width: 100%;
                height: 200px;
            }
            
            .back-btn {
                top: 20px;
                left: 20px;
                font-size: 20px;
            }
            
            .description-text {
                font-size: 13px;
                margin-bottom: 30px;
                padding: 0 10px;
            }
            
            .otp-input {
                font-size: 20px;
                letter-spacing: 6px;
                padding: 12px 15px;
            }
            
            .proceed-btn {
                padding: 15px 20px;
                font-size: 15px;
            }
            
            .main-content {
                padding: 0 10px;
            }
        }
        
        @media (max-width: 480px) {
            .container {
                padding: 15px 10px;
            }
            
            .logo-image {
                width: 70px;
                height: 100px;
            }
            
            .description-text {
                font-size: 12px;
                line-height: 1.5;
                margin-bottom: 25px;
            }
            
            .otp-input {
                font-size: 18px;
                letter-spacing: 4px;
                padding: 10px 15px;
            }
            
            .proceed-btn {
                padding: 12px 15px;
                font-size: 14px;
            }
            
            .back-btn {
                top: 15px;
                left: 15px;
                font-size: 18px;
            }
        }
        
        .main-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #10b981, #059669, #10b981);
            border-radius: 24px 24px 0 0;
        }
        
        .logo-container {
            margin-bottom: 50px;
            position: relative;
        }
        
        .logo {
            width: 100px;
            height: 100px;
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 25px;
            box-shadow: 0 15px 35px rgba(16, 185, 129, 0.4), 0 0 0 4px rgba(255, 255, 255, 0.1);
            position: relative;
            animation: float 3s ease-in-out infinite;
        }
        
        .logo::before {
            content: '';
            position: absolute;
            top: -2px;
            left: -2px;
            right: -2px;
            bottom: -2px;
            background: linear-gradient(135deg, #10b981, #059669, #10b981);
            border-radius: 50%;
            z-index: -1;
            opacity: 0.3;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .logo-icon {
            font-size: 40px;
            color: white;
            filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
        }
        
        .logo-text {
            color: #1f2937;
            font-size: 32px;
            font-weight: 700;
            letter-spacing: 3px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 10px;
        }
        
        .description {
            color: #6b7280;
            font-size: 18px;
            line-height: 1.7;
            margin-bottom: 50px;
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
        }
        
        .otp-container {
            margin-bottom: 40px;
            position: relative;
        }
        
        .otp-input {
            width: 100%;
            padding: 20px 25px;
            border: 2px solid #e5e7eb;
            border-radius: 16px;
            font-size: 24px;
            text-align: center;
            letter-spacing: 12px;
            font-weight: 700;
            color: #1f2937;
            background: linear-gradient(135deg, #f9fafb 0%, #ffffff 100%);
            transition: all 0.4s ease;
            box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
            font-family: 'Inter', monospace;
        }
        
        .otp-input:focus {
            outline: none;
            border-color: #10b981;
            background: white;
            box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.15), inset 0 2px 4px rgba(0, 0, 0, 0.05);
            transform: translateY(-2px);
        }
        
        .btn-proceed {
            width: 100%;
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: white;
            border: none;
            padding: 20px;
            border-radius: 16px;
            font-size: 20px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.4s ease;
            margin-bottom: 25px;
            position: relative;
            overflow: hidden;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .btn-proceed::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: left 0.5s;
        }
        
        .btn-proceed:hover::before {
            left: 100%;
        }
        
        .btn-proceed:hover {
            background: linear-gradient(135deg, #059669 0%, #047857 100%);
            transform: translateY(-3px);
            box-shadow: 0 15px 35px rgba(16, 185, 129, 0.4);
        }
        
        .btn-proceed:active {
            transform: translateY(-1px);
        }
        
        .resend-link {
            color: #6b7280;
            text-decoration: none;
            font-size: 14px;
            display: inline-block;
        }
        
        .resend-link:hover {
            color: #10b981;
            text-decoration: underline;
        }
        
        .otp-display {
            background: #f3f4f6;
            border: 2px solid #e5e7eb;
            border-radius: 12px;
            padding: 15px 20px;
            margin-bottom: 20px;
            font-family: monospace;
            font-size: 16px;
            letter-spacing: 3px;
            color: #374151;
        }
        
        .demo-notice {
            background: #dbeafe;
            border: 1px solid #93c5fd;
            border-radius: 8px;
            padding: 10px;
            margin-bottom: 20px;
            font-size: 12px;
            color: #1e40af;
        }
        
        /* Responsive Design */
        @media (max-width: 768px) {
            .main-card {
                padding: 30px 20px;
                margin: 20px;
            }
            
            .logo {
                width: 60px;
                height: 60px;
            }
            
            .logo-icon {
                font-size: 24px;
            }
            
            .logo-text {
                font-size: 20px;
            }
            
            .description {
                font-size: 14px;
            }
        }
        
        @media (max-width: 480px) {
            .main-card {
                padding: 20px 15px;
                margin: 10px;
            }
            
            .logo {
                width: 50px;
                height: 50px;
            }
            
            .logo-icon {
                font-size: 20px;
            }
            
            .logo-text {
                font-size: 18px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Back Button -->
        <a href="register.php" class="back-btn">
                <i class="fas fa-arrow-left"></i>
            </a>
        
        <!-- Logo Section -->
        <div class="logo-section">
            <div class="logo-container">
                <img src="assets/images/OptiSlip.png" alt="Opti Slip Logo" class="logo-image">
                </div>
                <!-- <div class="logo-text">OPTI SLIP</div> -->
            </div>
            
        <!-- Main Content -->
        <div class="main-content">
            
            <div class="description-text">
                We've sent a 6-digit verification code to your email address. Please check your inbox and enter the code below to verify your account.
            </div>
            
            <?php if (!empty($error)): ?>
                <div class="alert alert-danger" style="background: #fee2e2; color: #dc2626; padding: 12px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #fecaca;">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>
            
            <?php if (!empty($success)): ?>
                <div class="alert alert-success" style="background: #d1fae5; color: #065f46; padding: 12px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #a7f3d0;">
                    <i class="fas fa-check-circle me-2"></i>
                    <?php echo htmlspecialchars($success); ?>
                </div>
            <?php endif; ?>
            
            <?php if (isset($_SESSION['otp_fallback_message'])): ?>
                <div class="alert alert-warning" style="background: #fef3c7; color: #92400e; padding: 12px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #fde68a;">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    <?php echo htmlspecialchars($_SESSION['otp_fallback_message']); ?>
                </div>
                <?php unset($_SESSION['otp_fallback_message']); ?>
            <?php endif; ?>
            
            <!-- Email Info -->
            <?php if (!empty($email)): ?>
                <div class="email-info" style="background: rgba(255,255,255,0.1); padding: 15px; border-radius: 8px; margin-bottom: 20px; text-align: center;">
                    <div style="color: rgba(255,255,255,0.8); font-size: 14px; margin-bottom: 5px;">Verification code sent to:</div>
                    <div style="color: white; font-weight: 600; font-size: 16px;"><?php echo htmlspecialchars($email); ?></div>
                </div>
            <?php endif; ?>
            
            <!-- OTP Form -->
            <form method="POST" action="">
                <input type="text" name="otp" class="otp-input" placeholder="●●●●●●" maxlength="6" required>
                <button type="submit" class="proceed-btn">PROCEED</button>
            </form>
            
            <!-- Resend Section -->
            <div class="text-center" style="margin-top: 20px; padding: 10px;">
                <div style="color: rgba(255,255,255,0.7); font-size: 13px; margin-bottom: 10px;">
                    Didn't receive the code?
                </div>
                <form method="POST" action="" style="display: inline;">
                    <button type="submit" name="resend_otp" class="resend-btn" style="background: none; border: none; color: rgba(255,255,255,0.9); text-decoration: underline; font-size: 14px; cursor: pointer;">
                        Resend Code
                    </button>
                </form>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // OTP Input formatting
        document.querySelector('.otp-input').addEventListener('input', function(e) {
            // Remove any non-numeric characters
            this.value = this.value.replace(/[^0-9]/g, '');
            
            // Limit to 6 digits
            if (this.value.length > 6) {
                this.value = this.value.slice(0, 6);
            }
        });
        
        // Auto-focus on page load
        window.addEventListener('load', function() {
            document.querySelector('.otp-input').focus();
        });
        
        // Auto-focus on OTP input
        document.addEventListener('DOMContentLoaded', function() {
            const otpInput = document.querySelector('.otp-input');
            if (otpInput) {
                otpInput.focus();
            }
        });
        
        // Simple form validation
        document.querySelector('form').addEventListener('submit', function(e) {
            const otp = document.querySelector('.otp-input').value;
            
            if (otp.length !== 6) {
                e.preventDefault();
                alert('Please enter a 6-digit OTP');
                return;
            }
            
            // OTP validation is now handled by server-side PHP code
            // No client-side validation needed
        });
    </script>
</body>
</html>