<?php
/**
 * Form Data Debug Page
 * Check what data is being posted from the salary register form
 */

session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>🔍 Form Data Debug</h1>";
echo "<style>body{font-family:Arial;padding:20px;background:#f5f5f5;} .box{background:white;padding:20px;margin:10px 0;border-radius:8px;border-left:4px solid #4CAF50;} pre{background:#f9f9f9;padding:10px;border-radius:4px;overflow:auto;}</style>";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "<div class='box'>";
    echo "<h2>✅ POST Data Received!</h2>";
    
    echo "<h3>Raw POST Data:</h3>";
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    
    if (isset($_POST['employees'])) {
        echo "<h3>📋 Employees Data:</h3>";
        $count = 0;
        $withNames = 0;
        
        foreach ($_POST['employees'] as $index => $empData) {
            $count++;
            $name = trim($empData['employee_name'] ?? '');
            
            if (!empty($name)) {
                $withNames++;
                echo "<div style='background:#e8f5e9;padding:10px;margin:5px 0;border-radius:4px;'>";
                echo "<strong>Row {$index}:</strong> {$name}<br>";
                echo "Designation: " . ($empData['designation'] ?? 'N/A') . "<br>";
                echo "Basic: " . ($empData['basic_employer'] ?? 'N/A');
                echo "</div>";
            } else {
                echo "<div style='background:#ffebee;padding:10px;margin:5px 0;border-radius:4px;'>";
                echo "<strong>Row {$index}:</strong> <em>(empty - no name)</em>";
                echo "</div>";
            }
        }
        
        echo "<div style='margin-top:20px;padding:15px;background:#e3f2fd;border-radius:4px;'>";
        echo "<strong>Summary:</strong><br>";
        echo "Total Rows Checked: {$count}<br>";
        echo "Rows With Names: <strong style='color:green;'>{$withNames}</strong><br>";
        echo "Empty Rows: <strong style='color:red;'>" . ($count - $withNames) . "</strong>";
        echo "</div>";
        
        if ($withNames === 0) {
            echo "<div style='margin-top:20px;padding:15px;background:#fff3cd;border-radius:4px;border-left:4px solid #ffc107;'>";
            echo "<strong>⚠️ Warning:</strong> No employee names found!<br>";
            echo "This is why you get the 'No employees to save' error.<br>";
            echo "Please make sure to fill at least one employee name.";
            echo "</div>";
        } else {
            echo "<div style='margin-top:20px;padding:15px;background:#d4edda;border-radius:4px;border-left:4px solid #28a745;'>";
            echo "<strong>✅ Good!</strong> Found {$withNames} employee(s) with names.<br>";
            echo "This data should save successfully!";
            echo "</div>";
        }
        
    } else {
        echo "<div style='color:red;'><strong>❌ No 'employees' array in POST data!</strong></div>";
        echo "<p>This means the form is not submitting employee data correctly.</p>";
    }
    
    echo "</div>";
    
} else {
    echo "<div class='box'>";
    echo "<h2>📝 How to Use This Page</h2>";
    echo "<ol>";
    echo "<li>Go to Salary Register page</li>";
    echo "<li>Fill some employee data</li>";
    echo "<li>In the form, change the <code>action</code> attribute to point here</li>";
    echo "<li>Or manually create a test form below</li>";
    echo "</ol>";
    echo "</div>";
    
    // Create a simple test form
    echo "<div class='box'>";
    echo "<h2>🧪 Quick Test Form</h2>";
    echo "<p>Fill this form and submit to see if data is being passed:</p>";
    
    echo "<form method='POST' action='check_form_data.php'>";
    echo "<table border='1' cellpadding='10' style='border-collapse:collapse;margin:10px 0;'>";
    echo "<tr><th>Sl</th><th>Employee Name</th><th>Designation</th><th>Basic</th></tr>";
    
    for ($i = 0; $i < 3; $i++) {
        echo "<tr>";
        echo "<td>" . ($i + 1) . "</td>";
        echo "<td><input type='text' name='employees[{$i}][employee_name]' placeholder='Enter name' style='padding:5px;width:200px;'></td>";
        echo "<td><input type='text' name='employees[{$i}][designation]' placeholder='Position' style='padding:5px;width:150px;'></td>";
        echo "<td><input type='number' name='employees[{$i}][basic_employer]' placeholder='15000' style='padding:5px;width:100px;'></td>";
        echo "</tr>";
    }
    
    echo "</table>";
    echo "<button type='submit' name='test_submit' style='padding:10px 20px;background:#4CAF50;color:white;border:none;border-radius:4px;cursor:pointer;font-size:14px;'>✅ Test Submit</button>";
    echo "</form>";
    echo "</div>";
}

echo "<div class='box' style='border-left-color:#2196F3;'>";
echo "<h3>🔗 Quick Links</h3>";
echo "<a href='modules/payroll/salary_register.php' style='display:inline-block;padding:8px 16px;background:#2196F3;color:white;text-decoration:none;border-radius:4px;margin:5px;'>🚀 Go to Salary Register</a>";
echo "<a href='test_salary_register.php' style='display:inline-block;padding:8px 16px;background:#4CAF50;color:white;text-decoration:none;border-radius:4px;margin:5px;'>🔧 Database Test</a>";
echo "</div>";
?>
