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

echo "<h2>Debug Data Dashboard</h2>";

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

    echo "✅ Login berhasil<br><br>";

    // Get profile data
    $profile_result = api_get_profil($token);
    $profile_data = $profile_result['success'] ? $profile_result['data'] : [];

    echo "<h3>Profile Data:</h3>";
    echo "Success: " . ($profile_result['success'] ? 'YES' : 'NO') . "<br>";
    echo "Raw Data: <pre>" . print_r($profile_data, true) . "</pre>";

    if ($profile_result['success']) {
        $pegawai = $profile_data['pegawai'] ?? [];
        echo "<h4>Pegawai Data:</h4>";
        echo "<pre>" . print_r($pegawai, true) . "</pre>";

        echo "<h4>Extracted Values:</h4>";
        echo "Nama: " . ($pegawai['nama_lengkap'] ?? 'N/A') . "<br>";
        echo "Jabatan: " . ($pegawai['jabatan']['nama_jabatan'] ?? 'N/A') . "<br>";
        echo "Kantor: " . ($pegawai['kantor']['nama_kantor'] ?? 'N/A') . "<br>";
        echo "Unit Kerja: " . ($pegawai['unit_kerja']['nama_unit_kerja'] ?? 'N/A') . "<br>";
        echo "Alamat Kantor: " . ($pegawai['kantor']['alamat_kantor'] ?? 'N/A') . "<br>";
    }

    echo "<hr>";

    // Get berita data
    $berita_result = api_get_berita($token);
    $berita_data = $berita_result['success'] ? $berita_result['data'] : [];

    echo "<h3>Berita Data:</h3>";
    echo "Success: " . ($berita_result['success'] ? 'YES' : 'NO') . "<br>";
    echo "HTTP Code: " . $berita_result['http_code'] . "<br>";
    echo "Raw Data: <pre>" . print_r($berita_data, true) . "</pre>";

    if ($berita_result['success'] && !empty($berita_data)) {
        echo "<h4>Berita Items:</h4>";
        $berita_items = array_values($berita_data);
        foreach (array_slice($berita_items, 0, 2) as $index => $berita) {
            echo "<div style='border: 1px solid #ccc; padding: 10px; margin: 5px 0;'>";
            echo "<strong>Berita " . ($index + 1) . ":</strong><br>";
            echo "Judul: " . ($berita['judul'] ?? 'N/A') . "<br>";
            echo "Tanggal: " . ($berita['tanggal'] ?? 'N/A') . "<br>";
            echo "Isi: " . substr(strip_tags($berita['isi'] ?? 'N/A'), 0, 100) . "...<br>";
            echo "</div>";
        }
    }
} else {
    echo "❌ Login gagal: " . json_encode($login_result['data']);
}
