<?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'];

// Test data
$test_data = [
    'latitude' => -6.2088,
    'longitude' => 106.8456,
    'waktu' => date('Y-m-d H:i:s')
];

echo "<h1>Debug API Response - Presensi Submit</h1>";
echo "<p><strong>Token:</strong> " . substr($token, 0, 20) . "...</p>";
echo "<p><strong>Test Data:</strong></p>";
echo "<pre>" . json_encode($test_data, JSON_PRETTY_PRINT) . "</pre>";

echo "<hr>";

// Test Masuk
echo "<h2>Test API Save Masuk</h2>";
$result_masuk = api_save_masuk($token, $test_data);
echo "<p><strong>Success:</strong> " . ($result_masuk['success'] ? 'YES' : 'NO') . "</p>";
echo "<p><strong>HTTP Code:</strong> " . $result_masuk['http_code'] . "</p>";
echo "<p><strong>Raw Response:</strong></p>";
echo "<pre>" . json_encode($result_masuk, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "</pre>";

echo "<hr>";

// Test Pulang
echo "<h2>Test API Save Pulang</h2>";
$result_pulang = api_save_pulang($token, $test_data);
echo "<p><strong>Success:</strong> " . ($result_pulang['success'] ? 'YES' : 'NO') . "</p>";
echo "<p><strong>HTTP Code:</strong> " . $result_pulang['http_code'] . "</p>";
echo "<p><strong>Raw Response:</strong></p>";
echo "<pre>" . json_encode($result_pulang, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "</pre>";

echo "<hr>";

// Test Istirahat
echo "<h2>Test API Save Istirahat</h2>";
$result_istirahat = api_save_istirahat($token, $test_data);
echo "<p><strong>Success:</strong> " . ($result_istirahat['success'] ? 'YES' : 'NO') . "</p>";
echo "<p><strong>HTTP Code:</strong> " . $result_istirahat['http_code'] . "</p>";
echo "<p><strong>Raw Response:</strong></p>";
echo "<pre>" . json_encode($result_istirahat, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "</pre>";

echo "<hr>";

// Test dengan koordinat yang berbeda
echo "<h2>Test dengan Koordinat Jauh (500m dari kantor)</h2>";
$test_data_jauh = [
    'latitude' => -6.2138, // ~500m dari kantor
    'longitude' => 106.8506,
    'waktu' => date('Y-m-d H:i:s')
];

echo "<p><strong>Test Data Jauh:</strong></p>";
echo "<pre>" . json_encode($test_data_jauh, JSON_PRETTY_PRINT) . "</pre>";

$result_jauh = api_save_masuk($token, $test_data_jauh);
echo "<p><strong>Success:</strong> " . ($result_jauh['success'] ? 'YES' : 'NO') . "</p>";
echo "<p><strong>HTTP Code:</strong> " . $result_jauh['http_code'] . "</p>";
echo "<p><strong>Raw Response:</strong></p>";
echo "<pre>" . json_encode($result_jauh, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "</pre>";

echo "<hr>";

// Test dengan koordinat yang sangat jauh
echo "<h2>Test dengan Koordinat Sangat Jauh (10km dari kantor)</h2>";
$test_data_sangat_jauh = [
    'latitude' => -6.3088, // ~10km dari kantor
    'longitude' => 106.9456,
    'waktu' => date('Y-m-d H:i:s')
];

echo "<p><strong>Test Data Sangat Jauh:</strong></p>";
echo "<pre>" . json_encode($test_data_sangat_jauh, JSON_PRETTY_PRINT) . "</pre>";

$result_sangat_jauh = api_save_masuk($token, $test_data_sangat_jauh);
echo "<p><strong>Success:</strong> " . ($result_sangat_jauh['success'] ? 'YES' : 'NO') . "</p>";
echo "<p><strong>HTTP Code:</strong> " . $result_sangat_jauh['http_code'] . "</p>";
echo "<p><strong>Raw Response:</strong></p>";
echo "<pre>" . json_encode($result_sangat_jauh, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "</pre>";

echo "<hr>";
echo "<p><a href='rekam.php'>← Kembali ke Rekam Presensi</a></p>";
