<?php
require_once __DIR__ . '/admin/functions.php';

// Get current page from URL parameter
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;

// Set page variables for header
$pageTitle = 'Berita Terbaru' . ($currentPage > 1 ? ' - Halaman ' . $currentPage : '') . ' - Darurat Mafia Tanah';
$pageDescription = 'Kumpulan berita investigasi terbaru tentang kasus mafia tanah dan perjuangan keadilan agraria di Indonesia.' . ($currentPage > 1 ? ' Halaman ' . $currentPage . '.' : '');
$showAdminLink = false; // Hide admin link on public pages

// Get articles with pagination (15 per page)
$paginationData = getPublishedArticlesWithPagination($currentPage, 15);
$articles = $paginationData['articles'];
$totalPages = $paginationData['totalPages'];
$totalArticles = $paginationData['totalArticles'];

// Uncomment baris berikut untuk debugging
// echo '<pre>';
// var_dump($articles);
// echo '</pre>';

// Add news-specific CSS
$extraCSS = '
    <style>
        .news-item {
            background: #fff;
            margin-bottom: 2rem;
            padding: 1.5rem;
            border-radius: 8px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
            display: grid;
            grid-template-columns: 300px 1fr;
            gap: 1.5rem;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .news-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.15);
        }
        .news-item img {
            width: 100%;
            height: 200px;
            object-fit: cover;
            border-radius: 6px;
        }
        .article-content {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        .article-meta {
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 0.5rem;
            font-weight: 500;
        }
        .news-item h3 {
            margin: 0 0 1rem 0;
            font-size: 1.4rem;
            color: #2c3e50;
            line-height: 1.3;
        }
        .news-item h3:hover {
            color: #3498db;
        }
        .news-item p {
            color: #555;
            line-height: 1.6;
            margin-bottom: 1rem;
        }
        .read-more {
            display: inline-block;
            color: #3498db;
            text-decoration: none;
            font-weight: 600;
            margin-top: 1rem;
            transition: color 0.3s ease;
        }
        .read-more:hover {
            color: #2980b9;
            text-decoration: underline;
        }
        .read-more::after {
            content: " →";
            margin-left: 0.3rem;
        }
        @media (max-width: 768px) {
            .news-item {
                grid-template-columns: 1fr;
                padding: 1rem;
            }
            .news-item img {
                height: 180px;
            }
            .news-item h3 {
                font-size: 1.2rem;
            }
        }
        
        /* Pagination Styles */
        .pagination-container {
            margin: 3rem 0 2rem;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .pagination {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            background: white;
            padding: 1rem;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            border: 1px solid #e1e8ed;
        }
        
        .pagination-item {
            display: flex;
            align-items: center;
            justify-content: center;
            min-width: 40px;
            height: 40px;
            padding: 0.5rem;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .pagination-item:hover {
            transform: translateY(-1px);
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        
        .pagination-item.current {
            background: #3498db;
            color: white;
            border-color: #3498db;
        }
        
        .pagination-item.number {
            color: #2c3e50;
        }
        
        .pagination-item.number:hover {
            background: #f8f9fa;
            border-color: #e1e8ed;
        }
        
        .pagination-item.nav {
            color: #666;
            min-width: 50px;
        }
        
        .pagination-item.nav:hover {
            background: #3498db;
            color: white;
        }
        
        .pagination-item.nav.disabled {
            color: #ccc;
            cursor: not-allowed;
            pointer-events: none;
        }
        
        .pagination-icon {
            font-size: 0.9rem;
        }
        
        .pagination-dots {
            color: #999;
            font-weight: bold;
            padding: 0 0.5rem;
        }
        
        .pagination-info {
            text-align: center;
            color: #666;
            margin-top: 1rem;
            font-size: 0.9rem;
        }
        
        @media (max-width: 768px) {
            .pagination {
                padding: 0.8rem;
                gap: 0.3rem;
            }
            
            .pagination-item {
                min-width: 36px;
                height: 36px;
                font-size: 0.9rem;
            }
            
            .pagination-item.nav {
                min-width: 45px;
            }
        }
    </style>
';

// Include header
include 'includes/header.php';
?>
    <main>
        <div class="container main-content">
            <h1 class="page-title">Berita Terbaru<?php echo $currentPage > 1 ? ' - Halaman ' . $currentPage : ''; ?></h1>
            
            <div class="news-list">
                <?php if (empty($articles)): ?>
                    <p>Belum ada berita yang dipublikasikan.</p>
                <?php else: ?>
                    <?php foreach ($articles as $article): ?>
                        <article class="news-item">
                            <div class="image-container">
                                <?php if (!empty($article['image'])): ?>
                                    <img src="/articles/<?php echo htmlspecialchars($article['image']); ?>" 
                                         alt="<?php echo htmlspecialchars($article['title']); ?>"
                                         class="lazy-image"
                                         loading="lazy"
                                         decoding="async">
                                <?php else: ?>
                                    <img src="https://via.placeholder.com/800x400/34495e/ffffff?text=Berita" 
                                         alt="<?php echo htmlspecialchars($article['title']); ?>"
                                         class="lazy-image"
                                         loading="lazy"
                                         decoding="async">
                                <?php endif; ?>
                            </div>
                            <div class="article-content">
                                <div>
                                    <div class="article-meta">
                                        Dipublikasikan: <?php echo date('d F Y', strtotime($article['date'])); ?>
                                    </div>
                                    <h3>
                                        <?php if (isset($article['slug']) && !empty($article['slug'])): ?>
                                            <a href="/artikel/<?php echo urlencode($article['slug']); ?>" class="article-title-link">
                                                <?php echo htmlspecialchars($article['title']); ?>
                                            </a>
                                        <?php else: ?>
                                            <a href="/artikel?id=<?php echo $article['id']; ?>" class="article-title-link">
                                                <?php echo htmlspecialchars($article['title']); ?>
                                            </a>
                                        <?php endif; ?>
                                    </h3>
                                    <p><?php 
                                        $excerpt = strip_tags($article['content']);
                                        echo strlen($excerpt) > 200 ? substr($excerpt, 0, 200) . '...' : $excerpt;
                                    ?></p>
                                </div>
                                <div>
                                    <?php if (isset($article['slug']) && !empty($article['slug'])): ?>
                                        <a href="/artikel/<?php echo urlencode($article['slug']); ?>" class="read-more">
                                            Baca Selengkapnya »
                                        </a>
                                    <?php else: ?>
                                        <a href="/artikel?id=<?php echo $article['id']; ?>" class="read-more">
                                            Baca Selengkapnya »
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </article>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
            
            <!-- Pagination -->
            <?php if ($totalPages > 1): ?>
                <div class="pagination-container">
                    <nav class="pagination" role="navigation" aria-label="Pagination Navigation">
                        <!-- Previous Button -->
                        <?php if ($currentPage > 1): ?>
                            <a href="/berita-terbaru?page=<?php echo $currentPage - 1; ?>" 
                               class="pagination-item nav" 
                               aria-label="Previous page">
                                <span class="pagination-icon">‹</span>
                            </a>
                        <?php else: ?>
                            <span class="pagination-item nav disabled">
                                <span class="pagination-icon">‹</span>
                            </span>
                        <?php endif; ?>
                        
                        <!-- Page Numbers -->
                        <?php
                        // Always show page 1
                        if ($currentPage == 1) {
                            echo '<span class="pagination-item number current">1</span>';
                        } else {
                            echo '<a href="/berita-terbaru?page=1" class="pagination-item number">1</a>';
                        }
                        
                        // Show page 2 if exists
                        if ($totalPages >= 2) {
                            if ($currentPage == 2) {
                                echo '<span class="pagination-item number current">2</span>';
                            } else {
                                echo '<a href="/berita-terbaru?page=2" class="pagination-item number">2</a>';
                            }
                        }
                        
                        // Show page 3 if exists
                        if ($totalPages >= 3) {
                            if ($currentPage == 3) {
                                echo '<span class="pagination-item number current">3</span>';
                            } else {
                                echo '<a href="/berita-terbaru?page=3" class="pagination-item number">3</a>';
                            }
                        }
                        
                        // Show dots if there are more pages after 3
                        if ($totalPages > 4) {
                            echo '<span class="pagination-dots">...</span>';
                        }
                        
                        // Show current page if it's beyond page 3
                        if ($currentPage > 3 && $currentPage < $totalPages) {
                            echo '<span class="pagination-item number current">' . $currentPage . '</span>';
                        }
                        
                        // Show last page if total pages > 3
                        if ($totalPages > 3) {
                            if ($currentPage == $totalPages) {
                                echo '<span class="pagination-item number current">' . $totalPages . '</span>';
                            } else {
                                echo '<a href="/berita-terbaru?page=' . $totalPages . '" class="pagination-item number">' . $totalPages . '</a>';
                            }
                        }
                        ?>
                        
                        <!-- Next Button -->
                        <?php if ($currentPage < $totalPages): ?>
                            <a href="/berita-terbaru?page=<?php echo $currentPage + 1; ?>" 
                               class="pagination-item nav" 
                               aria-label="Next page">
                                <span class="pagination-icon">›</span>
                            </a>
                        <?php else: ?>
                            <span class="pagination-item nav disabled">
                                <span class="pagination-icon">›</span>
                            </span>
                        <?php endif; ?>
                    </nav>
                </div>
                
                <!-- Pagination Info -->
                <div class="pagination-info">
                    Menampilkan <?php echo (($currentPage - 1) * 15) + 1; ?> - <?php echo min($currentPage * 15, $totalArticles); ?> 
                    dari <?php echo $totalArticles; ?> artikel 
                    (Halaman <?php echo $currentPage; ?> dari <?php echo $totalPages; ?>)
                </div>
            <?php endif; ?>
        </div>
    </main>

<?php
// Include footer
include 'includes/footer.php';
?> 