<div class="mb-4">
    <h2 class="mb-2">Artyku&#322;y <small class="text-muted">(<?= $total ?>)</small></h2>
    <div class="d-flex align-items-center gap-2 flex-wrap">
        <form method="get" action="/articles" class="d-flex align-items-center gap-2 flex-wrap">
            <label for="site_id" class="small text-muted">Strona WWW:</label>
            <select name="site_id" id="site_id" class="form-select form-select-sm w-auto" style="min-width: 220px; width: 260px;">
                <option value="">Wszystkie</option>
                <?php foreach ($sites as $site): ?>
                    <option value="<?= $site['id'] ?>" <?= (int) $selectedSiteId === (int) $site['id'] ? 'selected' : '' ?>>
                        <?= htmlspecialchars($site['name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <button type="submit" class="btn btn-sm btn-outline-primary">Filtruj</button>
            <?php if (!empty($selectedSiteId)): ?>
                <a href="/articles" class="btn btn-sm btn-outline-secondary">Wyczy&#347;&#263;</a>
            <?php endif; ?>
        </form>

        <?php if (!empty($selectedSiteId)): ?>
            <form method="post" action="/articles/import" data-confirm="Pobrac i zaimportowac opublikowane artykuly z WordPress?">
                <input type="hidden" name="site_id" value="<?= (int) $selectedSiteId ?>">
                <button type="submit" class="btn btn-sm btn-success">Pobierz artyku&#322;y</button>
            </form>
        <?php endif; ?>
    </div>
</div>

<div class="card">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Tytu&#322;</th>
                    <th>Strona</th>
                    <th>Temat</th>
                    <th>Status</th>
                    <th>Data</th>
                    <th>Akcje</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($articles)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4">Brak artyku&#322;&#243;w</td></tr>
                <?php else: ?>
                    <?php foreach ($articles as $article): ?>
                        <tr>
                            <td><?= $article['id'] ?></td>
                            <td>
                                <a href="/articles/<?= $article['id'] ?>">
                                    <?= htmlspecialchars(mb_strimwidth($article['title'], 0, 50, '...')) ?>
                                </a>
                            </td>
                            <td><?= htmlspecialchars($article['site_name']) ?></td>
                            <td><span class="badge bg-secondary"><?= htmlspecialchars($article['topic_name']) ?></span></td>
                            <td>
                                <?php if ($article['status'] === 'published'): ?>
                                    <span class="badge bg-success">Opublikowany</span>
                                <?php elseif ($article['status'] === 'failed'): ?>
                                    <span class="badge bg-danger" title="<?= htmlspecialchars($article['error_message'] ?? '') ?>">B&#322;&#261;d</span>
                                <?php else: ?>
                                    <span class="badge bg-warning">Wygenerowany</span>
                                <?php endif; ?>
                                <?php if ((int) ($article['retry_count'] ?? 0) > 0): ?>
                                    <div class="small text-muted mt-1">
                                        Retry: <?= (int) $article['retry_count'] ?>
                                        <?php if (!empty($article['last_retry_at'])): ?>
                                            (<?= date('d.m.Y H:i', strtotime($article['last_retry_at'])) ?>)
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </td>
                            <td class="small"><?= $article['published_at'] ? date('d.m.Y H:i', strtotime($article['published_at'])) : date('d.m.Y H:i', strtotime($article['created_at'])) ?></td>
                            <td>
                                <div class="btn-group btn-group-sm">
                                    <a href="/articles/<?= $article['id'] ?>" class="btn btn-outline-primary" title="Podglad">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                    <form method="post" action="/articles/<?= $article['id'] ?>/delete" class="d-inline" data-confirm="Na pewno usunac artykul z systemu i WordPress?">
                                        <button type="submit" class="btn btn-outline-danger" title="Usun">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if ($totalPages > 1): ?>
<nav class="mt-3">
    <ul class="pagination justify-content-center">
        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
            <?php
                $query = ['page' => $i];
                if (!empty($selectedSiteId)) {
                    $query['site_id'] = $selectedSiteId;
                }
            ?>
            <li class="page-item <?= $i === $page ? 'active' : '' ?>">
                <a class="page-link" href="/articles?<?= http_build_query($query) ?>"><?= $i ?></a>
            </li>
        <?php endfor; ?>
    </ul>
</nav>
<?php endif; ?>


