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

function limit_words($text, $limit) {
    $words = explode(' ', $text);
    if (count($words) > $limit) {
        return implode(' ', array_slice($words, 0, $limit)) . '...';
    }
    return $text;
}
?>
<!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>Kelola Berita | SMK Budiniah 2</title>
    <link rel="stylesheet" href="style_admin.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            min-height: 100vh;
        }
        .container {
            max-width: 1100px;
            margin: 50px auto;
            padding: 30px;
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            position: relative;
        }
        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #0037fd, #5b8cffff);
            border-radius: 15px 15px 0 0;
        }
        h1 {
            text-align: center;
            color: #0037fd;
            margin-bottom: 30px;
            font-size: 28px;
            font-weight: bold;
        }
        .top-buttons {
            position: absolute;
            top: 20px;
            left: 30px;
            display: flex;
            gap: 10px;
        }
        .top-buttons a {
            background: linear-gradient(135deg, #0037fd, #5b8cffff);
            color: #fff;
            padding: 8px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        .top-buttons a:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,55,253,0.4);
        }
        .top-buttons button {
            background: linear-gradient(135deg, #0037fd, #5b8cffff);
            color: #fff;
            padding: 8px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        .top-buttons button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,55,253,0.4);
        }
        .btn {
            background: linear-gradient(135deg, #0037fd, #5b8cffff);
            color: #fff;
            padding: 8px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,55,253,0.4);
        }
        .news-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 60px;
        }
        .news-card {
            background: #fff;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            transition: transform 0.3s ease;
        }
        .news-card:hover {
            transform: translateY(-5px);
        }
        .news-card img {
            width: 100%;
            height: 200px;
            object-fit: cover;
        }
        .news-content {
            padding: 15px;
            text-transform: capitalize;
        }
        .news-content h3 {
            font-size: 18px;
            margin: 0 0 10px 0;
            color: #333;
        }
        .news-content p {
            font-size: 14px;
            color: #666;
            margin: 0 0 10px 0;
            line-height: 1.4;
        }
        .news-date {
            font-size: 12px;
            color: #999;
            margin-bottom: 10px;
        }
        .news-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .read-more {
            color: #0037fd;
            text-decoration: none;
            font-size: 14px;
            font-weight: bold;
        }
        .read-more:hover {
            color: #5b8cffff;
        }
        .edit-btn, .delete-btn {
            padding: 5px 10px;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-size: 12px;
            text-decoration: none;
            margin-left: 5px;
        }
        .edit-btn {
            background: #28a745;
            color: #fff;
        }
        .edit-btn:hover {
            background: #218838;
        }
        .delete-btn {
            background: #dc3545;
            color: #fff;
        }
        .delete-btn:hover {
            background: #c82333;
        }
        .no-news {
            text-align: center;
            color: #666;
            font-size: 18px;
            margin-top: 50px;
        }

        /* Mobile Responsiveness */
        @media (max-width: 768px) {
            .container {
                max-width: 500px;
                margin: 20px 20px 0 15%;
                padding: 20px 5%;
            }
            .news-grid {
                grid-template-columns: 1fr;
            }
            .top-buttons {
                position: static;
                justify-content: center;
                margin-bottom: 20px;
            }
        }
    </style>
</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <div class="container">
        <h1>Kelola Berita</h1>
        <div class="top-buttons">
            <a href="tambah_berita.php" class="btn">Tambah Berita</a>
            <button class="btn" onclick="toggleEditMode()">Edit Mode</button>
        </div>
        <div class="news-grid">
            <?php
            $query = "SELECT * FROM tb_post WHERE KATEGORI = 'berita'";
            $result = mysqli_query($host, $query);
            if ($result && mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    $imgSrc = !empty($row['gambar']) ? "upload/" . htmlspecialchars($row['gambar']) : "upload/default.jpg";
                    $judul = htmlspecialchars($row['judul_post']);
                    $deskripsi = htmlspecialchars($row['deskripsi']);
                    $tanggal = date('d M Y', strtotime($row['tanggal']));
                    $kategori = htmlspecialchars($row['kategori']);
                    echo '<div class="news-card">';
                    if ($row['jenis'] === 'foto' && !empty($row['gambar'])) {
                        echo '<img src="' . $imgSrc . '" alt="' . $judul . '" />';
                    } elseif ($row['jenis'] === 'video' && !empty($row['iframe'])) {
                        echo '<div class="video-container">' . $row['iframe'] . '</div>';
                    } else {
                        echo '<img src="upload/default.jpg" alt="No Image" />';
                    }
                    echo '<div class="news-content">';
                    echo '<h3>' . $judul . '</h3>';
                    echo '<p>' . limit_words($deskripsi, 15) . '</p>';
                    echo '<div class="news-date">' . $tanggal . ' - ' . $kategori . '</div>';
                    echo '<div class="news-actions">';
                    echo '<a href="detail_berita_admin.php?id=' . $row['id_post'] . '" class="read-more">Baca Selengkapnya</a>';
                    echo '<div class="action-buttons" style="display: none;">';
                    echo '<a href="edit_berita.php?id=' . $row['id_post'] . '" class="edit-btn">Edit</a>';
                    echo '<a href="hapus_berita.php?id=' . $row['id_post'] . '" class="delete-btn" onclick="return confirm(\'Apakah Anda yakin ingin menghapus berita ini?\')">Hapus</a>';
                    echo '</div>';
                    echo '</div>';
                    echo '</div>';
                    echo '</div>';
                }
            } else {
                echo '<div class="no-news">Belum ada berita tersedia.</div>';
            }
            ?>
        </div>
    </div>
    <script>
        function toggleEditMode() {
            const actionButtons = document.querySelectorAll('.action-buttons');
            actionButtons.forEach(btn => {
                btn.style.display = btn.style.display === 'none' ? 'block' : 'none';
            });
        }
    </script>
</body>
</html>

