<?php
// Halaman berita.php (Berita Kegiatan)
$pageTitle = 'Berita Kegiatan';
$activePage = 'berita';
require_once '../includes/header.php';

// Pagination setup
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 9; // Tampilkan 9 berita per halaman
$offset = ($current_page - 1) * $per_page;

// Ambil data berita dengan pagination
$db = dbConnect();
$db->query("SELECT b.*, u.nama_lengkap as penulis 
            FROM berita b 
            JOIN users u ON b.created_by = u.id 
            ORDER BY b.created_at DESC 
            LIMIT :offset, :per_page");
$db->bind(':offset', $offset);
$db->bind(':per_page', $per_page);
$berita_list = $db->resultSet();

// Hitung total berita untuk pagination
$db->query("SELECT COUNT(*) as total FROM berita");
$total_records = $db->single()->total;
$total_pages = ceil($total_records / $per_page);
?>

<!-- Hero Section Kecil -->
<section class="py-5 bg-light">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h1 class="fw-bold mb-4">Berita Kegiatan</h1>
                <p class="lead">Ikuti perkembangan kegiatan dan program terbaru dari Yayasan Pundi Amal Mulia</p>
            </div>
        </div>
    </div>
</section>

<!-- Berita List Section -->
<section class="py-5">
    <div class="container">
        <?php if (empty($berita_list)): ?>
            <div class="row">
                <div class="col-12 text-center">
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i> Belum ada berita kegiatan saat ini.
                    </div>
                </div>
            </div>
        <?php else: ?>
            <div class="row">
                <?php foreach ($berita_list as $berita): ?>
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card news-card h-100">
                            <img src="<?= SITE_URL ?>/assets/uploads/berita/<?= $berita->gambar ?>" class="card-img-top" alt="<?= $berita->judul ?>">
                            <div class="card-body">
                                <span class="news-date">
                                    <i class="far fa-calendar-alt me-1"></i> <?= tanggal_indo(date('Y-m-d', strtotime($berita->created_at)), true) ?>
                                </span>
                                <h5 class="card-title"><?= $berita->judul ?></h5>
                                <p class="card-text text-muted"><?= potongTeks($berita->konten, 150) ?></p>
                            </div>
                            <div class="card-footer bg-white">
                                <div class="d-flex justify-content-between align-items-center">
                                    <a href="<?= SITE_URL ?>/pages/berita-detail.php?id=<?= $berita->id ?>" class="btn btn-link text-success p-0">Baca Selengkapnya <i class="fas fa-arrow-right ms-1"></i></a>
                                    <small class="text-muted">By: <?= $berita->penulis ?></small>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            
            <!-- Pagination -->
            <?php if ($total_pages > 1): ?>
            <div class="row mt-4">
                <div class="col-12">
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-center">
                            <!-- Previous Page -->
                            <?php if ($current_page > 1): ?>
                                <li class="page-item">
                                    <a class="page-link" href="?page=<?= $current_page - 1 ?>" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                            <?php else: ?>
                                <li class="page-item disabled">
                                    <a class="page-link" href="#" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                            <?php endif; ?>
                            
                            <!-- Page Numbers -->
                            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                <li class="page-item <?= $i == $current_page ? 'active' : '' ?>">
                                    <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                                </li>
                            <?php endfor; ?>
                            
                            <!-- Next Page -->
                            <?php if ($current_page < $total_pages): ?>
                                <li class="page-item">
                                    <a class="page-link" href="?page=<?= $current_page + 1 ?>" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            <?php else: ?>
                                <li class="page-item disabled">
                                    <a class="page-link" href="#" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            <?php endif; ?>
                        </ul>
                    </nav>
                </div>
            </div>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</section>

<?php require_once '../includes/footer.php'; ?>