<?php
require_once 'config/db.php';
$settings = getAllSettings($pdo);
$activePage = 'news';
$pageTitle = 'Berita & Kegiatan - '.($settings['site_name']??'');

$cat     = $_GET['cat']  ?? 'all';
$page    = max(1, (int)($_GET['page'] ?? 1));
$perPage = 9;
$offset  = ($page - 1) * $perPage;

if ($cat !== 'all') {
    $totalRow  = $pdo->prepare("SELECT COUNT(*) FROM news WHERE is_active=1 AND category=?");
    $totalRow->execute([$cat]);
    $total = $totalRow->fetchColumn();

    $stmt = $pdo->prepare("SELECT * FROM news WHERE is_active=1 AND category=? ORDER BY created_at DESC LIMIT $perPage OFFSET $offset");
    $stmt->execute([$cat]);
    $newsList = $stmt->fetchAll();
} else {
    $total    = $pdo->query("SELECT COUNT(*) FROM news WHERE is_active=1")->fetchColumn();
    $stmt     = $pdo->prepare("SELECT * FROM news WHERE is_active=1 ORDER BY created_at DESC LIMIT $perPage OFFSET $offset");
    $stmt->execute();
    $newsList = $stmt->fetchAll();
}

$totalPages = ceil($total / $perPage);

include 'partials/header.php';
?>

<div class="page-hero">
    <div class="container">
        <nav aria-label="breadcrumb"><ol class="breadcrumb mb-2"><li class="breadcrumb-item"><a href="index.php">Home</a></li><li class="breadcrumb-item active" data-en="News &amp; Activities" data-id="Berita &amp; Kegiatan">Berita &amp; Kegiatan</li></ol></nav>
        <h1 class="fw-bold mb-1 fs-2" data-en="News &amp; Activities" data-id="Berita &amp; Kegiatan">Berita &amp; Kegiatan</h1>
        <p class="mb-0" style="color:rgba(255,255,255,.6);" data-en="Latest news and activities" data-id="Berita dan kegiatan terbaru">Berita dan kegiatan terbaru</p>
    </div>
</div>

<section class="py-5 py-lg-6">
    <div class="container">
        <div class="filter-tabs fade-up">
            <a href="news.php?cat=all" class="filter-tab <?= $cat==='all'?'active':'' ?>" data-en="All" data-id="Semua">Semua</a>
            <a href="news.php?cat=news" class="filter-tab <?= $cat==='news'?'active':'' ?>" data-en="News" data-id="Berita">Berita</a>
            <a href="news.php?cat=event" class="filter-tab <?= $cat==='event'?'active':'' ?>" data-en="Event" data-id="Kegiatan">Kegiatan</a>
        </div>

        <div class="row g-4">
            <?php foreach ($newsList as $n):
                $catLabel = $n['category'] === 'event' ? 'Kegiatan' : 'Berita';
            ?>
            <div class="col-sm-6 col-lg-4 fade-up">
                <a href="news-detail.php?slug=<?= urlencode($n['slug']) ?>" class="text-decoration-none">
                    <div class="news-card">
                        <div class="news-img">
                            <img src="<?= htmlspecialchars($n['image']??'') ?>" alt="<?= htmlspecialchars($n['title']) ?>"
                                 onerror="this.src='https://placehold.co/600x350/e0e0e0/888?text=News'">
                            <span class="news-cat"><?= $catLabel ?></span>
                        </div>
                        <div class="news-body">
                            <div class="news-date"><i class="bi bi-calendar3 me-1"></i><?= date('d M Y', strtotime($n['created_at'])) ?></div>
                            <h6 class="news-title"><?= htmlspecialchars($n['title']) ?></h6>
                            <p class="news-excerpt"><?= htmlspecialchars(substr(strip_tags($n['excerpt']??''),0,120)).'...' ?></p>
                        </div>
                    </div>
                </a>
            </div>
            <?php endforeach; ?>
            <?php if (empty($newsList)): ?>
                <div class="col-12 text-center py-5 text-muted">
                    <i class="bi bi-newspaper display-4 d-block mb-3"></i>Belum ada berita tersedia.
                </div>
            <?php endif; ?>
        </div>

        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
        <nav class="mt-5 d-flex justify-content-center fade-up">
            <ul class="pagination">
                <?php if ($page > 1): ?>
                    <li class="page-item"><a class="page-link" href="?cat=<?= urlencode($cat) ?>&page=<?= $page-1 ?>">&laquo;</a></li>
                <?php endif; ?>
                <?php for ($i=1;$i<=$totalPages;$i++): ?>
                    <li class="page-item <?= $i==$page?'active':'' ?>">
                        <a class="page-link" href="?cat=<?= urlencode($cat) ?>&page=<?= $i ?>"><?= $i ?></a>
                    </li>
                <?php endfor; ?>
                <?php if ($page < $totalPages): ?>
                    <li class="page-item"><a class="page-link" href="?cat=<?= urlencode($cat) ?>&page=<?= $page+1 ?>">&raquo;</a></li>
                <?php endif; ?>
            </ul>
        </nav>
        <?php endif; ?>
    </div>
</section>

<?php include 'partials/footer.php'; ?>
