<?php
header('Content-Type: application/json');
require_once '../DB/db_credentials.php';
require_once '../DB/db_connection.php';
$id = $_POST['id'] ?? '';
if (!$id) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Missing ID.']);
exit;
}
// check if person is responsible for any shift
$sql = "SELECT COUNT(*) FROM citeLeParis_shift WHERE fk_responsible = ?";
$stmt = mysqli_prepare($dbc, $sql);
mysqli_stmt_bind_param($stmt, 'i', $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $shiftCount);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
if ($shiftCount > 0) {
http_response_code(403);
echo json_encode(['status' => 'error', 'message' => 'This person is responsible for one or more shifts and cannot be deleted.']);
mysqli_close($dbc);
exit;
}
// check if person is registered for anything
$sql = "SELECT COUNT(*) FROM citeLeParis_register WHERE fk_person = ?";
$stmt = mysqli_prepare($dbc, $sql);
mysqli_stmt_bind_param($stmt, 'i', $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $regCount);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
if ($regCount > 0) {
http_response_code(403);
echo json_encode(['status' => 'error', 'message' => 'This person has registrations and cannot be deleted.']);
mysqli_close($dbc);
exit;
}
$sql = "DELETE FROM citeLeParis_user WHERE pk_userID = ?";
$stmt = mysqli_prepare($dbc, $sql);
if (!$stmt) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => 'Failed to prepare delete statement.']);
mysqli_close($dbc);
exit;
}
mysqli_stmt_bind_param($stmt, 'i', $id);
$ok = mysqli_stmt_execute($stmt);
if ($ok) {
http_response_code(200);
$response = ['status' => 'success', 'message' => 'User deleted successfully.'];
} else {
http_response_code(500);
$response = ['status' => 'error', 'message' => 'Delete failed: '.mysqli_stmt_error($stmt)];
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
echo json_encode($response);
?>