<?php 
session_start();

require 'db_connection.php';

if (isset($_GET['token'])) {
    $token = $_GET['token'];

    // Verify token
    $sql = "SELECT * FROM customer WHERE verification_token = '$token' AND verified = 0";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) { 
        // Token is valid, update verified status
        $row = $result->fetch_assoc();
        $update_sql = "UPDATE customer SET verified = 1, verification_token = NULL WHERE verification_token = '$token'";
        
        if ($conn->query($update_sql) === TRUE) {
            // HTML modal and CSS for centered box
            echo '
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Registration Successful</title>
                <style>
                    /* Modal background */
                    .modal-overlay {
                        position: fixed;
                        top: 0;
                        left: 0;
                        width: 100%;
                        height: 100%;
                        background: rgba(0, 0, 0, 0.5);
                        display: flex;
                        justify-content: center;
                        align-items: center;
                    }
                    
                    /* Modal box */
                    .modal-content {
                        background: white;
                        padding: 20px;
                        border-radius: 8px;
                        width: 400px;
                        text-align: center;
                    }
                    
                    .modal-content h2 {
                        margin-bottom: 10px;
                    }
                    
                    .modal-content p {
                        margin-bottom: 20px;
                    }

                    .modal-content button {
                        padding: 10px 20px;
                        border: none;
                        background-color: #999;
                        color: white;
                        font-size: 16px;
                        border-radius: 4px;
                        cursor: pointer;
                    }

                    .modal-content button:hover {
                        background-color: #0056b3;
                    }
                </style>
            </head>
            <body>

            <!-- Modal structure -->
            <div class="modal-overlay" id="modal">
                <div class="modal-content">
                    <h2>Registration Successful!</h2>
                    <p>Kindly log in with your phone and password to complete registration.</p>
                    <button id="loginButton">OK</button>
                </div>
            </div>

            <script>
                // Redirect to login page when OK is clicked
                document.getElementById("loginButton").addEventListener("click", function() {
                    window.location.href = "login.php";
                });
            </script>

            </body>
            </html>
            ';
        } else {
            echo "Error updating record: " . $conn->error;
        }
    } else {
        echo "Invalid or expired token.";
    }
} else {
    echo "No verification token provided.";
}

$conn->close();
?>
