<?php
require_once '../includes/auth.php';
if (!checkPermission('admin')) {
    header('Location: ../dashboard.php');
    exit;
}
require_once '../config/database.php';
require_once '../includes/functions.php';

$pdo = getPDO();
$error = '';
$success = '';

// Suppression d’un template
if (isset($_GET['delete'])) {
    $id = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT);
    if ($id) {
        try {
            $stmt = $pdo->prepare("DELETE FROM templates_library WHERE id = ?");
            $stmt->execute([$id]);
            logAction($pdo, $_SESSION['user_id'], 'delete_template_library', "Template ID $id");
            $success = "Template supprimé.";
        } catch (Exception $e) {
            $error = "Erreur : " . $e->getMessage();
        }
    }
    header('Location: templates_library.php');
    exit;
}

$templates = $pdo->query("SELECT * FROM templates_library ORDER BY nom")->fetchAll();

require_once '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1><i class="fas fa-book me-2"></i>Bibliothèque de templates</h1>
    <a href="editer_template_library.php" class="btn btn-success"><i class="fas fa-plus"></i> Nouveau template</a>
</div>

<?php if ($error): ?><div class="alert alert-danger"><?= $error ?></div><?php endif; ?>
<?php if ($success): ?><div class="alert alert-success"><?= $success ?></div><?php endif; ?>

<?php if (empty($templates)): ?>
    <div class="alert alert-info">Aucun template en bibliothèque.</div>
<?php else: ?>
    <div class="table-responsive">
        <table class="table table-striped table-hover" id="templatesTable">
            <thead class="table-dark">
                <tr>
                    <th>Nom</th>
                    <th>Description</th>
                    <th>Date création</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($templates as $tpl): ?>
                <tr>
                    <td><?= htmlspecialchars($tpl['nom']) ?></td>
                    <td><?= nl2br(htmlspecialchars($tpl['description'])) ?></td>
                    <td><?= date('d/m/Y H:i', strtotime($tpl['created_at'])) ?></td>
                    <td class="text-end">
                        <a href="editer_template_library.php?id=<?= $tpl['id'] ?>" class="btn btn-sm btn-warning"><i class="fas fa-edit"></i> Modifier</a>
                        <a href="?delete=<?= $tpl['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Supprimer ce template ?')"><i class="fas fa-trash"></i> Supprimer</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>

<script>
$(document).ready(function() {
    $('#templatesTable').DataTable({
        language: { url: '//cdn.datatables.net/plug-ins/1.13.4/i18n/fr-FR.json' },
        order: [[0, 'asc']],
        pageLength: 25
    });
});
</script>

<?php require_once '../includes/footer.php'; ?>