<?php
// personnel-document-delete.php - Eliminar un archivo de documento de personal
include __DIR__ . '/core/autoload.php';

$con = Database::getCon();

$id = intval($_GET['id'] ?? 0);
if ($id <= 0) {
    header('Location: ./?view=personnel');
    exit;
}

$stmt = $con->prepare("SELECT file_path, user_id FROM personnel_document_file WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$file = $stmt->get_result()->fetch_assoc();

if ($file) {
    $fullPath = __DIR__ . '/' . $file['file_path'];
    if (file_exists($fullPath)) {
        unlink($fullPath);
    }
    $del = $con->prepare("DELETE FROM personnel_document_file WHERE id = ?");
    $del->bind_param('i', $id);
    $del->execute();
    header('Location: ./?view=personnel-detail&user_id=' . $file['user_id']);
} else {
    header('Location: ./?view=personnel');
}
exit;
