<?php
require_once 'config/db.php';
$settings = getAllSettings($pdo);

$slug = $_GET['slug'] ?? '';
if (!$slug) { header('Location: news.php'); exit; }

$stmt = $pdo->prepare("SELECT * FROM news WHERE slug=? AND is_active=1 LIMIT 1");
$stmt->execute([$slug]);
$news = $stmt->fetch();
if (!$news) { header('Location: news.php'); exit; }

$related = $pdo->prepare("SELECT * FROM news WHERE is_active=1 AND id!=? AND category=? ORDER BY created_at DESC LIMIT 3");
$related->execute([$news['id'], $news['category']]);
$relatedPosts = $related->fetchAll();

$activePage = 'news';
$pageTitle = htmlspecialchars($news['title']).' - '.($settings['site_name']??'');

include 'partials/header.php';
?>

<div class="page-hero">
    <div class="container">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-2">
                <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                <li class="breadcrumb-item"><a href="news.php" data-en="News" data-id="Berita">Berita</a></li>
                <li class="breadcrumb-item active"><?= htmlspecialchars(substr($news['title'],0,30)).'...' ?></li>
            </ol>
        </nav>
        <div class="d-flex align-items-center gap-2 flex-wrap">
            <span class="badge" style="background:var(--accent);color:#000;"><?= $news['category']==='event'?'Kegiatan':'Berita' ?></span>
            <span style="color:rgba(255,255,255,.6);font-size:.9rem;"><i class="bi bi-calendar3 me-1"></i><?= date('d M Y', strtotime($news['created_at'])) ?></span>
        </div>
        <h1 class="fw-bold mt-2 fs-3"><?= htmlspecialchars($news['title']) ?></h1>
    </div>
</div>

<section class="py-5 py-lg-6">
    <div class="container">
        <div class="row g-5">
            <!-- Main Content -->
            <div class="col-lg-8 fade-up">
                <?php if ($news['image']): ?>
                    <img src="<?= htmlspecialchars($news['image']) ?>" alt="<?= htmlspecialchars($news['title']) ?>"
                         class="img-fluid rounded-3 mb-4" style="width:100%;max-height:500px;object-fit:cover;"
                         onerror="this.src='https://placehold.co/900x450/e0e0e0/888?text=News'">
                <?php endif; ?>

                <?php if ($news['excerpt']): ?>
                    <p class="lead fw-semibold text-muted mb-4"><?= htmlspecialchars($news['excerpt']) ?></p>
                <?php endif; ?>

                <div class="news-content">
                    <?= $news['content'] ?>
                </div>

                <!-- Share Buttons -->
                <div class="share-box mt-5 pt-4 border-top">
                    <span class="fw-semibold me-3" data-en="Share:" data-id="Bagikan:">Bagikan:</span>
                    <a href="https://wa.me/?text=<?= urlencode($news['title'].' '.BASE_URL.'/news-detail.php?slug='.$news['slug']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary me-2"><i class="bi bi-whatsapp"></i></a>
                    <a href="https://www.facebook.com/sharer/sharer.php?u=<?= urlencode(BASE_URL.'/news-detail.php?slug='.$news['slug']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary me-2"><i class="bi bi-facebook"></i></a>
                    <a href="https://twitter.com/intent/tweet?text=<?= urlencode($news['title']) ?>&url=<?= urlencode(BASE_URL.'/news-detail.php?slug='.$news['slug']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary"><i class="bi bi-twitter-x"></i></a>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="col-lg-4 fade-up">
                <?php if (!empty($relatedPosts)): ?>
                <div class="sidebar-box mb-4">
                    <h6 class="sidebar-title" data-en="Related Posts" data-id="Artikel Terkait">Artikel Terkait</h6>
                    <?php foreach ($relatedPosts as $rp): ?>
                    <a href="news-detail.php?slug=<?= urlencode($rp['slug']) ?>" class="sidebar-news-item text-decoration-none">
                        <img src="<?= htmlspecialchars($rp['image']??'') ?>" alt="<?= htmlspecialchars($rp['title']) ?>"
                             onerror="this.src='https://placehold.co/80x60/e0e0e0/888?text=News'">
                        <div>
                            <div class="sidebar-news-title"><?= htmlspecialchars($rp['title']) ?></div>
                            <small class="text-muted"><?= date('d M Y', strtotime($rp['created_at'])) ?></small>
                        </div>
                    </a>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>

                <!-- CTA Box -->
                <div class="sidebar-cta">
                    <h6 class="fw-bold mb-2" data-en="Need Our Services?" data-id="Butuh Layanan Kami?">Butuh Layanan Kami?</h6>
                    <p class="small text-muted mb-3" data-en="Contact us for a free consultation and quotation." data-id="Hubungi kami untuk konsultasi dan penawaran gratis.">Hubungi kami untuk konsultasi dan penawaran gratis.</p>
                    <a href="contact.php" class="btn btn-dark btn-sm w-100" data-en="Contact Us" data-id="Hubungi Kami">Hubungi Kami</a>
                </div>
            </div>
        </div>
    </div>
</section>

<?php include 'partials/footer.php'; ?>
