
<?php
require_once '../config/config.php';
require_once '../config/db.php';
$page_title = 'Liste des fichiers à télécharger';
$current_page = 'admin-liste-telechargements';

$db = getDbConnection();
$stmt = $db->query('SELECT * FROM telechargements ORDER BY date_ajout DESC');
$downloads = $stmt->fetchAll(PDO::FETCH_ASSOC);


$page_content = '<div class="row justify-content-center">'
    . '<div class="col-lg-12">'
    . '<div class="card border border-primary mt-4">'
    . '<div class="card-header"><h4 class="mb-0">Liste des fichiers à télécharger</h4></div>'
    . '<div class="card-body">'
    // Filtre de recherche
    // ...pas de filtre...
    . '<table id="downloadsTable" class="table table-striped table-bordered">'
    . '<thead><tr>'
    . '<th>Type</th>'
    . '<th>Titre</th>'
    // ...colonne Description supprimée...
    . '<th>Fichier/Lien</th>'
    . '<th>Date d\'ajout</th>'
    . '<th>Afficher</th>'
    . '<th>Actions</th>'
    . '</tr></thead><tbody>';
foreach ($downloads as $dl) {
    $page_content .= '<tr>';
    // Colonne type avec icône
    if (!empty($dl['fichier'])) {
        $page_content .= '<td class="text-center"><i class="ri-file-download-line text-primary" title="Fichier"></i></td>';
    } elseif (!empty($dl['lien'])) {
        $page_content .= '<td class="text-center"><i class="ri-link text-success" title="Lien"></i></td>';
    } else {
        $page_content .= '<td class="text-center"><i class="ri-question-line text-muted" title="Inconnu"></i></td>';
    }
    $page_content .= '<td>' . htmlspecialchars($dl['titre']) . '</td>'
        . '<td>';
    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 .= '</td>'
        . '<td>' . htmlspecialchars($dl['date_ajout']) . '</td>'
        // Bouton afficher/cacher
        . '<td class="text-center">'
        . '<form method="post" action="admin-toggle-affichage.php" style="display:inline">'
        . '<input type="hidden" name="id" value="' . $dl['id'] . '">' 
        . '<button type="submit" name="toggle" value="' . ($dl['afficher'] ? '0' : '1') . '" class="btn btn-sm ' . ($dl['afficher'] ? 'btn-success' : 'btn-secondary') . '" title="' . ($dl['afficher'] ? 'Cacher' : 'Afficher') . '">' 
        . '<i class="' . ($dl['afficher'] ? 'ri-eye-line' : 'ri-eye-off-line') . '"></i></button></form>'
        . '</td>'
        . '<td class="text-center">'
        . '<a href="admin-editer-telechargement.php?id=' . $dl['id'] . '" class="btn btn-sm btn-warning" title="Éditer"><i class="ri-edit-line"></i></a> '
        . '<a href="admin-details-telechargement.php?id=' . $dl['id'] . '" class="btn btn-sm btn-info" title="Détails"><i class="ri-eye-line"></i></a> '
        . '<a href="admin-supprimer-telechargement.php?id=' . $dl['id'] . '" class="btn btn-sm btn-danger" title="Supprimer" onclick="return confirm(\'Confirmer la suppression ?\');"><i class="ri-delete-bin-line"></i></a>'
        . '</td>'
        . '</tr>';
}
$page_content .= '</tbody></table>'
    . '</div></div></div></div>';

// Ajout DataTables


$page_content .= '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/remixicon@3.5.0/fonts/remixicon.css">'
    . '<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">';

$page_js_files = [
    'https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js',
    'https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js'
];

$page_js = '$(document).ready(function() {'
    . 'var table = $("#downloadsTable").DataTable({language: {url: "//cdn.datatables.net/plug-ins/1.13.6/i18n/fr-FR.json"}});'
    . '$("#filterInput").on("keyup", function() {table.search($(this).val()).draw();});'
    . '});';

include 'template.php';
