<!DOCTYPE html>
<html>
<head>
    <title>Check Saved Data</title>
    <style>
        body { font-family: Arial; padding: 20px; background: #f5f5f5; }
        .container { background: white; padding: 20px; border-radius: 8px; margin-bottom: 20px; }
        table { width: 100%; border-collapse: collapse; margin: 10px 0; }
        th, td { padding: 8px; border: 1px solid #ddd; text-align: left; font-size: 12px; }
        th { background: #4CAF50; color: white; }
        .success { color: green; font-weight: bold; }
        .error { color: red; font-weight: bold; }
    </style>
</head>
<body>
    <h1>🔍 Database Data Check</h1>
    
    <?php
    try {
        $db = new PDO('mysql:host=localhost;dbname=hrm2;charset=utf8mb4', 'root', '');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        echo "<div class='container'>";
        echo "<h2>✅ Database Connected</h2>";
        
        // Count all records
        $countStmt = $db->query("SELECT COUNT(*) FROM salary_register");
        $totalRecords = $countStmt->fetchColumn();
        
        echo "<p class='success'>Total records in database: <strong>{$totalRecords}</strong></p>";
        
        if ($totalRecords > 0) {
            // Get all records
            $stmt = $db->query("
                SELECT 
                    register_id,
                    site_id,
                    month,
                    employee_name,
                    designation,
                    basic_employer,
                    days_present,
                    basic_earned,
                    pf,
                    created_at
                FROM salary_register 
                ORDER BY created_at DESC 
                LIMIT 20
            ");
            
            $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
            
            echo "<h3>Latest 20 Records:</h3>";
            echo "<table>";
            echo "<tr>";
            echo "<th>ID</th><th>Site</th><th>Month</th><th>Employee Name</th>";
            echo "<th>Designation</th><th>Basic</th><th>Days</th><th>PF</th><th>Saved At</th>";
            echo "</tr>";
            
            foreach ($records as $record) {
                echo "<tr>";
                echo "<td>{$record['register_id']}</td>";
                echo "<td>{$record['site_id']}</td>";
                echo "<td>{$record['month']}</td>";
                echo "<td><strong>{$record['employee_name']}</strong></td>";
                echo "<td>{$record['designation']}</td>";
                echo "<td>{$record['basic_employer']}</td>";
                echo "<td>{$record['days_present']}</td>";
                echo "<td>{$record['pf']}</td>";
                echo "<td>{$record['created_at']}</td>";
                echo "</tr>";
            }
            
            echo "</table>";
            
            // Group by month
            $monthStmt = $db->query("
                SELECT month, COUNT(*) as count 
                FROM salary_register 
                GROUP BY month 
                ORDER BY created_at DESC
            ");
            
            echo "<h3>Records by Month:</h3>";
            echo "<ul>";
            while ($row = $monthStmt->fetch(PDO::FETCH_ASSOC)) {
                echo "<li><strong>{$row['month']}</strong>: {$row['count']} employees</li>";
            }
            echo "</ul>";
            
        } else {
            echo "<p class='error'>❌ No data found in database!</p>";
            echo "<p>This means data is NOT being saved.</p>";
        }
        
        echo "</div>";
        
    } catch (Exception $e) {
        echo "<div class='container'>";
        echo "<p class='error'>❌ Database Error: " . $e->getMessage() . "</p>";
        echo "</div>";
    }
    ?>
    
    <div class='container'>
        <h3>🔗 Quick Links</h3>
        <a href='modules/payroll/salary_register.php' style='display:inline-block;padding:10px 20px;background:#4CAF50;color:white;text-decoration:none;border-radius:4px;margin:5px;'>📊 Salary Register</a>
        <a href='test_salary_register.php' style='display:inline-block;padding:10px 20px;background:#2196F3;color:white;text-decoration:none;border-radius:4px;margin:5px;'>🔧 Database Test</a>
    </div>
    
</body>
</html>
