<?php
    session_start();

    // Check if admin is logged in
    if (!isset($_SESSION['admin_username'])) {
        header("Location: admin_login.php");
        exit;
    }

    // Include database connection
    include('../config.php');

    // Check if visitor ID is provided in the URL
    if (isset($_GET['visitor_id'])) {
        // Sanitize the visitor ID
        $visitor_id = filter_var($_GET['visitor_id'], FILTER_SANITIZE_NUMBER_INT);
        
        // Prepare and execute SQL query to delete the visitor based on ID
        $stmt = $pdo->prepare("DELETE FROM visitors WHERE id = ?");
        $stmt->execute([$visitor_id]);

        // Redirect back to the page where the delete button was clicked
        header("Location: {$_SERVER['HTTP_REFERER']}");
        exit;
    } else {
        // If no visitor ID is provided, redirect to an error page or homepage
        header("Location: error.php");
        exit;
    }
?>
