<?php
require_once __DIR__ . '/../shared/includes/protect.php';
include 'conexion.php';

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['id_trabajador'])) {
    $id_trabajador = intval($_POST['id_trabajador']);

    if ($id_trabajador > 0) {
        try {
            $sql = "DELETE FROM Trabajador WHERE id_trabajador = ?";
            $stmt = $pdo->prepare($sql);
            $result = $stmt->execute([$id_trabajador]);

            if ($result) {
                header("Location: index.php?success=deleted");
                exit();
            } else {
                header("Location: index.php?error=delete_failed");
                exit();
            }
        } catch (PDOException $e) {
            header("Location: index.php?error=db_error");
            exit();
        }
    }
}
?>
