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

// Debug login langsung
$username = $_POST['username'] ?? 'admin';
$password = $_POST['password'] ?? 'admin';

echo "<h2>Debug Login</h2>";
echo "Username: " . htmlspecialchars($username) . "<br>";
echo "Password: " . htmlspecialchars($password) . "<br><br>";

// Test langsung dengan curl
$url = API_BASE . 'login';
$data = json_encode([
    'username' => $username,
    'password' => $password
]);

echo "URL: " . $url . "<br>";
echo "Data: " . $data . "<br><br>";

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Accept: application/json',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
    ],
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_VERBOSE => true
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
$info = curl_getinfo($ch);

echo "<h3>Response:</h3>";
echo "HTTP Code: " . $httpCode . "<br>";
echo "Error: " . ($error ?: 'None') . "<br>";
echo "Response: " . htmlspecialchars($response) . "<br><br>";

echo "<h3>CURL Info:</h3>";
echo "<pre>" . print_r($info, true) . "</pre>";

curl_close($ch);

// Test dengan method GET juga
echo "<h3>Test GET Request:</h3>";
$get_url = API_BASE . 'login?username=' . urlencode($username) . '&password=' . urlencode($password);
echo "GET URL: " . $get_url . "<br>";

$ch2 = curl_init();
curl_setopt_array($ch2, [
    CURLOPT_URL => $get_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_FOLLOWLOCATION => true
]);

$response2 = curl_exec($ch2);
$httpCode2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
$error2 = curl_error($ch2);

echo "HTTP Code: " . $httpCode2 . "<br>";
echo "Error: " . ($error2 ?: 'None') . "<br>";
echo "Response: " . htmlspecialchars($response2) . "<br>";

curl_close($ch2);
?>

<form method="POST">
    <input type="text" name="username" placeholder="Username" value="<?php echo htmlspecialchars($username); ?>">
    <input type="password" name="password" placeholder="Password" value="<?php echo htmlspecialchars($password); ?>">
    <button type="submit">Test Login</button>
</form>