<?php
require 'config.php';
if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit;
}

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    echo "<div class='alert alert-danger'>Invalid broker ID.</div>";
    include('footer.php');
    exit;
}

$id = (int)$_GET['id'];
$id = $_GET['id'];

// Optional: Confirm before deleting
if (isset($_GET['confirm']) && $_GET['confirm'] == 1) {
    $stmt = $dbh->prepare("DELETE FROM surveys WHERE id = ?");
    $stmt->execute([$id]);
    header("Location: index.php");
    exit;
}

// Fetch survey for confirmation display
$stmt = $dbh->prepare("SELECT broker_name FROM surveys WHERE id = ?");
$stmt->execute([$id]);
$survey = $stmt->fetch();

if (!$survey) {
    die("Survey not found.");
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Delete Survey</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <h3 class="text-danger">Confirm Deletion</h3>
    <p>Are you sure you want to delete the survey: <strong><?= htmlspecialchars($survey['broker_name']) ?></strong>?</p>
    <a href="delete.php?id=<?= $id ?>&confirm=1" class="btn btn-danger">Yes, Delete</a>
    <a href="index.php" class="btn btn-secondary">Cancel</a>
</div>
</body>
</html>
