<?php
require_once 'includes/auth.php';
if (!checkPermission('admin')) {
    http_response_code(403);
    exit;
}
require_once 'config/database.php';

$sous_dossier_id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$sous_dossier_id) {
    echo json_encode(['error' => 'ID manquant']);
    exit;
}

$pdo = getPDO();
$stmt = $pdo->prepare("SELECT contenu_template FROM templates WHERE sous_dossier_id = ?");
$stmt->execute([$sous_dossier_id]);
$template = $stmt->fetch();
if ($template) {
    header('Content-Type: application/json');
    echo $template['contenu_template'];
} else {
    echo json_encode(['error' => 'Template non trouvé']);
}