<?php
include('auth.php'); // Sertakan autentikasi
$newsFiles = glob('../data/news/*.json');
// Tampilkan nama pengguna
$username = $_SESSION['username'] ?? 'Cobela'; // Gunakan 'Guest' sebagai default jika tidak ada username
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dashboard - KoeasNews</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body class="bg-dark">
<div class="container py-4">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card mt-4 border-0 shadow rounded-4 p-3">
<div class="card-body">
<h4 class="card-title"><a class="text-decoration-none text-dark" href="dashboard.php"> Dashboard CMS</a> - News Management <span class="small float-end"><small>Halo <?php echo htmlspecialchars($username); ?></small>&nbsp;<a href="logout.php" class="btn btn-danger btn-sm">Logout</a></span></h4>
<a class="btn shadow btn-sm mb-3 mt-3" href="add_news.php" role="button">Create News</a> &nbsp; <a class="btn shadow btn-sm mb-3 mt-3" href="add_kategori.php" role="button">Add Categori</a> &nbsp; <a class="btn shadow btn-sm mb-3 mt-3" href="settings.php" role="button">Settings</a>
<table class="table table-bordered mt-3">
<thead>
<tr>
<th scope="col">News</th>
<th scope="col">Date</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ($newsFiles as $file): ?>
<?php $news = json_decode(file_get_contents($file), true); ?>
<tr>
<td><?php echo $news['judul']; ?></td>
<td class="small"><?php echo $news['tanggal']; ?></td>
<td class="text-center">
    <a class="text-decoration-none text-danger" href="edit_news.php?slug=<?php echo urlencode(pathinfo($file, PATHINFO_FILENAME)); ?>">Edit</a>
</td>
<td class="text-center"><a class="text-decoration-none text-danger" href="delete_news.php?file=<?php echo basename($file); ?>" onclick="return confirm('Yakin ingin menghapus berita ini?');">Delete</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
