<?php
/**
 * Quick Verification Script
 * Run this to check if the 500 error is fixed
 * Access via: http://yoursite.com/verify_fix.php
 * 
 * DELETE THIS FILE after verification!
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>500 Error Fix Verification</h1>";
echo "<hr>";

// Test 1: Database Connection
echo "<h2>Test 1: Database Connection</h2>";
try {
    require_once 'include/dbconfig.php';
    echo "<p style='color: green;'>✓ Database connection successful</p>";
    echo "<p>Database: " . DB_NAME . "</p>";
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ Database connection failed: " . $e->getMessage() . "</p>";
    exit;
}

// Test 2: Check if username column exists
echo "<h2>Test 2: Username Column Check</h2>";
$query = "SHOW COLUMNS FROM account LIKE 'username'";
$result = mysqli_query($connection, $query);

if ($result && mysqli_num_rows($result) > 0) {
    echo "<p style='color: green;'>✓ Username column exists</p>";
    $column = mysqli_fetch_assoc($result);
    echo "<pre>";
    print_r($column);
    echo "</pre>";
} else {
    echo "<p style='color: red;'>✗ Username column does NOT exist</p>";
    echo "<p><strong>ACTION REQUIRED:</strong> Run fix_database.sql script</p>";
}

// Test 3: Check account data
echo "<h2>Test 3: Account Data Check</h2>";
$query = "SELECT id, acc_no, uname, username, fname, lname, email FROM account LIMIT 5";
$result = mysqli_query($connection, $query);

if ($result) {
    echo "<p style='color: green;'>✓ Query executed successfully</p>";
    echo "<table border='1' cellpadding='5' style='border-collapse: collapse;'>";
    echo "<tr><th>ID</th><th>Account No</th><th>uname</th><th>username</th><th>Name</th><th>Email</th></tr>";
    
    $has_data = false;
    while ($row = mysqli_fetch_assoc($result)) {
        $has_data = true;
        $username_status = $row['username'] ? $row['username'] : "<span style='color: red;'>NULL</span>";
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['acc_no'] . "</td>";
        echo "<td>" . $row['uname'] . "</td>";
        echo "<td>" . $username_status . "</td>";
        echo "<td>" . $row['fname'] . " " . $row['lname'] . "</td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    if (!$has_data) {
        echo "<p style='color: orange;'>⚠ No accounts found in database</p>";
    }
} else {
    echo "<p style='color: red;'>✗ Query failed: " . mysqli_error($connection) . "</p>";
}

// Test 4: Check for accounts without username
echo "<h2>Test 4: Missing Username Check</h2>";
$query = "SELECT COUNT(*) as count FROM account WHERE username IS NULL OR username = ''";
$result = mysqli_query($connection, $query);

if ($result) {
    $row = mysqli_fetch_assoc($result);
    $missing_count = $row['count'];
    
    if ($missing_count > 0) {
        echo "<p style='color: orange;'>⚠ Found $missing_count account(s) without username</p>";
        echo "<p><strong>ACTION REQUIRED:</strong> Run populate_usernames.php or update manually</p>";
    } else {
        echo "<p style='color: green;'>✓ All accounts have usernames</p>";
    }
}

// Test 5: Test login query
echo "<h2>Test 5: Login Query Test</h2>";
$test_username = 'Webster';
$test_password = '12345678!';

$username_escaped = mysqli_real_escape_string($connection, $test_username);
$password_escaped = mysqli_real_escape_string($connection, $test_password);

$query = "SELECT * FROM account WHERE username='$username_escaped' AND upass='$password_escaped'";
$result = mysqli_query($connection, $query);

if ($result) {
    $count = mysqli_num_rows($result);
    if ($count > 0) {
        echo "<p style='color: green;'>✓ Login query works! Found matching account</p>";
        $account = mysqli_fetch_assoc($result);
        echo "<p>Account found: " . $account['fname'] . " " . $account['lname'] . " (ID: " . $account['id'] . ")</p>";
    } else {
        echo "<p style='color: orange;'>⚠ Login query works but no matching account found</p>";
        echo "<p>Tested with username: '$test_username' and password: '$test_password'</p>";
        echo "<p>Make sure this account exists with these credentials</p>";
    }
} else {
    echo "<p style='color: red;'>✗ Login query failed: " . mysqli_error($connection) . "</p>";
}

// Test 6: Check class.user.php
echo "<h2>Test 6: User Class Check</h2>";
try {
    require_once 'include/class.user.php';
    $reg_user = new USER();
    echo "<p style='color: green;'>✓ USER class loaded successfully</p>";
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ USER class failed: " . $e->getMessage() . "</p>";
}

// Summary
echo "<hr>";
echo "<h2>Summary</h2>";
echo "<p><strong>If all tests passed:</strong></p>";
echo "<ol>";
echo "<li>Try logging in at your login page</li>";
echo "<li>Use username: 'Webster' and password: '12345678!'</li>";
echo "<li>If login works, DELETE this file (verify_fix.php) for security</li>";
echo "</ol>";

echo "<p><strong>If tests failed:</strong></p>";
echo "<ol>";
echo "<li>Run fix_database.sql script in phpMyAdmin</li>";
echo "<li>Refresh this page to verify again</li>";
echo "<li>Check FIX_500_ERROR_GUIDE.md for detailed instructions</li>";
echo "</ol>";

mysqli_close($connection);
?>

<style>
    body {
        font-family: Arial, sans-serif;
        margin: 20px;
        background-color: #f5f5f5;
    }
    h1 {
        color: #333;
    }
    h2 {
        color: #555;
        margin-top: 30px;
        padding: 10px;
        background-color: #e0e0e0;
    }
    table {
        background-color: white;
        margin: 10px 0;
    }
    th {
        background-color: #4CAF50;
        color: white;
    }
</style>
