<?php
/**
 * Dynamic News/Result/Post Details Page
 */
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/db.php';

$db = DB();
$slug = $_GET['slug'] ?? '';

if (empty($slug)) {
    header('Location: index.php');
    exit;
}

$post = $db->fetchOne("SELECT * FROM news_posts WHERE slug = ? AND is_active = 1", [$slug]);

if (!$post) {
    header('HTTP/1.0 404 Not Found');
    echo "<h1>404 Not Found</h1>";
    echo "The page you are looking for does not exist.";
    exit;
}
$pageTitle = htmlspecialchars($post['title']) . ' | IBVTE News';
$cleanContent = strip_tags($post['content']);
$pageDesc  = htmlspecialchars(mb_strimwidth($cleanContent, 0, 160, "..."));
include __DIR__ . '/includes/header.php';
?>

<div class="container py-5" style="min-height: 70vh;">
    <div class="row justify-content-center">
        <div class="col-lg-9">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                    <li class="breadcrumb-item text-capitalize active" aria-current="page"><?= $post['post_type'] ?></li>
                </ol>
            </nav>

            <div class="card shadow-sm border-0" style="border-radius: 15px; overflow: hidden;">
                <div class="card-body p-5">
                    <span class="badge bg-primary mb-3 text-uppercase"><?= $post['post_type'] ?></span>
                    <h1 class="fw-bold mb-3" style="color: #3b0b7c;"><?= htmlspecialchars($post['title']) ?></h1>
                    <div class="text-muted mb-4 pb-4 border-bottom">
                        <i class="far fa-calendar-alt me-2"></i> Published on <?= date('d F, Y', strtotime($post['created_at'])) ?>
                    </div>
                    
                    <div class="post-content">
                        <?= $post['content'] ?>
                    </div>

                    <?php if (!empty($post['featured_image'])): ?>
                        <?php 
                        $ext = strtolower(pathinfo($post['featured_image'], PATHINFO_EXTENSION));
                        $fileUrl = 'assets/uploads/news/' . $post['featured_image'];
                        if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])): 
                        ?>
                            <div class="mt-4 text-center">
                                <img src="<?= $fileUrl ?>" class="img-fluid rounded" style="max-height: 500px;" alt="Attachment">
                            </div>
                        <?php elseif ($ext === 'pdf'): ?>
                            <div class="mt-5 text-center bg-light p-4 rounded border">
                                <i class="fas fa-file-pdf fa-3x text-danger mb-3"></i>
                                <h5>Attached PDF Document</h5>
                                <a href="<?= $fileUrl ?>" target="_blank" class="btn btn-primary mt-2">
                                    <i class="fas fa-download me-2"></i> Download / View PDF
                                </a>
                            </div>
                        <?php else: ?>
                            <div class="mt-4">
                                <a href="<?= $fileUrl ?>" target="_blank" class="btn btn-outline-secondary">
                                    <i class="fas fa-paperclip me-2"></i> View Attachment
                                </a>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="mt-4 text-center">
                <a href="index.php" class="btn btn-outline-primary rounded-pill px-4">
                    <i class="fas fa-arrow-left me-2"></i> Back to Home
                </a>
            </div>
        </div>
    </div>
</div>

<style>
    .post-content img {
        max-width: 100%;
        height: auto;
        border-radius: 8px;
        margin: 15px 0;
    }
    .post-content table {
        width: 100% !important;
        max-width: 100%;
        margin-bottom: 1rem;
        background-color: transparent;
        border-collapse: collapse;
    }
    .post-content table th,
    .post-content table td {
        padding: 0.75rem;
        vertical-align: top;
        border-top: 1px solid #dee2e6;
    }
</style>

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