<?php
require_once 'config.php';

$news_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if(!$news_id) {
    die("Новость не найдена");
}

$stmt = $pdo->prepare("SELECT * FROM news WHERE id = ? AND is_active = 1");
$stmt->execute([$news_id]);
$news = $stmt->fetch(PDO::FETCH_ASSOC);

if(!$news) {
    die("Новость не найдена");
}

// Увеличиваем счётчик просмотров
$updateStmt = $pdo->prepare("UPDATE news SET views = views + 1 WHERE id = ?");
$updateStmt->execute([$news_id]);
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title><?= h($news['title']) ?> - Учебный центр</title>
    <meta name="description" content="<?= h(mb_substr($news['content'], 0, 150)) ?>">
    <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: 1000px; margin: 30px auto; padding: 0 20px; min-height: 60vh; }
        .news-container { background: #fff; border-radius: 24px; padding: 35px; box-shadow: 0 10px 30px rgba(0,0,0,0.08); }
        .news-title { font-size: 2rem; font-weight: bold; color: #0f172a; margin-bottom: 15px; }
        .news-meta { display: flex; gap: 20px; margin-bottom: 25px; padding-bottom: 15px; border-bottom: 1px solid #e2e8f0; color: #64748b; }
        .news-category { background: #e2e8f0; color: #475569; padding: 4px 12px; border-radius: 20px; font-size: 0.8rem; }
        .news-image { width: 100%; max-height: 400px; object-fit: cover; border-radius: 16px; margin: 20px 0; }
        .news-content { font-size: 1.1rem; line-height: 1.8; color: #334155; }
        .btn-back { display: inline-block; background: #64748b; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 8px; margin-top: 30px; }
        @media (max-width: 768px) { .news-title { font-size: 1.5rem; } }
    </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>
    <a href="index.php" class="btn-back">← Назад к новостям</a>
    
    <div class="news-container">
        <div class="news-title"><?= h($news['title']) ?></div>
        <div class="news-meta">
            <span class="news-category">📁 <?= h($news['category']) ?></span>
            <span>📅 <?= date('d.m.Y H:i', strtotime($news['created_at'])) ?></span>
            <span>👁️ <?= $news['views'] ?> просмотров</span>
        </div>
        <img src="<?= h($news['image_url'] ?: 'img/placeholder.jpg') ?>" alt="<?= h($news['title']) ?>" class="news-image">
        <div class="news-content">
            <?= nl2br(h($news['content'])) ?>
        </div>
    </div>
</main>

<footer>
    <p>&copy; 2025 Учебный центр "Знания". Качественное образование.</p>
</footer>
</body>
</html>