<?php
require_once __DIR__ . '/../includes/init.php';

require_admin();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {
    $stmt = db()->prepare("DELETE FROM kb_documents WHERE id = ?");
    $stmt->execute([$_POST['delete_id']]);
    header('Location: kb_documents.php');
    exit;
}

$stmt = db()->prepare("SELECT * FROM kb_documents ORDER BY created_at DESC");
$stmt->execute();
$documents = $stmt->fetchAll();

$page_title = 'Baza Wiedzy';
?>
<?php require_once __DIR__ . '/../includes/html_head.php'; ?>
<body>

<?php include 'menu.php'; ?>

<main class="container my-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h2">Baza Wiedzy</h1>
        <a href="edit_kb_document.php" class="btn btn-primary">
            <i class="bi bi-plus-lg"></i> Dodaj nowy
        </a>
    </div>

    <div class="card">
        <div class="card-body">
            <table class="table table-striped table-hover">
                <thead class="table-light">
                    <tr>
                        <th>Tytuł</th>
                        <th>Język</th>
                        <th>Aktywny</th>
                        <th>Utworzono</th>
                        <th>Akcje</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($documents)): ?>
                        <tr>
                            <td colspan="5" class="text-center">Nie znaleziono dokumentów.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($documents as $doc) : ?>
                            <tr>
                                <td><?= htmlspecialchars($doc['title']) ?></td>
                                <td><?= htmlspecialchars($doc['language']) ?></td>
                                <td>
                                    <?php if ($doc['is_active']): ?>
                                        <span class="badge bg-success">Tak</span>
                                    <?php else: ?>
                                        <span class="badge bg-danger">Nie</span>
                                    <?php endif; ?>
                                </td>
                                <td><?= htmlspecialchars($doc['created_at']) ?></td>
                                <td>
                                    <a href="edit_kb_document.php?id=<?= $doc['id'] ?>" class="btn btn-sm btn-secondary">
                                        <i class="bi bi-pencil-fill"></i> Edytuj
                                    </a>
                                    <form action="kb_documents.php" method="POST" class="d-inline" onsubmit="return confirm('Czy na pewno chcesz usunąć ten dokument?');">
                                        <input type="hidden" name="delete_id" value="<?= $doc['id'] ?>">
                                        <button type="submit" class="btn btn-sm btn-danger"><i class="bi bi-trash-fill"></i> Usuń</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</main>

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

</body>
</html>
