<?php
require_once 'RouterosAPI.php';

// Konfigurasi Mikrotik
$config = [
    'host' => 'sg-17.hostddns.us',
    'user' => 'kuci',
    'pass' => 'kuci',
    'port' => 47709,
    'timeout' => 3
];

echo "<h2>Test Koneksi Mikrotik API</h2>";
echo "<pre>";
echo "Host: {$config['host']}\n";
echo "Port: {$config['port']}\n";
echo "User: {$config['user']}\n";
echo "Timeout: {$config['timeout']} detik\n";
echo str_repeat("-", 50) . "\n\n";

$startTime = microtime(true);

$api = new RouterosAPI();
$result = $api->connect(
    $config['host'],
    $config['user'],
    $config['pass'],
    $config['port'],
    $config['timeout']
);

$endTime = microtime(true);
$duration = round($endTime - $startTime, 3);

if ($result) {
    echo "✅ KONEKSI BERHASIL!\n";
    echo "Waktu koneksi: {$duration} detik\n\n";
    
    // Test ambil info sistem
    echo "Mengambil informasi sistem...\n";
    $identity = $api->comm('/system/identity/print');
    $resource = $api->comm('/system/resource/print');
    
    if (!empty($identity)) {
        echo "Nama Router: " . ($identity[0]['name'] ?? 'N/A') . "\n";
    }
    
    if (!empty($resource)) {
        echo "Version: " . ($resource[0]['version'] ?? 'N/A') . "\n";
        echo "Board: " . ($resource[0]['board-name'] ?? 'N/A') . "\n";
        echo "Uptime: " . ($resource[0]['uptime'] ?? 'N/A') . "\n";
        echo "CPU Load: " . ($resource[0]['cpu-load'] ?? 'N/A') . "%\n";
    }
    
    $api->disconnect();
    
} else {
    echo "❌ KONEKSI GAGAL!\n";
    echo "Error: " . $api->error . "\n";
    echo "Waktu mencoba: {$duration} detik\n";
}

echo "</pre>";