<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login.php"); exit;
}

// Logika Hapus Berita
if (isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    $stmt = $pdo->prepare("SELECT gambar FROM berita WHERE id_berita = ?");
    $stmt->execute([$id]);
    $b = $stmt->fetch();
    if ($b['gambar']) unlink("../assets/img/berita/" . $b['gambar']);
    
    $pdo->prepare("DELETE FROM berita WHERE id_berita = ?")->execute([$id]);
    header("Location: manage_berita.php");
}

$berita = $pdo->query("SELECT * FROM berita ORDER BY tgl_posting DESC")->fetchAll();
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kelola Berita</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="bg-light">
    <?php include 'sidebar.php'; ?>

    <div class="main-content">
        <div class="container-fluid">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h4 class="fw-bold mb-0">Berita & Artikel</h4>
                <a href="berita_add.php" class="btn btn-primary shadow-sm"><i class="fa fa-plus me-1"></i> Tulis Berita</a>
            </div>

            <div class="row g-3">
                <?php if (empty($berita)): ?>
                    <div class="col-12">
                        <div class="alert alert-info border-0 shadow-sm">Belum ada berita. Silakan tambah berita baru.</div>
                    </div>
                <?php endif; ?>

                <?php foreach($berita as $b): ?>
                <div class="col-md-6 col-lg-4">
                    <div class="card border-0 shadow-sm rounded-4 overflow-hidden h-100">
                        <?php if($b['gambar']): ?>
                            <img src="../assets/img/berita/<?= $b['gambar'] ?>" class="card-img-top" style="height: 200px; object-fit: cover;">
                        <?php endif; ?>
                        <div class="card-body">
                            <span class="badge bg-soft-primary text-primary mb-2" style="background: #e7f1ff;"><?= $b['kategori'] ?></span>
                            <h6 class="fw-bold"><?= $b['judul'] ?></h6>
                            <p class="text-muted small mb-3"><?= substr(strip_tags($b['isi']), 0, 100) ?>...</p>
                            <div class="d-flex justify-content-between align-items-center mt-auto">
                                <small class="text-muted"><?= date('d M Y', strtotime($b['tgl_posting'])) ?></small>
                                <div>
                                    <a href="berita_edit.php?id=<?= $b['id_berita'] ?>" class="btn btn-sm btn-outline-warning border-0"><i class="fa fa-edit"></i></a>
                                    <a href="?hapus=<?= $b['id_berita'] ?>" class="btn btn-sm btn-outline-danger border-0" onclick="return confirm('Hapus berita ini?')"><i class="fa fa-trash"></i></a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</body>
</html>