<?php
/**
 * Debug Error - HAPUS SETELAH SELESAI!
 */

// Aktifkan error reporting
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 {
    // Test 1: Load database
    echo "=== Test 1: Load Database ===\n";
    require_once __DIR__ . '/includes/db.php';
    echo "✅ includes/db.php loaded\n";
    
    // Test 2: Connect to database
    echo "\n=== Test 2: Connect to Database ===\n";
    $db = db();
    echo "✅ Database connected\n";
    
    // Test 3: Check tables
    echo "\n=== Test 3: Check Tables ===\n";
    $tables = ['branches', 'users', 'packages', 'customers', 'bills', 'settings'];
    foreach ($tables as $table) {
        try {
            $count = $db->query("SELECT COUNT(*) FROM $table")->fetchColumn();
            echo "✅ Table '$table': $count rows\n";
        } catch (Exception $e) {
            echo "❌ Table '$table': " . $e->getMessage() . "\n";
        }
    }
    
    // Test 4: Load functions
    echo "\n=== Test 4: Load Functions ===\n";
    require_once __DIR__ . '/includes/functions.php';
    echo "✅ includes/functions.php loaded\n";
    
    // Test 5: Test getSetting
    echo "\n=== Test 5: Test getSetting() ===\n";
    try {
        $appName = getSetting('app_name', 'Default App');
        echo "✅ getSetting('app_name'): $appName\n";
    } catch (Exception $e) {
        echo "❌ getSetting() error: " . $e->getMessage() . "\n";
    }
    
    // Test 6: Load classes
    echo "\n=== Test 6: Load Classes ===\n";
    require_once __DIR__ . '/classes/Customer.php';
    echo "✅ Customer.php loaded\n";
    require_once __DIR__ . '/classes/Bill.php';
    echo "✅ Bill.php loaded\n";
    require_once __DIR__ . '/classes/Branch.php';
    echo "✅ Branch.php loaded\n";
    
    // Test 7: Test Customer::all()
    echo "\n=== Test 7: Test Customer::all() ===\n";
    try {
        $customers = Customer::all();
        echo "✅ Customer::all(): " . count($customers) . " customers\n";
    } catch (Exception $e) {
        echo "❌ Customer::all() error: " . $e->getMessage() . "\n";
    }
    
    // Test 8: Test Branch::all()
    echo "\n=== Test 8: Test Branch::all() ===\n";
    try {
        $branches = Branch::all();
        echo "✅ Branch::all(): " . count($branches) . " branches\n";
    } catch (Exception $e) {
        echo "❌ Branch::all() error: " . $e->getMessage() . "\n";
    }
    
    // Test 9: Check user session
    echo "\n=== Test 9: Check Session ===\n";
    session_start();
    if (isset($_SESSION['logged_in'])) {
        echo "✅ Session logged_in: YES\n";
        echo "   User ID: " . ($_SESSION['user_id'] ?? 'N/A') . "\n";
        echo "   Role: " . ($_SESSION['role'] ?? 'N/A') . "\n";
    } else {
        echo "❌ Session logged_in: NO\n";
    }
    
    echo "\n✅✅✅ All tests completed!";
    
} catch (Exception $e) {
    echo "\n❌❌❌ FATAL ERROR:\n";
    echo "Message: " . $e->getMessage() . "\n";
    echo "File: " . $e->getFile() . "\n";
    echo "Line: " . $e->getLine() . "\n";
    echo "Trace:\n" . $e->getTraceAsString() . "\n";
}

echo "</pre>";
?>