<?php
session_start();
include 'includes/auth.php';
include 'includes/db.php';

// Validate ID
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    $_SESSION['message'] = "Invalid member ID.";
    header("Location: members_all.php");
    exit;
}

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

// Optional: Fetch member name for logging (optional)
$member = mysqli_fetch_assoc(mysqli_query($conn, "SELECT CONCAT(first_name, ' ', surname) AS name FROM members WHERE id = $id"));

// Attempt delete
if (mysqli_query($conn, "DELETE FROM members WHERE id = $id")) {
    $_SESSION['message'] = "Member " . ($member['name'] ?? '') . " deleted successfully.";
} else {
    $_SESSION['message'] = "Error deleting member: " . mysqli_error($conn);
}

header("Location: members_all.php");
exit;
