<?php
/**
 * Attendance Debug - Check what's wrong
 */
require_once __DIR__ . '/../../includes/init.php';
requireLogin();

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Attendance Debug</title>
    <style>
        body { font-family: Arial; padding: 20px; }
        .success { background: #d1fae5; padding: 10px; margin: 10px 0; border-radius: 6px; }
        .error { background: #fee2e2; padding: 10px; margin: 10px 0; border-radius: 6px; }
        .info { background: #dbeafe; padding: 10px; margin: 10px 0; border-radius: 6px; }
    </style>
</head>
<body>
    <h1>Attendance Module Debug</h1>
    
    <?php
    try {
        $db = getDB();
        echo "<div class='success'>✅ Database connection successful</div>";
        
        // Check if attendance table exists
        $stmt = $db->query("SHOW TABLES LIKE 'attendance'");
        if ($stmt->rowCount() > 0) {
            echo "<div class='success'>✅ Attendance table exists</div>";
        } else {
            echo "<div class='error'>❌ Attendance table does NOT exist!</div>";
            echo "<div class='info'>You need to import the complete schema.sql file</div>";
        }
        
        // Check if employees table exists
        $stmt = $db->query("SHOW TABLES LIKE 'employees'");
        if ($stmt->rowCount() > 0) {
            echo "<div class='success'>✅ Employees table exists</div>";
        } else {
            echo "<div class='error'>❌ Employees table does NOT exist!</div>";
        }
        
        // Check site_id
        $siteId = getCurrentSiteId();
        echo "<div class='info'>Current Site ID: " . ($siteId ?? 'NOT SET') . "</div>";
        
        // Try to get attendance count
        if ($siteId) {
            try {
                $stmt = $db->prepare("SELECT COUNT(*) FROM attendance WHERE site_id = ?");
                $stmt->execute([$siteId]);
                $count = $stmt->fetchColumn();
                echo "<div class='success'>✅ Attendance records found: " . $count . "</div>";
            } catch (Exception $e) {
                echo "<div class='error'>❌ Error querying attendance: " . htmlspecialchars($e->getMessage()) . "</div>";
            }
        }
        
        // List all tables
        echo "<h2>All Tables in Database:</h2>";
        $stmt = $db->query("SHOW TABLES");
        $tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
        echo "<ul>";
        foreach ($tables as $table) {
            echo "<li>" . htmlspecialchars($table) . "</li>";
        }
        echo "</ul>";
        
    } catch (Exception $e) {
        echo "<div class='error'>❌ Error: " . htmlspecialchars($e->getMessage()) . "</div>";
    }
    ?>
    
    <hr>
    <p><a href="<?php echo BASE_URL; ?>/modules/attendance/list.php">Go to Attendance List</a></p>
    <p><a href="<?php echo BASE_URL; ?>/index.php">Back to Dashboard</a></p>
</body>
</html>

