<?php
require_once 'includes/config.php';

// ตรวจสอบ id
if (!isset($_GET['id'])) {
    header("Location: news.php");
    exit;
}

$id = intval($_GET['id']);

// ดึงข้อมูลข่าว
$stmt = $conn->prepare(
    "SELECT * FROM news WHERE id = ? AND status = 'published'"
);
$stmt->bind_param("i", $id);
$stmt->execute();

$result = $stmt->get_result();

if ($result->num_rows == 0) {

    $stmt->close();

    header("Location: news.php");
    exit;

}

$news = $result->fetch_assoc();
$stmt->close();

include 'includes/header.php';
?>

<div class="container mt-5">

    <div class="mb-4">

        <h2 class="fw-bold">

            <?= htmlspecialchars($news['title']); ?>

        </h2>

        <p class="text-muted">

            <i class="fa-solid fa-calendar-days"></i>
            <?= date("d/m/Y", strtotime($news['created_at'])); ?>

        </p>

    </div>

    <?php if (!empty($news['image'])): ?>

        <div class="text-center mb-4">

            <img
                src="uploads/news/<?= htmlspecialchars($news['image']); ?>"
                class="img-fluid rounded shadow"
                style="max-width:900px; width:100%;"
                alt="<?= htmlspecialchars($news['title']); ?>">

        </div>

    <?php endif; ?>

    <div class="card shadow">

        <div class="card-body">

            <div style="font-size:18px; line-height:1.8;">

                <?= nl2br(htmlspecialchars($news['detail'])); ?>

            </div>

        </div>

    </div>

    <div class="mt-4 mb-5">

        <a
            href="news.php"
            class="btn btn-secondary">

            ← กลับหน้าข่าวประชาสัมพันธ์

        </a>

    </div>

</div>

<?php
$conn->close();
include 'includes/footer.php';
?>
