<?php
require_once 'config.php';

$selected_category = isset($_GET['category']) ? trim($_GET['category']) : '';
$categories = getAllNewsCategories($pdo);

if($selected_category && $selected_category !== '') {
    $stmt = $pdo->prepare("SELECT * FROM news WHERE is_active = 1 AND category = ? ORDER BY created_at DESC");
    $stmt->execute([$selected_category]);
} else {
    $stmt = $pdo->prepare("SELECT * FROM news WHERE is_active = 1 ORDER BY created_at DESC");
    $stmt->execute();
}
$news = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Новости учебного центра</title>
    <meta name="description" content="Актуальные новости, объявления, мероприятия учебного центра. Фильтр по категориям новостей.">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', Arial, sans-serif; background: #f0f9ff; line-height: 1.6; }
        header, footer { background: #0f172a; color: #f8fafc; padding: 1rem; text-align: center; }
        nav a { color: #f8fafc; margin: 0 15px; text-decoration: none; transition: 0.3s; }
        nav a:hover { color: #3b82f6; }
        main { max-width: 1300px; margin: 30px auto; padding: 0 20px; min-height: 60vh; }
        h1 { text-align: center; color: #0f172a; margin-bottom: 10px; }
        .subtitle { text-align: center; color: #475569; margin-bottom: 30px; }
        
        .filter-section { background: #fff; padding: 20px; border-radius: 16px; margin-bottom: 30px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
        .filter-title { font-size: 1.1rem; font-weight: bold; margin-bottom: 15px; color: #0f172a; }
        .filter-buttons { display: flex; flex-wrap: wrap; gap: 10px; }
        .filter-btn { background: #e2e8f0; color: #0f172a; border: none; padding: 8px 20px; border-radius: 30px; cursor: pointer; transition: 0.3s; text-decoration: none; display: inline-block; font-size: 0.9rem; }
        .filter-btn:hover { background: #cbd5e1; }
        .filter-btn.active { background: #2563eb; color: #fff; }
        .reset-btn { background: #64748b; color: #fff; }
        
        .news-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 25px; }
        .news-card { background: #fff; border-radius: 20px; overflow: hidden; box-shadow: 0 5px 20px rgba(0,0,0,0.08); transition: transform 0.3s; }
        .news-card:hover { transform: translateY(-5px); }
        .news-card img { width: 100%; height: 200px; object-fit: cover; }
        .news-info { padding: 20px; }
        .news-title { font-size: 1.2rem; font-weight: bold; color: #0f172a; margin-bottom: 8px; }
        .category-badge { display: inline-block; background: #e2e8f0; color: #475569; padding: 4px 12px; border-radius: 20px; font-size: 0.7rem; margin-bottom: 10px; }
        .news-date { color: #64748b; font-size: 0.8rem; margin-bottom: 12px; }
        .news-content { color: #475569; font-size: 0.9rem; line-height: 1.5; }
        .btn { display: inline-block; background: #2563eb; color: #fff; padding: 8px 20px; text-decoration: none; border-radius: 30px; transition: 0.3s; font-weight: bold; margin-top: 15px; }
        .btn:hover { background: #1d4ed8; }
        footer { margin-top: 50px; }
        .empty-message { text-align: center; padding: 50px; background: #fff; border-radius: 16px; }
        @media (max-width: 768px) { .news-grid { grid-template-columns: 1fr; } .filter-buttons { justify-content: center; } }
    </style>
</head>
<body>
<header>
    <div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap;">
        <h1 style="margin: 0;">🎓 Учебный центр "Знания"</h1>
        <nav>
            <a href="index.php">📰 Новости</a>
            <a href="schedule.php">📅 Расписание</a>
            <?php if(isLoggedIn()): ?>
                <a href="logout.php">🚪 Выйти (<?= h($_SESSION['username']) ?>)</a>
            <?php else: ?>
                <a href="login.php">🔐 Вход</a>
                <a href="register.php">📝 Регистрация</a>
            <?php endif; ?>
        </nav>
    </div>
</header>

<main>
    <h1>📰 Новости учебного центра</h1>
    <p class="subtitle">Будьте в курсе всех событий</p>
    
    <div class="filter-section">
        <div class="filter-title">🏷️ Фильтр по категории</div>
        <div class="filter-buttons">
            <a href="index.php" class="filter-btn <?= empty($selected_category) ? 'active' : '' ?>">Все новости</a>
            <?php foreach($categories as $cat): ?>
                <a href="?category=<?= urlencode($cat) ?>" class="filter-btn <?= $selected_category == $cat ? 'active' : '' ?>">
                    <?= h($cat) ?>
                </a>
            <?php endforeach; ?>
        </div>
    </div>
    
    <?php if(count($news) > 0): ?>
        <div class="news-grid">
            <?php foreach($news as $item): ?>
                <div class="news-card">
                    <img src="<?= h($item['image_url'] ?: 'img/placeholder.jpg') ?>" alt="<?= h($item['title']) ?>">
                    <div class="news-info">
                        <div class="news-title"><?= h($item['title']) ?></div>
                        <div><span class="category-badge">📁 <?= h($item['category']) ?></span></div>
                        <div class="news-date">📅 <?= date('d.m.Y H:i', strtotime($item['created_at'])) ?></div>
                        <div class="news-content"><?= h(mb_substr($item['content'], 0, 120)) ?>...</div>
                        <a href="news_detail.php?id=<?= $item['id'] ?>" class="btn">📖 Читать далее</a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="empty-message">
            <p>😕 Новостей в выбранной категории не найдено.</p>
            <a href="index.php" class="filter-btn active">Показать все новости</a>
        </div>
    <?php endif; ?>
</main>

<footer>
    <p>&copy; 2025 Учебный центр "Знания". Качественное образование.</p>
    <p>📍 Москва, ул. Образовательная, 15 | 📞 8-800-555-35-35 | ✉️ info@znania.ru</p>
</footer>
</body>
</html>