<?php
// study-delete.php - Eliminar un estudio complementario
include __DIR__ . '/core/autoload.php';

$con = Database::getCon();

$id = intval($_GET['id'] ?? 0);
$pacientId = intval($_GET['pacient_id'] ?? 0);

if ($id <= 0) {
    header('Location: ./?view=studies&pacient_id=' . $pacientId);
    exit;
}

$stmt = $con->prepare("SELECT file_path, pacient_id FROM patient_study 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 patient_study WHERE id = ?");
    $del->bind_param('i', $id);
    $del->execute();
    $pacientId = $file['pacient_id'];
}

header('Location: ./?view=studies&pacient_id=' . $pacientId . '&deleted=1');
exit;
