<?php
// error_log.php - View PHP Errors
// File location: /bidwheels.net/error_log.php

// Only allow access from localhost for security
$allowed_ips = ['127.0.0.1', '::1'];
if (!in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)) {
    die('Access denied');
}

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

// Create or clear log file
$log_file = __DIR__ . '/logs/php_errors.log';

// Get last 100 lines of error log
$errors = '';
if (file_exists($log_file)) {
    $errors = shell_exec("tail -n 100 " . escapeshellarg($log_file));
} else {
    // Check PHP default error log
    $php_log = ini_get('error_log');
    if ($php_log && file_exists($php_log)) {
        $errors = shell_exec("tail -n 100 " . escapeshellarg($php_log));
    } else {
        $errors = "No error log found. Creating log file...\n";
        // Create logs directory
        if (!is_dir(__DIR__ . '/logs')) {
            mkdir(__DIR__ . '/logs', 0777, true);
        }
        // Set custom error log
        ini_set('error_log', __DIR__ . '/logs/php_errors.log');
        error_log("Error logging enabled at: " . date('Y-m-d H:i:s'));
        $errors = "Error log created at: " . __DIR__ . "/logs/php_errors.log\n";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>PHP Error Logger</title>
    <style>
        body {
            background: #0a0a0a;
            color: #fff;
            font-family: monospace;
            padding: 20px;
        }
        h1 { color: #FF8C3E; }
        .error-container {
            background: #1a1a1a;
            border: 1px solid #333;
            border-radius: 8px;
            padding: 15px;
            margin-top: 20px;
            overflow-x: auto;
        }
        .error-line {
            font-family: monospace;
            font-size: 12px;
            border-bottom: 1px solid #333;
            padding: 8px;
            margin: 0;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        .error-line.error { color: #ff6b6b; background: rgba(255,0,0,0.1); }
        .error-line.warning { color: #ffd93d; }
        .error-line.notice { color: #6bcbff; }
        .success { color: #6bcb77; }
        button {
            background: #FF8C3E;
            color: #000;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover { background: #e67a22; }
        .test-form {
            background: #1a1a1a;
            padding: 20px;
            border-radius: 8px;
            margin-top: 20px;
        }
        input, select {
            background: #333;
            border: 1px solid #555;
            color: white;
            padding: 8px;
            margin: 5px;
            border-radius: 4px;
        }
        .info {
            background: #2a2a2a;
            padding: 15px;
            border-radius: 8px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>🔍 PHP Error Logger - BidWheels</h1>
    
    <div class="info">
        <strong>📁 Log File Location:</strong> <?php echo __DIR__ . '/logs/php_errors.log'; ?><br>
        <strong>🕐 Current Time:</strong> <?php echo date('Y-m-d H:i:s'); ?><br>
        <strong>🔧 PHP Version:</strong> <?php echo phpversion(); ?><br>
        <strong>📊 Error Reporting:</strong> <?php echo ini_get('error_reporting'); ?>
    </div>
    
    <div style="margin: 20px 0;">
        <button onclick="clearLogs()">🗑️ Clear Logs</button>
        <button onclick="location.reload()">🔄 Refresh</button>
        <button onclick="testError()">🧪 Test Error</button>
    </div>
    
    <div class="error-container">
        <h3>📋 Recent Errors (Last 100 lines)</h3>
        <pre style="margin:0; padding:0;">
<?php 
if (empty($errors) || $errors == "No error log found.") {
    echo "\n✅ No errors found. Try placing a bid again and refresh this page.\n";
} else {
    $lines = explode("\n", $errors);
    foreach ($lines as $line) {
        $class = '';
        if (strpos($line, 'error') !== false || strpos($line, 'Error') !== false) $class = 'error';
        elseif (strpos($line, 'warning') !== false || strpos($line, 'Warning') !== false) $class = 'warning';
        elseif (strpos($line, 'notice') !== false || strpos($line, 'Notice') !== false) $class = 'notice';
        echo '<div class="error-line ' . $class . '">' . htmlspecialchars($line) . '</div>';
    }
}
?>
        </pre>
    </div>
    
    <!-- Test Form to Simulate Bid -->
    <div class="test-form">
        <h3>🧪 Test Bid Submission (Debug)</h3>
        <form method="POST" action="product/detail.php?id=2">
            <input type="hidden" name="place_bid" value="1">
            <input type="number" name="bid_amount" placeholder="Bid Amount" value="80000">
            <input type="text" name="email" placeholder="Email" value="test@test.com">
            <input type="text" name="full_name" placeholder="Full Name" value="Test User">
            <input type="text" name="phone" placeholder="Phone" value="1234567890">
            <select name="payment_method" required>
                <option value="Credit Card">Credit Card</option>
                <option value="Bank Transfer">Bank Transfer</option>
            </select>
            <input type="text" name="card_name" placeholder="Card Name" value="Test Card">
            <input type="text" name="card_number" placeholder="Card Number" value="4111111111111111">
            <input type="text" name="expiry" placeholder="MM/YY" value="12/28">
            <input type="text" name="cvc" placeholder="CVC" value="123">
            <input type="text" name="zip_code" placeholder="Zip" value="90210">
            <button type="submit">🔨 Test Place Bid</button>
        </form>
    </div>
    
    <div class="info">
        <h3>📝 How to Debug:</h3>
        <ol>
            <li>1. Go to <strong>product/detail.php?id=2</strong></li>
            <li>2. Fill in the bid form and click "Place Bid"</li>
            <li>3. Come back to <strong>error_log.php</strong> and refresh</li>
            <li>4. Look for any red error lines</li>
        </ol>
        <p><strong>Common issues:</strong></p>
        <ul>
            <li>❌ Database connection failed</li>
            <li>❌ Table 'bids' doesn't exist</li>
            <li>❌ Foreign key constraint fails (user_id not found)</li>
            <li>❌ Missing required POST field</li>
            <li>❌ Syntax error in SQL query</li>
        </ul>
    </div>
</body>
<script>
function clearLogs() {
    fetch('clear_errors.php')
        .then(response => response.text())
        .then(data => { alert(data); location.reload(); });
}
function testError() {
    fetch('test_error.php')
        .then(response => response.text())
        .then(data => { alert(data); location.reload(); });
}
</script>
</html>