<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

echo "<h2>🔍 Debug Error</h2>";
echo "<pre style='background:#f4f4f4; padding:15px; border:1px solid #ddd;'>";

try {
    echo "Step 1: Load auth...\n";
    require_once __DIR__ . '/includes/auth.php';
    echo "✅ auth.php loaded\n\n";
    
    echo "Step 2: Load functions...\n";
    require_once __DIR__ . '/includes/functions.php';
    echo "✅ functions.php loaded\n\n";
    
    echo "Step 3: Check session...\n";
    startSession();
    echo "✅ Session started\n";
    echo "   Logged in: " . (isLoggedIn() ? 'YES' : 'NO') . "\n";
    echo "   Role: " . (currentRole() ?? 'N/A') . "\n\n";
    
    echo "Step 4: Load database...\n";
    $db = db();
    echo "✅ Database connected\n\n";
    
    echo "Step 5: Test queries...\n";
    $total = $db->query("SELECT COUNT(*) FROM customers")->fetchColumn();
    echo "✅ Customers: $total\n";
    
    $total = $db->query("SELECT COUNT(*) FROM branches")->fetchColumn();
    echo "✅ Branches: $total\n";
    
    $total = $db->query("SELECT COUNT(*) FROM bills")->fetchColumn();
    echo "✅ Bills: $total\n\n";
    
    echo "Step 6: Load header...\n";
    // Skip header karena output HTML
    echo "⏭️ Skipped (akan test manual)\n\n";
    
    echo "✅✅✅ Semua test berhasil!\n";
    echo "Masalah ada di header.php, sidebar.php, atau footer.php\n";
    
} catch (Exception $e) {
    echo "\n❌ ERROR:\n";
    echo "Message: " . $e->getMessage() . "\n";
    echo "File: " . $e->getFile() . "\n";
    echo "Line: " . $e->getLine() . "\n";
    echo "\nTrace:\n" . $e->getTraceAsString() . "\n";
}

echo "</pre>";
?>