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

echo "<h2>Debug Berita/Pengumuman</h2>";

// Login dulu
$login_result = api_login('Widia', 'qwerty5*');
if ($login_result['success']) {
    $token = $login_result['data']['token'];
    $_SESSION['token'] = $token;
    $_SESSION['user_data'] = $login_result['data'];

    // Get berita
    $berita_result = api_get_berita($token);
    echo "<h3>Berita API Response:</h3>";
    echo "<pre>" . json_encode($berita_result, JSON_PRETTY_PRINT) . "</pre>";

    if ($berita_result['success']) {
        $berita_data = $berita_result['data'];
        echo "<h3>Berita Data Count: " . count($berita_data) . "</h3>";

        if (!empty($berita_data)) {
            echo "<h3>Sample Berita:</h3>";
            foreach (array_slice($berita_data, 0, 3) as $index => $berita) {
                echo "<div style='border: 1px solid #ccc; padding: 10px; margin: 10px 0;'>";
                echo "<strong>Berita #" . (intval($index) + 1) . ":</strong><br>";
                echo "ID: " . ($berita['id_berita'] ?? 'N/A') . "<br>";
                echo "Tanggal: " . ($berita['tanggal'] ?? 'N/A') . "<br>";
                echo "Judul: " . ($berita['judul'] ?? 'N/A') . "<br>";
                echo "Isi: " . ($berita['isi'] ?? 'N/A') . "<br>";
                echo "Photo: " . ($berita['photo'] ?? 'N/A') . "<br>";
                echo "</div>";
            }
        } else {
            echo "<p style='color: red;'>Berita data kosong!</p>";
        }
    } else {
        echo "<p style='color: red;'>API berita gagal: " . json_encode($berita_result['data']) . "</p>";
    }
} else {
    echo "Login gagal: " . json_encode($login_result['data']);
}
