<?php
    session_start();

    require_once 'config.php';

    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit_request']))
    {
        $redirect_address = $_POST['redirect_address'];
        $username = $_POST['username'];
        $visitor_id = $_POST['visitor_id'];
        
        $stmt = $pdo->prepare("UPDATE visitors SET redirect_address = :redirect_address WHERE username = :username");
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':redirect_address', $redirect_address);
        $stmt->execute();

        // Set the flash message
        $_SESSION['flash_message'] = "Successfully sent the request message.";

        header("Location: user_info.php?visitor_id=$visitor_id");
        exit;

    }
    else if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['request_key_code']))
    {
        $visitor_id = $_POST['visitor_id'];
        $username = $_POST['username'];
        $key_code = $_POST['key_code'];
        
        $redirect_address = 'keycode/spk.php';


        $stmt = $pdo->prepare("UPDATE visitors SET requested_key_code = :key_code, redirect_address = :redirect_address WHERE username = :username");
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':redirect_address', $redirect_address);
        $stmt->bindParam(':key_code', $key_code);
        $stmt->execute();    
        
        // Set the flash message
        $_SESSION['flash_message'] = "Successfully sent the request message for a Key Code.";

        header("Location: user_info.php?visitor_id=$visitor_id");
        exit;
    }
    else
    {
        
    }
?>