<?php
$links = $links ?? [];
$stats = $stats ?? [];
$page = (int) ($page ?? 1);
$totalPages = (int) ($totalPages ?? 1);
$totalLinks = (int) ($totalLinks ?? 0);

$active = (int) ($stats['active'] ?? 0);
$expired = (int) ($stats['expired'] ?? 0);
$removed = (int) ($stats['removed'] ?? 0);
$failed = (int) ($stats['failed'] ?? 0);

$statusBadge = function (string $status): string {
    return match ($status) {
        'active' => '<span class="badge bg-success">Aktywny</span>',
        'expired' => '<span class="badge bg-secondary">Wygasly</span>',
        'removed' => '<span class="badge bg-secondary">Usuniety</span>',
        'failed' => '<span class="badge bg-danger">Blad</span>',
        default => '<span class="badge bg-light text-dark">' . htmlspecialchars($status) . '</span>',
    };
};
?>

<h4 class="fw-bold mb-4">StatLink &mdash; Publikacje</h4>

<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="card text-center border-success border-opacity-25">
            <div class="card-body py-3">
                <div class="text-muted small text-uppercase fw-bold">Aktywne</div>
                <div class="fs-3 fw-bold text-success"><?= $active ?></div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-center">
            <div class="card-body py-3">
                <div class="text-muted small text-uppercase fw-bold">Wygasle</div>
                <div class="fs-3 fw-bold text-secondary"><?= $expired ?></div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-center">
            <div class="card-body py-3">
                <div class="text-muted small text-uppercase fw-bold">Usuniete</div>
                <div class="fs-3 fw-bold text-secondary"><?= $removed ?></div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-center border-danger border-opacity-25">
            <div class="card-body py-3">
                <div class="text-muted small text-uppercase fw-bold">Bledy</div>
                <div class="fs-3 fw-bold text-danger"><?= $failed ?></div>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span>Lista linkow (<?= $totalLinks ?>)</span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-sm table-hover mb-0">
                <thead>
                    <tr>
                        <th>Artykul</th>
                        <th>Strona</th>
                        <th>Anchor</th>
                        <th>Dodano</th>
                        <th>Wygasa</th>
                        <th>Status</th>
                        <th>ID</th>
                    </tr>
                </thead>
                <tbody>
                <?php if (empty($links)): ?>
                    <tr><td colspan="7" class="text-muted text-center py-4">Brak linkow StatLink.</td></tr>
                <?php else: ?>
                    <?php foreach ($links as $link): ?>
                    <tr>
                        <td>
                            <?php if (!empty($link['article_title'])): ?>
                                <a href="/articles/<?= (int) $link['article_id'] ?>" class="text-decoration-none">
                                    <?= htmlspecialchars(mb_strimwidth((string) $link['article_title'], 0, 50, '...')) ?>
                                </a>
                            <?php else: ?>
                                <span class="text-muted">ID: <?= (int) $link['article_id'] ?></span>
                            <?php endif; ?>
                        </td>
                        <td class="text-nowrap"><?= htmlspecialchars((string) ($link['site_name'] ?? '')) ?></td>
                        <td>
                            <small class="text-muted"><?= htmlspecialchars(mb_strimwidth((string) $link['anchor'], 0, 40, '...')) ?></small>
                        </td>
                        <td class="text-nowrap">
                            <small><?= !empty($link['added_at']) ? date('Y-m-d', strtotime($link['added_at'])) : '-' ?></small>
                        </td>
                        <td class="text-nowrap">
                            <small><?= !empty($link['expires_at']) ? date('Y-m-d', strtotime($link['expires_at'])) : '-' ?></small>
                        </td>
                        <td><?= $statusBadge((string) $link['status']) ?></td>
                        <td><small class="text-muted"><?= $link['statlink_id'] ?? '-' ?></small></td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="card-footer">
        <nav>
            <ul class="pagination pagination-sm mb-0 justify-content-center">
                <?php if ($page > 1): ?>
                    <li class="page-item"><a class="page-link" href="/statlink?page=<?= $page - 1 ?>">&laquo;</a></li>
                <?php endif; ?>
                <?php for ($i = max(1, $page - 2); $i <= min($totalPages, $page + 2); $i++): ?>
                    <li class="page-item <?= $i === $page ? 'active' : '' ?>">
                        <a class="page-link" href="/statlink?page=<?= $i ?>"><?= $i ?></a>
                    </li>
                <?php endfor; ?>
                <?php if ($page < $totalPages): ?>
                    <li class="page-item"><a class="page-link" href="/statlink?page=<?= $page + 1 ?>">&raquo;</a></li>
                <?php endif; ?>
            </ul>
        </nav>
    </div>
    <?php endif; ?>
</div>

<?php if (!empty($links)): ?>
<div class="mt-3 text-muted small">
    <strong>Legenda:</strong>
    <?= $statusBadge('active') ?> linkowany w StatLink &middot;
    <?= $statusBadge('expired') ?> oczekuje na usuniecie &middot;
    <?= $statusBadge('removed') ?> usuniety ze StatLink &middot;
    <?= $statusBadge('failed') ?> blad dodawania/usuwania
</div>
<?php endif; ?>
