<?php $title = htmlspecialchars($category->name) . ' - Documents'; ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><?= htmlspecialchars($category->name) ?></h1>
    <div><a href="<?= $baseUrl ?>/documents" class="glass-btn mr-2"><i class="fas fa-arrow-left mr-1"></i>Back</a><a href="<?= $baseUrl ?>/documents/upload" class="glass-btn glass-btn-primary"><i class="fas fa-upload mr-1"></i>Upload</a></div>
</div>
<div class="glass-table">
    <table class="table">
        <thead><tr><th>Title</th><th>Type</th><th>Size</th><th>Uploaded By</th><th>Date</th><th>Downloads</th><th>Action</th></tr></thead>
        <tbody>
            <?php if (!empty($documents)): ?>
                <?php foreach ($documents as $doc): ?>
                <tr>
                    <td><?= htmlspecialchars($doc->title) ?></td>
                    <td><span class="glass-badge"><?= htmlspecialchars($doc->file_type ?: 'N/A') ?></span></td>
                    <td><?= $doc->file_size ? round($doc->file_size / 1024, 1) . ' KB' : 'N/A' ?></td>
                    <td><?= htmlspecialchars($doc->uploaded_by_name ?? 'Unknown') ?></td>
                    <td><?= date('M d, Y', strtotime($doc->created_at)) ?></td>
                    <td><?= $doc->download_count ?? 0 ?></td>
                    <td class="table-actions">
                        <a href="<?= $baseUrl ?>/documents/<?= $doc->id ?>/download" class="btn btn-sm btn-success"><i class="fas fa-download"></i></a>
                        <?php if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'): ?>
                        <form method="POST" action="<?= $baseUrl ?>/documents/<?= $doc->id ?>/delete" style="display:inline;" onsubmit="return confirm('Delete this document?');">
                            <button type="submit" class="btn btn-sm btn-danger"><i class="fas fa-trash"></i></button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr><td colspan="7" class="text-center" style="color: rgba(255,255,255,0.5);">No documents in this category</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
