<?php
/**
 * News Listing Page — Gmind Institute
 *
 * News listing with branch filter, news cards with branch badges,
 * and pagination. Same layout as blog.
 *
 * Expected PHP variables:
 *   $news         — array of news records
 *   $branches     — array of branch records (for filter dropdown)
 *   $currentBranch — string current branch filter (optional)
 *   $pagination   — array with 'current', 'total', 'last', 'prev', 'next' keys
 *   $csrfToken    — CSRF token string
 *   $currentPage  — string 'news'
 *   $title        — page title
 */

$news          = $news          ?? [];
$branches      = $branches      ?? [];
$currentBranch = $currentBranch ?? 'all';
$pagination    = $pagination    ?? ['current' => 1, 'total' => 1, 'last' => 1, 'prev' => null, 'next' => null];
$basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind';
include __DIR__ . '/../layouts/header.php';

?>
<!-- ============================================================
     Page Banner
     ============================================================ -->
<section class="page-banner" aria-label="Page heading">
    <div class="container">
        <h1><?= __('news.title') ?></h1>
        <p><?= __('news.subtitle') ?></p>
        <nav class="breadcrumb" aria-label="Breadcrumb">
            <a href="<?= $basePath ?>/"><?= __('news.breadcrumb_home') ?></a>
            <span aria-hidden="true">/</span>
            <span><?= __('news.breadcrumb_news') ?></span>
        </nav>
    </div>
</section>

<!-- ============================================================
     News Listing Section
     ============================================================ -->
<section class="news-listing-section section-offwhite" aria-label="Branch news">
    <div class="container">
        <!-- Branch Filter -->
        <?php if (!empty($branches)): ?>
        <div class="news-filter-bar">
            <form method="GET" action="<?= $basePath ?>/news" class="news-filter-form">
                <label for="branch_filter"><?= __('news.filter_by_branch') ?>:</label>
                <select id="branch_filter" name="branch" onchange="this.form.submit()">
                    <option value="all" <?= $currentBranch === 'all' ? 'selected' : '' ?>><?= __('news.all_branches') ?></option>
                    <?php foreach ($branches as $branch): ?>
                    <option value="<?= htmlspecialchars($branch['id'] ?? '', ENT_QUOTES, 'UTF-8') ?>" <?= $currentBranch == ($branch['id'] ?? '') ? 'selected' : '' ?>>
                        <?= htmlspecialchars($branch['city'] ?? $branch['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </form>
        </div>
        <?php endif; ?>

        <?php if (!empty($news)): ?>
        <div class="news-grid news-grid-full">
            <?php foreach ($news as $item): ?>
            <article class="news-card">
                <div class="news-card-image">
                    <img
                        src="<?= htmlspecialchars(!empty($item['image']) ? upload_url($item['image']) : $basePath . '/images/news-default.jpg', ENT_QUOTES, 'UTF-8') ?>"
                        alt="<?= htmlspecialchars(getBilingual($item, 'title') ?: 'News', ENT_QUOTES, 'UTF-8') ?>"
                        loading="lazy"
                    >
                </div>
                <div class="news-card-body">
                    <div class="news-card-meta">
                        <span class="news-card-date">
                            <?= htmlspecialchars(date('M d, Y', strtotime($item['published_at'] ?? $item['created_at'] ?? 'now')), ENT_QUOTES, 'UTF-8') ?>
                        </span>
                        <?php if (!empty($item['branch_name'])): ?>
                        <span class="news-card-branch-badge"><?= htmlspecialchars($item['branch_name'], ENT_QUOTES, 'UTF-8') ?></span>
                        <?php endif; ?>
                    </div>
                    <h2 class="news-card-title">
                        <a href="<?= $basePath ?>/news/<?= htmlspecialchars($item['slug'] ?? $item['id'] ?? '#', ENT_QUOTES, 'UTF-8') ?>">
                            <?= htmlspecialchars(getBilingual($item, 'title') ?: 'News Update', ENT_QUOTES, 'UTF-8') ?>
                        </a>
                    </h2>
                    <p class="news-card-excerpt">
                        <?= htmlspecialchars(mb_substr(strip_tags(getBilingual($item, 'content') ?: ($item['content'] ?? '')), 0, 180), ENT_QUOTES, 'UTF-8') ?><?= mb_strlen(strip_tags(getBilingual($item, 'content') ?: ($item['content'] ?? ''))) > 180 ? '...' : '' ?>
                    </p>
                    <a href="<?= $basePath ?>/news/<?= htmlspecialchars($item['slug'] ?? $item['id'] ?? '#', ENT_QUOTES, 'UTF-8') ?>" class="news-card-link">
                        <?= __('news.read_more') ?>
                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>
                    </a>
                </div>
            </article>
            <?php endforeach; ?>
        </div>

        <!-- Pagination -->
        <?php if (($pagination['last'] ?? 1) > 1): ?>
        <nav class="pagination" aria-label="News pagination">
            <?php if ($pagination['prev'] ?? null): ?>
            <a href="<?= $basePath ?>/news?page=<?= (int) $pagination['prev'] ?><?= $currentBranch !== 'all' ? '&branch=' . htmlspecialchars($currentBranch, ENT_QUOTES, 'UTF-8') : '' ?>" class="pagination-btn" aria-label="Previous page">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="15 18 9 12 15 6"/></svg>
                <?= __('courses.prev') ?>
            </a>
            <?php endif; ?>

            <?php for ($i = 1; $i <= $pagination['last']; $i++): ?>
            <?php if ($i === (int) $pagination['current']): ?>
            <span class="pagination-btn active" aria-current="page"><?= $i ?></span>
            <?php else: ?>
            <a href="<?= $basePath ?>/news?page=<?= $i ?><?= $currentBranch !== 'all' ? '&branch=' . htmlspecialchars($currentBranch, ENT_QUOTES, 'UTF-8') : '' ?>" class="pagination-btn"><?= $i ?></a>
            <?php endif; ?>
            <?php endfor; ?>

            <?php if ($pagination['next'] ?? null): ?>
            <a href="<?= $basePath ?>/news?page=<?= (int) $pagination['next'] ?><?= $currentBranch !== 'all' ? '&branch=' . htmlspecialchars($currentBranch, ENT_QUOTES, 'UTF-8') : '' ?>" class="pagination-btn" aria-label="Next page">
                <?= __('courses.next') ?>
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="9 18 15 12 9 6"/></svg>
            </a>
            <?php endif; ?>
        </nav>
        <?php endif; ?>

        <?php else: ?>
        <div class="empty-state">
            <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 19.5A2.5 2.5 0 016.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 014 19.5v-15A2.5 2.5 0 016.5 2z"/></svg>
            <h3><?= __('news.no_news') ?></h3>
            <p><?= __('news.no_news_desc') ?></p>
        </div>
        <?php endif; ?>
    </div>
</section>

<?php include __DIR__ . '/../layouts/footer.php'; ?>
