<?php
header('Content-Type: text/html; charset=utf-8');

echo "<h2>🧪 Testing Tatkal Dates API</h2>";
echo "<style>
    body { font-family: Arial, sans-serif; margin: 20px; }
    .success { color: green; }
    .error { color: red; }
    pre { background: #f5f5f5; padding: 15px; border-radius: 5px; overflow-x: auto; }
</style>";

// Test the API directly
try {
    $apiUrl = 'https://astro.siddharthnahar.in/astro_admin/api/get_tatkal_dates.php';
    
    echo "<h3>📡 API URL:</h3>";
    echo "<code>$apiUrl</code><br><br>";
    
    echo "<h3>📊 API Response:</h3>";
    
    $context = stream_context_create([
        'http' => [
            'method' => 'GET',
            'header' => [
                'Cache-Control: no-cache',
                'Pragma: no-cache'
            ]
        ]
    ]);
    
    $response = file_get_contents($apiUrl, false, $context);
    
    if ($response === false) {
        echo "<span class='error'>❌ Failed to fetch API response</span>";
    } else {
        echo "<pre>" . htmlspecialchars($response) . "</pre>";
        
        // Try to decode JSON
        $data = json_decode($response, true);
        if ($data) {
            echo "<h3>✅ JSON Decoded Successfully:</h3>";
            echo "<pre>" . print_r($data, true) . "</pre>";
            
            if (isset($data['available_dates'])) {
                echo "<h3>📅 Available Dates Count: " . count($data['available_dates']) . "</h3>";
                
                if (!empty($data['available_dates'])) {
                    echo "<h4>Dates List:</h4>";
                    foreach ($data['available_dates'] as $date) {
                        echo "• " . ($date['formatted_date'] ?? $date['date']) . " (" . ($date['day_name'] ?? 'Unknown') . ")<br>";
                    }
                }
            }
        } else {
            echo "<span class='error'>❌ Invalid JSON response</span>";
        }
    }
    
} catch (Exception $e) {
    echo "<span class='error'>❌ Error: " . $e->getMessage() . "</span>";
}

echo "<br><br><a href='../admin/tatkal_management_panel.php'>← Back to Tatkal Management</a>";
echo " | <a href='get_tatkal_dates.php' target='_blank'>🔗 Direct API</a>";
?>
