<?php
// simple_debug.php
session_start();

// load config helper API
require_once __DIR__ . '/../config.php';

// --- ambil data berita dari API ---
$response = api_request('berita', $_SESSION['token'] ?? null);

// --- debug basic ---
echo "<h2>Berita Data:</h2>";

if (!$response) {
    echo "<p style='color:red'>Gagal ambil data dari API.</p>";
    exit;
}

// tampilkan info umum
echo "<pre>";
echo "Success: " . ($response['status'] == 1 ? 'YES' : 'NO') . "\n";
echo "HTTP Code: 200\n";
echo "Data count: " . (is_array($response['data'] ?? null) ? count($response['data']) : 0) . "\n";
echo "</pre>";

// --- tampilkan daftar berita ---
if (!empty($response['data']) && is_array($response['data'])) {
    echo "<h3>Daftar Berita:</h3>";

    foreach ($response['data'] as $item) {
        $judul = htmlspecialchars($item['judul'] ?? '-');
        $tanggal = htmlspecialchars($item['tanggal'] ?? '-');
        $isi = nl2br(htmlspecialchars(trim($item['isi'] ?? '')));
        $photoUrl = uploads_berita_url((string) ($item['photo'] ?? ''));

        echo "<div style='border:1px solid #ccc;padding:10px;margin-bottom:15px;border-radius:8px'>";
        echo "<h4 style='margin:0 0 5px 0;'>📅 $tanggal — $judul</h4>";
        if ($photoUrl !== '') {
            echo "<img src='" . htmlspecialchars($photoUrl) . "' alt='$judul' style='max-width:250px;border-radius:8px;display:block;margin:8px 0;'>";
        }
        echo "<p>$isi</p>";
        echo "</div>";
    }
} else {
    echo "<p>Tidak ada berita ditemukan.</p>";
}
