<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require_once __DIR__ . '/../../includes/auth.php';
require_role(['super_admin','branch_admin']);
require_once __DIR__ . '/../../includes/db.php';

function cdb_table_exists(mysqli $db, string $t): bool {
  $st=$db->prepare("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? LIMIT 1");
  $st->bind_param('s',$t); $st->execute();
  $ok=(bool)$st->get_result()->fetch_row(); $st->close(); return $ok;
}

$candidateTables = ['centres','centers','branches','branch','venues','locations'];
$TBL = ''; foreach ($candidateTables as $t) if (cdb_table_exists($mysqli,$t)) { $TBL=$t; break; }
if ($TBL === '') { header('Location: index.php'); exit; }

$id = (int)($_POST['id'] ?? 0);
if ($id>0) {
  $mysqli->query("DELETE FROM `$TBL` WHERE `id`=".(int)$id);
}
header('Location: index.php');
