<?php
// Base URL untuk website Anda yang dibuat secara dinamis
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$host = $_SERVER['HTTP_HOST'];
$directory = dirname($_SERVER['SCRIPT_NAME']);
$base_url = $protocol . $host . $directory;

// Ambil pengaturan dari file settings.json
$settings = json_decode(file_get_contents(__DIR__ . '/data/settings.json'), true);
$siteDescription = $settings['site_description'] ?? 'Deskripsi default website';
$siteSlogan = $settings['site_slogan'] ?? 'Slogan default'; // Ambil slogan

$pageDir = __DIR__ . '/data/pages/';
$pageFiles = glob($pageDir . '*.json');

// Ambil slug dari query string
$slug = $_GET['slug'] ?? '';

// Cek apakah slug kosong
if (empty($slug)) {
    die('Halaman tidak ditemukan');
}

// Folder tempat menyimpan halaman
$pageDir = __DIR__ . '/data/pages/';

// Path file JSON halaman
$pageFile = $pageDir . $slug . '.json';

// Cek apakah file halaman ada
if (!file_exists($pageFile)) {
    die('Halaman tidak ditemukan.');
}

// Ambil data halaman dari file JSON
$pageData = json_decode(file_get_contents($pageFile), true);
if (json_last_error() !== JSON_ERROR_NONE) {
    die('Error decoding JSON: ' . json_last_error_msg());
}

// Tampilkan halaman
?>
<?php
// Folder tempat berita disimpan
$newsDir = __DIR__ . '/data/news/';

// Inisialisasi array untuk menyimpan kategori yang ada
$kategoriDenganArtikel = [];

// Baca semua file di folder berita
$newsFiles = glob($newsDir . '*.json');

// Loop melalui setiap file berita
foreach ($newsFiles as $file) {
    // Ambil data berita
    $newsData = json_decode(file_get_contents($file), true);

    // Pastikan data valid dan kategori ada
    if (isset($newsData['kategori']) && !empty($newsData['kategori'])) {
        // Tambahkan kategori ke dalam array jika belum ada
        if (!in_array($newsData['kategori'], $kategoriDenganArtikel)) {
            $kategoriDenganArtikel[] = $newsData['kategori'];
        }
    }
}
?>
<?php
$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
?>
<!DOCTYPE html4>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo htmlspecialchars($news['judul']); ?> | <?php echo htmlspecialchars($settings['site_name'] ?? 'Nama Website'); ?></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">
<script src="https://kit.fontawesome.com/312e1f4072.js" crossorigin="anonymous"></script>
<style>
    p a {
    color: #000;
    font-weight: bold;
    border-bottom: 1px solid #ccc; /* Warna abu-abu dan ketebalan border */
    text-decoration: none; /* Hilangkan garis bawah default */
}
p a:hover {
    color: #be1e2e;
    font-weight: bold;
    border-bottom: 1px solid #be1e2e; /* Warna abu-abu dan ketebalan border */
    text-decoration: none; /* Hilangkan garis bawah default */
}
</style>
</head>
<body class="bg-light">
<div class="container mt-3 mb-4">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card mt-4 mb-4 border-0 shadow rounded-4 p-3">
                <!-- menu -->
<nav class="navbar m-4 navbar-expand-lg shadow rounded-3 mb-3">
  <div class="container-fluid">
    <a class="navbar-brand fw-bold" href="<?php echo $base_url; ?>"><i class="fa-solid fa-rss text-danger"></i>&nbsp;<?php echo htmlspecialchars($settings['site_name'] ?? 'Nama Website'); ?></a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
    <li class="nav-item">
        <a class="nav-link active" aria-current="page" href="<?php echo $base_url; ?>">Home</a>
    </li>
    <?php foreach ($kategoriDenganArtikel as $kategori): ?>
    <li><a class="nav-link active" href="<?php echo $base_url; ?>/kategori.php?kategori=<?php echo urlencode($kategori); ?>"><?php echo htmlspecialchars($kategori); ?></a></li>
<?php endforeach; ?>

</ul>

    </div>
  </div>
</nav>
<!--/ menu -->
<div class="ps-4 pe-4">
<form class="d-flex" role="search" action="search.php" method="GET">
    <input type="text" name="query" placeholder="Cari artikel..." class="form-control me-2 rounded-3">
    <button type="submit" class="btn btn-dark rounded-3"><i class="fa-solid fa-magnifying-glass"></i></button>
</form>
</div>
                <div class="p-4">
        <h1 class="text-center ps-3 pe-3 pb-4"><?php echo htmlspecialchars($pageData['judul']); ?></h1>
        <div>
            <?php if (!empty($pageData['image'])): ?>
                <img src="<?php echo $base_url . '/' . htmlspecialchars($pageData['image']); ?>" alt="<?php echo htmlspecialchars($pageData['image_description']); ?>" class="img-fluid mb-3">
            <?php endif; ?>
        </div>
        <div>
            <?php echo $pageData['konten']; ?>
        </div>
    </div>
            </div>
            
            <div class="d-flex justify-content-center">
                <nav aria-label="breadcrumb">
  <ol class="breadcrumb">
                <?php
foreach ($pageFiles as $file) {
    $pageData = json_decode(file_get_contents($file), true);
    $slug = basename($file, '.json');
    echo '<li class="breadcrumb-item"><a class=" text-secondary small text-decoration-none" href="' . $slug . '">' . htmlspecialchars($pageData['judul']) . '</a></li>';
}
?>
</ol>
</nav>
            </div>
            <p class="small text-center text-secondary">&copy; 2024 <?php echo htmlspecialchars($settings['site_name'] ?? 'Nama Website'); ?>. All Rights Reserved</p>
         </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script type='text/javascript' src='https://platform-api.sharethis.com/js/sharethis.js#property=645f9517192408001992d19d&product=inline-share-buttons' async='async'></script>

</body>
</html>