<?php
include 'koneksi.php';

// Get the news ID from URL parameter
$id_post = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($id_post <= 0) {
    echo "<script>alert('ID berita tidak valid'); window.location.href='berita_sekolah.php';</script>";
    exit;
}

// Fetch news details from database
$query = "SELECT * FROM tb_post WHERE id_post = $id_post AND kategori = 'berita'";
$result = mysqli_query($host, $query);

if (!$result || mysqli_num_rows($result) == 0) {
    echo "<script>alert('Berita tidak ditemukan'); window.location.href='berita_sekolah.php';</script>";
    exit;
}

$row = mysqli_fetch_assoc($result);
$judul = htmlspecialchars($row['judul_post']);
$deskripsi = htmlspecialchars($row['deskripsi']);
$tanggal = date('d M Y', strtotime($row['tanggal']));
$jenis = $row['jenis'];
$gambar = htmlspecialchars($row['gambar']);
$iframe = $row['iframe'];
?>
<!DOCTYPE html>
<html lang="en">
<head><link rel="icon" href="upload/bn2.png" type="image/png">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $judul; ?> | SMK Budiniah 2</title>
    <link rel="stylesheet" href="style.css">
    <style>
        body {
            font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
        body a{
            cursor: pointer;
        }
        .navigasi {
            margin-bottom: 20px;
        }
        .detail-container {
            max-width: 800px;
            margin: 20px auto;
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .detail-title {
            font-size: 32px;
            color: #333;
            margin-bottom: 10px;
            text-transform: capitalize;
            font-weight: bold;
        }
        .detail-date {
            color: #666;
            font-size: 14px;
            margin-bottom: 20px;
        }
        .detail-image {
            width: 100%;
            max-height: 400px;
            object-fit: cover;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .detail-description {
            font-size: 18px;
            line-height: 1.6;
            color: #555;
            text-align: justify;
        }
        .video-container {
            position: relative;
            padding-bottom: 56.25%;
            height: 0;
            overflow: hidden;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .video-container iframe {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        .back-button {
            display: inline-block;
            margin-top: 30px;
            padding: 10px 20px;
            background: linear-gradient(135deg, #0f62fe, #3f3ea5ff);
            color: white;
            text-decoration: none;
            border-radius: 5px;
            transition: all 0.3s ease;
        }
        .back-button:hover {
            background: linear-gradient(135deg, #3f3ea5ff, #0f62fe);
            transform: translateY(-2px);
        }
    </style>
</head>
<body>
    <div class="navigasi">
        <?php require "navbar.php"; ?>
    </div>

    <div class="detail-container">
        <h1 class="detail-title"><?php echo $judul; ?></h1>
        <div class="detail-date"><?php echo $tanggal; ?></div>

        <?php if ($jenis === 'foto' && !empty($gambar)): ?>
            <img src="upload/<?php echo $gambar; ?>" alt="<?php echo $judul; ?>" class="detail-image">
        <?php elseif ($jenis === 'video' && !empty($iframe)): ?>
            <div class="video-container">
                <?php echo $iframe; ?>
            </div>
        <?php endif; ?>

        <div class="detail-description"><?php echo nl2br($deskripsi); ?></div>

        <a href="berita_sekolah.php" class="back-button">← Kembali ke Berita</a>
    </div>
</body>
</html>

