<?php
/**
 * Simple Database Connection Test
 * Quick test to check if database connection works
 */

// Enable error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);

echo "<h1>Simple Database Test</h1>";

// Database credentials
$host = 'localhost';
$username = 'homecar1_root';
$password = '2021wemadeit';
$database = 'homecar1_connectpay';

echo "<p><strong>Testing connection to:</strong> $database@$host</p>";

try {
    // Create connection
    $conn = new mysqli($host, $username, $password, $database);
    
    // Check connection
    if ($conn->connect_error) {
        throw new Exception("Connection failed: " . $conn->connect_error);
    }
    
    echo "<p style='color: green; font-weight: bold;'>✓ DATABASE CONNECTION SUCCESS!</p>";
    
    // Test simple query
    $result = $conn->query("SELECT 1 as test");
    if ($result) {
        echo "<p style='color: green;'>✓ Query test passed</p>";
    } else {
        echo "<p style='color: red;'>✗ Query test failed: " . $conn->error . "</p>";
    }
    
    // Check if users table exists
    $result = $conn->query("SHOW TABLES LIKE 'users'");
    if ($result && $result->num_rows > 0) {
        echo "<p style='color: green;'>✓ Users table exists</p>";
        
        // Count users
        $result = $conn->query("SELECT COUNT(*) as count FROM users");
        if ($result) {
            $row = $result->fetch_assoc();
            echo "<p>Total users: " . $row['count'] . "</p>";
        }
    } else {
        echo "<p style='color: red;'>✗ Users table missing</p>";
    }
    
    // Test including dbconn.php
    echo "<hr><h2>Testing dbconn.php</h2>";
    
    if (file_exists('layouts/dbconn.php')) {
        echo "<p style='color: green;'>✓ dbconn.php file found</p>";
        
        // Close current connection to test dbconn.php
        $conn->close();
        
        // Include dbconn.php
        include_once 'layouts/dbconn.php';
        
        if (isset($conn) && $conn instanceof mysqli) {
            echo "<p style='color: green;'>✓ dbconn.php works correctly!</p>";
            
            // Test query through dbconn
            $test = $conn->query("SELECT 'dbconn works' as message");
            if ($test) {
                $row = $test->fetch_assoc();
                echo "<p style='color: green;'>✓ " . $row['message'] . "</p>";
            }
        } else {
            echo "<p style='color: red;'>✗ dbconn.php failed to create connection</p>";
        }
    } else {
        echo "<p style='color: red;'>✗ dbconn.php file not found in layouts/ folder</p>";
        
        // Show current directory structure
        echo "<p><strong>Current directory:</strong> " . getcwd() . "</p>";
        echo "<p><strong>Looking for:</strong> " . getcwd() . "/layouts/dbconn.php</p>";
        
        if (is_dir('layouts')) {
            echo "<p style='color: orange;'>layouts/ folder exists</p>";
            $files = scandir('layouts');
            echo "<p>Files in layouts/: " . implode(', ', array_diff($files, ['.', '..'])) . "</p>";
        } else {
            echo "<p style='color: red;'>layouts/ folder does not exist</p>";
        }
    }
    
} catch (Exception $e) {
    echo "<p style='color: red; font-weight: bold;'>✗ CONNECTION FAILED!</p>";
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
    
    echo "<hr><h2>Troubleshooting</h2>";
    echo "<ol>";
    echo "<li>Check if MySQL is running</li>";
    echo "<li>Verify database credentials</li>";
    echo "<li>Make sure database '$database' exists</li>";
    echo "<li>Check user permissions for '$username'</li>";
    echo "</ol>";
    
    echo "<h3>Try these commands:</h3>";
    echo "<pre>";
    echo "mysql -u $username -p\n";
    echo "SHOW DATABASES;\n";
    echo "USE $database;\n";
    echo "SHOW TABLES;\n";
    echo "</pre>";
}

echo "<hr>";
echo "<p><em>Test completed at " . date('Y-m-d H:i:s') . "</em></p>";
echo "<p style='background: #ffe6e6; padding: 10px; border-radius: 5px;'>";
echo "<strong>Security Note:</strong> Delete this file after testing!";
echo "</p>";
?>

<style>
body { 
    font-family: Arial, sans-serif; 
    max-width: 800px; 
    margin: 20px auto; 
    padding: 20px; 
}
h1, h2 { 
    color: #333; 
    border-bottom: 1px solid #ccc; 
    padding-bottom: 5px; 
}
pre { 
    background: #f4f4f4; 
    padding: 10px; 
    border-radius: 5px; 
    overflow-x: auto; 
}
</style>