<?php
require_once __DIR__ . '/../config.php';

echo "<h2>Test API dengan Token Valid</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>";

    // Test semua endpoint dengan token
    $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'
    ];

    foreach ($endpoints as $name => $function) {
        echo "<h4>2. Test $name</h4>";
        $result = $function($token);
        echo "Success: " . ($result['success'] ? 'YES' : 'NO') . "<br>";
        echo "HTTP Code: " . $result['http_code'] . "<br>";
        echo "Response: " . json_encode($result['data'], JSON_PRETTY_PRINT) . "<br><br>";
    }

    // Test save presensi (tanpa foto)
    echo "<h4>3. Test Save Presensi Masuk (tanpa foto)</h4>";
    $presensi_data = [
        'latitude' => '-6.9022',
        'longitude' => '107.6181',
        'waktu' => date('Y-m-d H:i:s'),
        'jarak' => '0'
    ];
    $save_result = api_save_masuk($token, $presensi_data);
    echo "Success: " . ($save_result['success'] ? 'YES' : 'NO') . "<br>";
    echo "Response: " . json_encode($save_result['data'], JSON_PRETTY_PRINT) . "<br><br>";
} else {
    echo "<strong style='color: red;'>Login gagal!</strong><br>";
    echo "Error: " . json_encode($login_result, JSON_PRETTY_PRINT) . "<br>";
}
