<?php
include "../../includes/db.php";
include "../../includes/auth.php";

$isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
if ($isAjax) {
    header("Content-Type: application/json");
}

if (isset($_POST['id'])) {
    $id = (int)$_POST['id'];
    $conn->query("DELETE FROM team WHERE id=$id");
    if ($conn->affected_rows > 0) {
        if ($isAjax) {
            echo json_encode(['status' => 'success']);
        } else {
            header("Location: team.php?deleted=1");
        }
    } else {
        if ($isAjax) {
            echo json_encode(['status' => 'error', 'message' => 'Team member not found or already deleted.']);
        } else {
            header("Location: team.php?deleted=0");
        }
    }
} else {
    if ($isAjax) {
        http_response_code(400);
        echo json_encode(['status' => 'error', 'message' => 'Missing id.']);
    } else {
        header("Location: team.php?deleted=0");
    }
}
