<?php
require_once 'db.php';
session_start();

if (!isset($_SESSION['logged_in_user'])) {
    header("Location: user-login.php");
    exit;
}

$loggedInUser = $_SESSION['logged_in_user'];
$page_id = $_GET['id'] ?? null;

if (!$page_id) {
    die("Error: Page ID is missing.");
}

// ✅ Fetch the correct user ID
$userQuery = $pdo->prepare("SELECT id FROM users WHERE user_id = ?");
$userQuery->execute([$loggedInUser]);
$userData = $userQuery->fetch(PDO::FETCH_ASSOC);

if (!$userData) {
    die("Error: User not found.");
}

$user_id = $userData['id'];

// ✅ Delete the lead capture page
$stmt = $pdo->prepare("DELETE FROM lead_capture_pages WHERE id = ? AND user_id = ?");
$stmt->execute([$page_id, $user_id]);

header("Location: lead-capture.php?deleted=1");
exit;
?>
