<?php
require_once '../config/config.php';
require_once '../config/db.php';
$page_title = 'Détail du téléchargement';
$current_page = 'admin-liste-telechargements';

$id = intval($_GET['id'] ?? 0);
$db = getDbConnection();
$stmt = $db->prepare('SELECT * FROM telechargements WHERE id = ?');
$stmt->execute([$id]);
$dl = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$dl) {
    echo '<div class="alert alert-danger">Téléchargement introuvable.</div>';
    exit;
}

$page_content = '<div class="row justify-content-center">'
    . '<div class="col-lg-8 col-md-10">'
    . '<div class="card border border-info mt-4">'
    . '<div class="card-header"><h4 class="mb-0">Détail du téléchargement</h4></div>'
    . '<div class="card-body">'
    . '<dl class="row">'
    . '<dt class="col-sm-4">Titre</dt><dd class="col-sm-8">' . htmlspecialchars($dl['titre']) . '</dd>'
    . '<dt class="col-sm-4">Description</dt><dd class="col-sm-8">' . htmlspecialchars($dl['description']) . '</dd>'
    . '<dt class="col-sm-4">Lien/Fichier</dt><dd class="col-sm-8">';
if (!empty($dl['fichier'])) {
    $page_content .= '<a href="../downloads/' . htmlspecialchars($dl['fichier']) . '" target="_blank">Fichier</a>';
} elseif (!empty($dl['lien'])) {
    $page_content .= '<a href="' . htmlspecialchars($dl['lien']) . '" target="_blank">Lien</a>';
}
$page_content .= '</dd>'
    . '<dt class="col-sm-4">Image</dt><dd class="col-sm-8">';
if ($dl['image']) {
    $page_content .= '<img src="../downloads/' . htmlspecialchars($dl['image']) . '" alt="Image associée" style="max-width:120px;">';
} else {
    $page_content .= 'Aucune';
}
$page_content .= '</dd>'
    . '<dt class="col-sm-4">Date d\'ajout</dt><dd class="col-sm-8">' . htmlspecialchars($dl['date_ajout']) . '</dd>'
    . '<dt class="col-sm-4">Afficher</dt><dd class="col-sm-8">' . ($dl['afficher'] ? '<span class="badge bg-success">Oui</span>' : '<span class="badge bg-secondary">Non</span>') . '</dd>'
    . '</dl>'
    . '<a href="admin-liste-telechargements.php" class="btn btn-secondary">Retour à la liste</a>'
    . '</div></div></div></div>';

include 'template.php';
