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

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

// Login dulu untuk mendapatkan token
echo "<h3>1. Login untuk mendapatkan token</h3>";
$login_result = api_login('Widia', 'qwerty5*');
echo "Login Result: " . json_encode($login_result, JSON_PRETTY_PRINT) . "<br><br>";

if ($login_result['success'] && isset($login_result['data']['token'])) {
    $token = $login_result['data']['token'];
    echo "<strong style='color: green;'>Login berhasil! Token: " . substr($token, 0, 20) . "...</strong><br><br>";

    // Simpan token di session untuk testing
    $_SESSION['token'] = $token;
    $_SESSION['user_data'] = $login_result['data'];

    // Test semua endpoint yang digunakan di dashboard
    $endpoints = [
        'profil' => 'api_get_profil',
        'presensi_today' => 'api_get_presensi_today',
        'presensi' => 'api_get_presensi',
        'berita' => 'api_get_berita',
        'libur' => 'api_get_libur',
        'cuti' => 'api_get_cuti',
        'lembur' => 'api_get_lembur'
    ];

    echo "<h3>2. Test Data untuk Dashboard</h3>";
    foreach ($endpoints as $name => $function) {
        echo "<h4>$name</h4>";
        $result = $function($token);
        echo "Success: " . ($result['success'] ? 'YES' : 'NO') . "<br>";
        echo "HTTP Code: " . $result['http_code'] . "<br>";

        if ($result['success'] && !empty($result['data'])) {
            echo "Data Count: " . (is_array($result['data']) ? count($result['data']) : 'N/A') . "<br>";
            echo "Sample Data: " . json_encode(array_slice($result['data'], 0, 2), JSON_PRETTY_PRINT) . "<br>";
        } else {
            echo "Error: " . json_encode($result['data'], JSON_PRETTY_PRINT) . "<br>";
        }
        echo "<br>";
    }

    echo "<h3>3. Test Dashboard URL</h3>";
    echo "<a href='dashboard.php' target='_blank'>Buka Dashboard</a><br><br>";
} else {
    echo "<strong style='color: red;'>Login gagal!</strong><br>";
    echo "Error: " . json_encode($login_result, JSON_PRETTY_PRINT) . "<br>";
}
