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

echo "<h2>Test Dashboard Variables</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 data seperti di dashboard
    $profile_result = api_get_profil($token);
    $profile_data = $profile_result['success'] ? $profile_result['data'] : [];

    $presensi_today_result = api_get_presensi_today($token);
    $presensi_today = $presensi_today_result['success'] ? $presensi_today_result['data'] : [];

    // Extract variables seperti di dashboard
    $pegawai = $profile_data['pegawai'] ?? [];
    $user_name = $pegawai['nama_lengkap'] ?? 'User';
    $user_jabatan = $pegawai['jabatan']['nama_jabatan'] ?? 'Karyawan';
    $user_id = $pegawai['nip'] ?? '';
    $user_photo = $pegawai['photo'] ?? '';
    $unit_kerja = $pegawai['unit_kerja']['nama_unit_kerja'] ?? 'Unit Kerja';
    $kantor = $pegawai['kantor']['nama_kantor'] ?? 'Kantor Pusat';

    $presensi_data_today = $presensi_today['data'] ?? [];
    $masuk_time = $presensi_data_today['jam_masuk'] ?? '00:00';
    $istirahat_mulai = $presensi_data_today['mulai_istirahat'] ?? '00:00';
    $istirahat_selesai = $presensi_data_today['beres_istirahat'] ?? '00:00';
    $pulang_time = $presensi_data_today['jam_pulang'] ?? '00:00';

    $jadwal = $pegawai['jadwal'] ?? [];
    $jam_masuk = $jadwal['masuk'] ?? '08:00';
    $jam_pulang = $jadwal['pulang'] ?? '17:00';
    $jam_istirahat = $jadwal['istirahat'] ?? '12:00';
    $toleransi_masuk = $jadwal['toleransi_masuk'] ?? '15';
    $toleransi_pulang = $jadwal['toleransi_pulang'] ?? '15';

    echo "<h3>Variables Check:</h3>";
    echo "user_name: " . ($user_name ?: 'EMPTY') . "<br>";
    echo "user_jabatan: " . ($user_jabatan ?: 'EMPTY') . "<br>";
    echo "user_id: " . ($user_id ?: 'EMPTY') . "<br>";
    echo "unit_kerja: " . ($unit_kerja ?: 'EMPTY') . "<br>";
    echo "kantor: " . ($kantor ?: 'EMPTY') . "<br>";
    echo "jam_masuk: " . ($jam_masuk ?: 'EMPTY') . "<br>";
    echo "jam_pulang: " . ($jam_pulang ?: 'EMPTY') . "<br>";
    echo "jam_istirahat: " . ($jam_istirahat ?: 'EMPTY') . "<br>";
    echo "toleransi_masuk: " . ($toleransi_masuk ?: 'EMPTY') . "<br>";
    echo "masuk_time: " . ($masuk_time ?: 'EMPTY') . "<br>";
    echo "pulang_time: " . ($pulang_time ?: 'EMPTY') . "<br>";

    echo "<h3>Raw Profile Data:</h3>";
    echo "<pre>" . json_encode($profile_data, JSON_PRETTY_PRINT) . "</pre>";

    echo "<h3>Raw Presensi Today Data:</h3>";
    echo "<pre>" . json_encode($presensi_today, JSON_PRETTY_PRINT) . "</pre>";
} else {
    echo "Login gagal: " . json_encode($login_result['data']);
}
