<?php
require_once __DIR__ . '/../includes/config.php';

if (!isLoggedIn()) {
    redirectWithMessage(APP_URL . '/login.php', 'Unauthorized', 'error');
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    redirectWithMessage(APP_URL . '/admin/settings.php', 'Invalid request', 'error');
}

$type = escape($_POST['type'] ?? '');
$id = (int)($_POST['id'] ?? 0);

$tables = [
    'category' => 'categories',
    'order' => 'order_statuses',
    'client' => 'client_statuses'
];

$table = $tables[$type] ?? null;

if (!$table || $id <= 0) {
    redirectWithMessage(APP_URL . '/admin/settings.php', 'Invalid request', 'error');
}

$db->query("DELETE FROM $table WHERE id = $id");
redirectWithMessage(APP_URL . '/admin/settings.php', 'Șters cu succes', 'success');
?>
