<?php
/**
 * Delete Case
 * Permanently delete a case and its follow-ups
 */

require_once '../../config/config.php';
require_once '../../includes/functions.php';

requireLogin();

$pdo = getDB();
$caseId = (int)($_GET['id'] ?? 0);
$userId = $_SESSION['user_id'];

// Verify case belongs to doctor
$stmt = $pdo->prepare("SELECT id, patient_name FROM cases WHERE id = ? AND user_id = ?");
$stmt->execute([$caseId, $userId]);
$case = $stmt->fetch();

if (!$case) { header('Location: list.php?error=Case not found'); exit; }

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $confirm = $_POST['confirm'] ?? '';
    if ($confirm == 'DELETE') {
        $stmt = $pdo->prepare("DELETE FROM cases WHERE id = ? AND user_id = ?");
        $stmt->execute([$caseId, $userId]);
        header('Location: list.php?msg=Case deleted successfully');
        exit;
    } else {
        $error = "Please type DELETE to confirm";
    }
}
?>
<!DOCTYPE html>
<html>
<head><title>Delete Case</title><link href="../../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"></head>
<body>
<div class="container mt-5"><div class="row"><div class="col-md-6 mx-auto"><div class="card"><div class="card-header bg-danger text-white"><h4>Delete Case</h4></div>
<div class="card-body"><?php if(isset($error)): ?><div class="alert alert-danger"><?php echo $error; ?></div><?php endif; ?>
<p>You are about to delete case for: <strong><?php echo escape($case['patient_name']); ?></strong></p>
<div class="alert alert-warning">This action cannot be undone. All follow-ups will also be deleted.</div>
<form method="POST"><div class="mb-3"><label>Type <strong>DELETE</strong> to confirm:</label><input type="text" name="confirm" class="form-control" placeholder="DELETE" required></div>
<button type="submit" class="btn btn-danger">Delete Permanently</button><a href="view.php?id=<?php echo $caseId; ?>" class="btn btn-secondary">Cancel</a></form></div></div></div></div></div>
</body>
</html>