<?php
session_start();
require_once 'config/database.php';
require_once 'includes/site_settings.php';

echo "<h2>Your Uploaded Logos</h2>";

// Get all custom uploaded logos
$custom_logos = glob('uploads/logos/custom_logo_*.jpg');
$session_logo = $_SESSION['custom_logo_path'] ?? null;

echo "<h3>Currently Active Logo (from session)</h3>";
if ($session_logo) {
    $full_session_path = __DIR__ . '/' . ltrim($session_logo, './');
    if (file_exists($full_session_path)) {
        echo "<div style='border: 3px solid green; padding: 15px; margin: 10px 0; background: #f0fff0;'>";
        echo "<h4 style='color: green;'>✅ ACTIVE LOGO</h4>";
        echo "<img src='../$session_logo' style='max-height: 150px; border: 2px solid #28a745; border-radius: 8px;'>";
        echo "<p><strong>Path:</strong> $session_logo</p>";
        echo "<p><strong>Status:</strong> Currently being used in certificates</p>";
        echo "</div>";
    }
} else {
    echo "<div style='border: 2px solid orange; padding: 15px; margin: 10px 0; background: #fff3cd;'>";
    echo "<h4>⚠️ No Active Custom Logo</h4>";
    echo "<p>No custom logo is currently selected for certificates.</p>";
    echo "</div>";
}

echo "<h3>All Your Uploaded Custom Logos</h3>";

if (!empty($custom_logos)) {
    echo "<div style='display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px;'>";
    
    foreach ($custom_logos as $logo_path) {
        $relative_path = str_replace(__DIR__ . '\\', '', $logo_path);
        $file_size = filesize($logo_path);
        $file_info = getimagesize($logo_path);
        $is_active = ($session_logo && strpos($session_logo, basename($logo_path)) !== false);
        
        echo "<div style='border: " . ($is_active ? '3px solid green' : '1px solid #ddd') . "; 
              padding: 15px; 
              border-radius: 8px; 
              background: " . ($is_active ? '#f0fff0' : '#f8f9fa') . ";'>";
        
        if ($is_active) {
            echo "<span style='background: #28a745; color: white; padding: 5px 10px; border-radius: 4px; float: right;'>ACTIVE</span>";
        }
        
        echo "<img src='$relative_path' style='max-width: 100%; max-height: 120px; border-radius: 4px;'>";
        echo "<div style='margin-top: 10px;'>";
        echo "<p><strong>Name:</strong> " . basename($logo_path) . "</p>";
        echo "<p><strong>Size:</strong> " . round($file_size/1024, 2) . " KB</p>";
        echo "<p><strong>Dimensions:</strong> " . $file_info[0] . " × " . $file_info[1] . " px</p>";
        echo "<p><strong>Type:</strong> " . $file_info['mime'] . "</p>";
        echo "</div>";
        
        if (!$is_active) {
            echo "<form method='POST' style='margin-top: 10px;'>";
            echo "<input type='hidden' name='activate_logo' value='$relative_path'>";
            echo "<button type='submit' style='background: #007bff; color: white; border: none; padding: 8px 16px; border-radius: 4px; cursor: pointer;'>";
            echo "Use This Logo";
            echo "</button>";
            echo "</form>";
        }
        
        echo "</div>";
    }
    
    echo "</div>";
} else {
    echo "<div style='border: 2px solid #dc3545; padding: 20px; text-align: center; background: #f8d7da; border-radius: 8px;'>";
    echo "<h4>❌ No Custom Logos Found</h4>";
    echo "<p>You haven't uploaded any custom logos yet.</p>";
    echo "<p>To upload a custom logo:</p>";
    echo "<ol>";
    echo "<li>Go to any certificate page</li>";
    echo "<li>Select a student</li>";
    echo "<li>Click 'Choose Custom Logo' and upload your image</li>";
    echo "<li>Click 'Upload Logo'</li>";
    echo "</ol>";
    echo "</div>";
}

// Handle logo activation
if (isset($_POST['activate_logo'])) {
    $logo_to_activate = $_POST['activate_logo'];
    $_SESSION['custom_logo_path'] = $logo_to_activate;
    echo "<div style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 15px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);'>";
    echo "✅ Logo activated successfully!";
    echo "<br><small>Refresh page to see changes</small>";
    echo "</div>";
    
    // Auto-refresh after 2 seconds
    echo "<script>setTimeout(function(){ window.location.reload(); }, 2000);</script>";
}

echo "<h3>Quick Actions</h3>";
echo "<div style='display: flex; gap: 15px; flex-wrap: wrap;'>";
echo "<a href='certificates/praise_certificate.php' style='background: #17a2b8; color: white; padding: 12px 20px; text-decoration: none; border-radius: 6px;'>View Certificate Example</a>";
echo "<a href='admin/site_settings.php' style='background: #28a745; color: white; padding: 12px 20px; text-decoration: none; border-radius: 6px;'>Upload New Logo</a>";
echo "<a href='.' style='background: #6c757d; color: white; padding: 12px 20px; text-decoration: none; border-radius: 6px;'>Back to Home</a>";
echo "</div>";

echo "<h3>How Custom Logos Work</h3>";
echo "<div style='background: #e9ecef; padding: 20px; border-radius: 8px;'>";
echo "<h4>Logo Priority System:</h4>";
echo "<ol>";
echo "<li><strong>Custom Uploaded Logo</strong> (highest priority) - What you upload specifically for certificates</li>";
echo "<li><strong>Site Settings Logo</strong> - General logo from admin settings</li>";
echo "<li><strong>Default Icon</strong> (fallback) - 🎓 Graduation cap icon</li>";
echo "</ol>";
echo "<p><strong>Current Status:</strong> Your custom logos are working! They just need to be activated to appear in certificates.</p>";
echo "</div>";
?>