<?php
$title = htmlspecialchars($category->name) . ' - Forum';
$isModerator = isset($_SESSION['user_role']) && in_array($_SESSION['user_role'], ['admin', 'editor']);
?>
<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>
    <a href="<?= $baseUrl ?>/forum/<?= $category->id ?>/create-topic" class="glass-btn glass-btn-primary"><i class="fas fa-plus mr-1"></i>New Topic</a>
</div>
<div class="glass-card p-0">
    <?php if (!empty($topics)): ?>
        <?php foreach ($topics as $topic): ?>
        <div class="p-3" style="border-bottom: 1px solid rgba(255,255,255,0.05);">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <a href="<?= $baseUrl ?>/forum/topic/<?= $topic->id ?>" style="color: white; font-weight: 600;">
                        <?= $topic->is_pinned ? '<i class="fas fa-thumbtack mr-1 text-warning"></i>' : '' ?>
                        <?= $topic->is_locked ? '<i class="fas fa-lock mr-1 text-danger"></i>' : '' ?>
                        <?= htmlspecialchars($topic->title) ?>
                    </a>
                    <small class="d-block" style="color: rgba(255,255,255,0.5);">by <?= htmlspecialchars($topic->user_name ?? 'Unknown') ?> | <?= $topic->post_count ?? 0 ?> replies | <?= $topic->views ?> views</small>
                </div>
                <div class="d-flex align-items-center">
                    <small style="color: rgba(255,255,255,0.4);"><?= $topic->last_post_at ? date('M d, Y', strtotime($topic->last_post_at)) : date('M d, Y', strtotime($topic->created_at)) ?></small>
                    <?php if ($isModerator): ?>
                    <div class="ml-2" style="display:inline-flex; gap: 4px;">
                        <form method="POST" action="<?= $baseUrl ?>/forum/topic/<?= $topic->id ?>/pin" style="display:inline;">
                            <button type="submit" class="btn btn-sm <?= $topic->is_pinned ? 'text-warning' : '' ?>" style="background:transparent; border:none; color:rgba(255,255,255,0.4);" title="<?= $topic->is_pinned ? 'Unpin' : 'Pin' ?>"><i class="fas fa-thumbtack"></i></button>
                        </form>
                        <form method="POST" action="<?= $baseUrl ?>/forum/topic/<?= $topic->id ?>/lock" style="display:inline;">
                            <button type="submit" class="btn btn-sm" style="background:transparent; border:none; color:rgba(255,255,255,0.4);" title="<?= $topic->is_locked ? 'Unlock' : 'Lock' ?>"><i class="fas <?= $topic->is_locked ? 'fa-lock' : 'fa-unlock' ?>"></i></button>
                        </form>
                        <form method="POST" action="<?= $baseUrl ?>/forum/topic/<?= $topic->id ?>/delete" style="display:inline;" onsubmit="return confirm('Delete this topic?');">
                            <button type="submit" class="btn btn-sm" style="background:transparent; border:none; color:rgba(255,255,255,0.4);" title="Delete"><i class="fas fa-trash"></i></button>
                        </form>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    <?php else: ?>
        <div class="empty-state"><i class="fas fa-comment"></i><p>No topics yet. Be the first to start a discussion!</p></div>
    <?php endif; ?>
</div>