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

// Check if user is logged in
if (!isset($_SESSION['token'])) {
    header('Location: login.php');
    exit;
}

$token = $_SESSION['token'];

// Get berita data
$berita_result = api_get_berita($token);

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

if ($berita_result['success']) {
    $berita_data = $berita_result['data'];
    $berita_items = $berita_data['data'] ?? $berita_data;

    if (is_array($berita_items)) {
        echo "<h3>Available Berita Items:</h3>";
        echo "<table border='1' cellpadding='5' cellspacing='0'>";
        echo "<tr><th>Index</th><th>ID</th><th>Judul</th><th>Tanggal</th><th>Link</th></tr>";

        foreach ($berita_items as $index => $item) {
            $item_id = $item['id'] ?? 'NO_ID';
            $item_judul = $item['judul'] ?? 'NO_TITLE';
            $item_tanggal = $item['tanggal'] ?? 'NO_DATE';
            $link = "detail-info.php?id=" . urlencode($item_id);

            echo "<tr>";
            echo "<td>$index</td>";
            echo "<td>$item_id</td>";
            echo "<td>" . htmlspecialchars($item_judul) . "</td>";
            echo "<td>" . htmlspecialchars($item_tanggal) . "</td>";
            echo "<td><a href='$link' target='_blank'>Test Link</a></td>";
            echo "</tr>";
        }

        echo "</table>";
    } else {
        echo "<p>Berita items is not an array</p>";
    }
} else {
    echo "<p style='color: red;'>API call failed: " . ($berita_result['data']['pesan'] ?? 'Unknown error') . "</p>";
}

echo "<p><a href='dashboard.php'>← Kembali ke Dashboard</a></p>";
