<?php
// Emergency diagnostic and fix script

echo "<h1>🚨 Emergency PHP Configuration Fix</h1>";

// Check if we can run PHP at all
echo "<h2>1. PHP Status Check</h2>";
echo "<p>✅ PHP is working (you can see this page)</p>";
echo "<p>PHP Version: " . phpversion() . "</p>";

// Check current settings
echo "<h2>2. Current PHP Settings</h2>";
echo "<table border='1' style='border-collapse: collapse;'>";
echo "<tr><th>Setting</th><th>Value</th></tr>";
echo "<tr><td>max_input_vars</td><td>" . ini_get('max_input_vars') . "</td></tr>";
echo "<tr><td>memory_limit</td><td>" . ini_get('memory_limit') . "</td></tr>";
echo "<tr><td>max_execution_time</td><td>" . ini_get('max_execution_time') . "</td></tr>";
echo "</table>";

// Show php.ini location
echo "<h2>3. PHP Configuration File</h2>";
echo "<p><strong>php.ini location:</strong> " . php_ini_loaded_file() . "</p>";

// Instructions for manual fix
echo "<h2>4. Manual Fix Instructions</h2>";
echo "<div style='background: #fff3cd; padding: 15px; border-radius: 5px;'>";
echo "<h3>If Apache won't start after php.ini changes:</h3>";
echo "<ol>";
echo "<li><strong>Stop Apache</strong> in XAMPP Control Panel</li>";
echo "<li><strong>Open php.ini</strong> at: <code>" . php_ini_loaded_file() . "</code></li>";
echo "<li><strong>Find line 424</strong> and make sure it looks EXACTLY like this:</li>";
echo "<pre style='background: #f8f9fa; padding: 10px;'>max_input_vars = 5000</pre>";
echo "<li><strong>NOT like this:</strong></li>";
echo "<pre style='background: #f8d7da; padding: 10px;'>;max_input_vars = 1000</pre>";
echo "<li><strong>Save the file</strong></li>";
echo "<li><strong>Start Apache</strong> again</li>";
echo "</ol>";
echo "</div>";

// Alternative solution
echo "<h2>5. Alternative Solution</h2>";
echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px;'>";
echo "<p><strong>Good news:</strong> The main fatal error in marks/entry.php is already fixed!</p>";
echo "<p>Even if max_input_vars stays at 1000, you can still use the marks entry system with smaller batches of students.</p>";
echo "<p><strong>Current capacity:</strong> ~166 students per form (with max_input_vars = 1000)</p>";
echo "</div>";

// Test the main fix
echo "<h2>6. Test Main Fix (String Addition)</h2>";
$test_marks = ['cq' => '25.5', 'mcq' => '15', 'practical' => '10.5', 'assignment' => '5', 'attendance' => '4'];

try {
    $total = (float)($test_marks['cq'] ?? 0) + (float)($test_marks['mcq'] ?? 0) + (float)($test_marks['practical'] ?? 0) + 
             (float)($test_marks['assignment'] ?? 0) + (float)($test_marks['attendance'] ?? 0);
    
    echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px;'>";
    echo "<p><strong>✅ MAIN FIX WORKING:</strong> String addition test passed</p>";
    echo "<p>Total: $total (no fatal error!)</p>";
    echo "</div>";
} catch (Exception $e) {
    echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
    echo "<p><strong>❌ Error:</strong> " . $e->getMessage() . "</p>";
    echo "</div>";
}

echo "<h2>7. Next Steps</h2>";
echo "<ol>";
echo "<li><strong>Fix php.ini</strong> using the instructions above</li>";
echo "<li><strong>Test marks entry:</strong> <a href='marks/entry.php'>marks/entry.php</a></li>";
echo "<li><strong>If still having issues,</strong> the system will work with smaller student batches</li>";
echo "</ol>";
?>