<?php
// Simple Dompdf Test
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Dompdf Test</h2>";

// Check if Dompdf is loaded
if (file_exists('vendor/autoload.php')) {
    require_once 'vendor/autoload.php';
    echo "<p style='color: green;'>✅ Autoload file found and loaded</p>";
    
    if (class_exists('Dompdf\Dompdf')) {
        echo "<p style='color: green;'>✅ Dompdf class available</p>";
        
        try {
            // Test PDF generation
            $dompdf = new Dompdf\Dompdf();
            $html = '<h1 style="color: blue;">Test PDF Document</h1><p>This PDF was generated successfully!</p><p>Current time: ' . date('Y-m-d H:i:s') . '</p>';
            $dompdf->loadHtml($html);
            $dompdf->setPaper('A4', 'portrait');
            $dompdf->render();
            $output = $dompdf->output();
            
            echo "<p style='color: green;'>✅ PDF generated successfully</p>";
            echo "<p>PDF size: " . strlen($output) . " bytes</p>";
            
            // Save test PDF
            file_put_contents('test_output.pdf', $output);
            echo "<p style='color: blue;'>📄 Test PDF saved as 'test_output.pdf'</p>";
            
            // Download link
            echo "<p><a href='test_output.pdf' download>📥 Download Test PDF</a></p>";
            
        } catch (Exception $e) {
            echo "<p style='color: red;'>❌ PDF generation failed: " . $e->getMessage() . "</p>";
            echo "<p>Stack trace:</p>";
            echo "<pre>" . $e->getTraceAsString() . "</pre>";
        }
        
    } else {
        echo "<p style='color: red;'>❌ Dompdf class not found</p>";
    }
} else {
    echo "<p style='color: red;'>❌ Autoload file not found</p>";
    echo "<p>Checking vendor directory...</p>";
    if (is_dir('vendor')) {
        echo "<p style='color: orange;'>⚠️ Vendor directory exists but autoload.php missing</p>";
        dir_list('vendor');
    } else {
        echo "<p style='color: red;'>❌ Vendor directory not found</p>";
    }
}

function dir_list($dir) {
    echo "<p>Contents of $dir:</p><ul>";
    if (is_dir($dir)) {
        $files = scandir($dir);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                echo "<li>$file</li>";
            }
        }
    }
    echo "</ul>";
}
?>