<?php
require __DIR__ . '/_bootstrap.php';

$docId = (int)($_POST['doc_id'] ?? 0);
$studentId = (int)($_POST['student_id'] ?? 0);
if ($docId <= 0 || $studentId <= 0) { die('Invalid'); }

// Load doc
$stmt = $pdo->prepare("SELECT * FROM student_documents WHERE id=? AND student_id=?");
$stmt->execute([$docId, $studentId]);
$doc = $stmt->fetch();
if (!$doc) { header('Location: ' . url_for('/admin/student_view.php?id=' . $studentId)); exit; }

// Delete file from disk
$fs = realpath(__DIR__ . '/../' . $doc['file_path']);
if ($fs && is_file($fs)) { @unlink($fs); }

// Delete DB row
$pdo->prepare("DELETE FROM student_documents WHERE id=?")->execute([$docId]);

header('Location: ' . url_for('/admin/student_view.php?id=' . $studentId));
