<?php include 'koneksi.php';
session_start();
if (!isset($_SESSION['login_admin'])) {
    header('Location: dashboard.php');
    exit;
}

// 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_admin.php';</script>";
    exit;
}

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

if (!$result || mysqli_num_rows($result) == 0) {
    echo "<script>alert('Berita tidak ditemukan'); window.location.href='berita_admin.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 Admin</title>
    <link rel="stylesheet" href="style_admin.css">
    <link rel="stylesheet" href="footer.css">
<style>
    *{
        margin: 0;
        padding: 0;
        scroll-behavior: smooth;
    }

    body{
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        background: #ffffff;
    }
    
    .berita-detail {
        width: 100%;
        background: #eef5ff;         
        padding: 60px 0 120px;          
        text-align: center;
    }

    .berita-detail h1 {
        font-size: 40px;
        font-weight: bold;
        color: #2a2a2a;
        text-transform: capitalize;
    }

    .detail-berita {
        margin: -80px auto 40px auto;    
        background: #fff;
        padding: 30px;
        border-radius: 15px;
        max-width: 900px;

        display: grid;
        gap: 20px;

        box-shadow: 0 8px 20px rgba(0,0,0,0.12);
        position: relative;
        z-index: 10;
    }

    .bagian-teks {
        background: #fff;
        border-radius: 10px;
        line-height: 1.6;
    }

    .bagian-teks h3 {
        margin-bottom: 15px;
        color: #333;
    }

    .bagian-teks p {
        margin-bottom: 15px;
        text-align: justify;
    }

    .bagian-teks .tanggal {
        font-size: 14px;
        color: #666;
        margin-bottom: 20px;
    }

    .bagian-media {
        text-align: center;
        margin-bottom: 20px;
    }

    .bagian-media img {
        max-width: 100%;
        height: auto;
        border-radius: 10px;
    }

    .video-container {
        position: relative;
        padding-bottom: 56.25%;
        height: 0;
        overflow: hidden;
        border-radius: 10px;
    }

    .video-container iframe {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    .button-kembali {
        text-align: center;
        margin-top: 30px;
    }

    .button-kembali a {
        display: inline-block;
        padding: 10px 20px;
        background: linear-gradient(135deg, #0037fd, #5b8cffff);
        color: #fff;
        text-decoration: none;
        border-radius: 5px;
        font-weight: bold;
        transition: all 0.3s ease;
    }

    .button-kembali a:hover {
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(0,55,253,0.4);
    }
    
    @media (max-width: 768px) {
        .detail-berita {
            grid-template-columns: 1fr;
            margin: -50px 20px 20px 20px;
            padding: 20px;
        }
    }
</style>
</head>
<body>

<div class="berita-detail">
    <h1><?php echo $judul; ?></h1>
</div>
<div class="detail-berita">
    <div class="bagian-teks">
        <div class="tanggal">Tanggal: <?php echo $tanggal; ?></div>

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

        <h3>Deskripsi Berita</h3>
        <p><?php echo nl2br($deskripsi); ?></p>

        <div class="button-kembali">
            <a href="berita_admin.php">← Kembali ke Berita Admin</a>
        </div>
    </div>
</div>
</body>
</html>

