<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json; charset=UTF-8");

include_once 'db_connect.php'; 

$patient_id = isset($_GET['patient_id']) ? $_GET['patient_id'] : null;

if (!$patient_id) {
    echo json_encode(["status" => "error", "message" => "Patient ID is missing"]);
    exit;
}

try {
    $conn->exec("SET NAMES utf8mb4");

    // جلب التقارير الطبية للمريض بناءً على patient_id
    $sql = "SELECT id, patient_id, user_id, report_title, report_path, created_at 
            FROM medical_reports_links 
            WHERE patient_id = :patient_id 
            ORDER BY created_at DESC";

    $stmt = $conn->prepare($sql);
    $stmt->execute([':patient_id' => $patient_id]);
    $reports = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        "status" => "success", 
        "data" => $reports
    ]);

} catch (PDOException $e) {
    echo json_encode(["status" => "error", "message" => $e->getMessage()]);
}
?>