<?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 lec_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 = ['lecturers','lecturer','teachers','teacher','staff','faculty','instructors','instructor'];
$TBL = ''; foreach ($candidateTables as $t) if (lec_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');
