<?php
// MJ Marketplace System Test - v3.0.4.2.8
@ini_set('display_errors','0');
@ini_set('log_errors','1');
function h($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
$root = dirname(__DIR__);
$configFile = $root . '/includes/config.php';
$pdo = null; $errors = [];
try {
    if (file_exists($configFile)) {
        $cfg = include $configFile;
        if (is_array($cfg)) {
            $pdo = new PDO('mysql:host='.$cfg['db_host'].';dbname='.$cfg['db_name'].';charset=utf8mb4', $cfg['db_user'], $cfg['db_pass'], [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC]);
        }
    }
} catch (Throwable $e) { $errors[] = $e->getMessage(); }
function table_count($pdo,$t){ try { return (int)$pdo->query('SELECT COUNT(*) FROM `'.str_replace('`','',$t).'`')->fetchColumn(); } catch (Throwable $e) { return false; } }
function col_exists($pdo,$t,$c){ try { $s=$pdo->prepare('SHOW COLUMNS FROM `'.str_replace('`','',$t).'` LIKE ?'); $s->execute([$c]); return (bool)$s->fetch(); } catch(Throwable $e){ return false; } }
$requiredTables = ['mj_users','mj_admin_users','mj_categories','mj_listings','mj_companies','mj_deals','seller_leads','mj_seller_leads','seller_lead_activities','mj_notifications','mj_settings','mj_orders','mj_payments','mj_wallets','mj_admin_review_queue','mj_schema_migrations','mj_form_duplicate_guard','mj_form_submission_log','mj_security_events','mj_location_cache','mj_error_logs','mj_auto_repair_logs','mj_page_cache','mj_count_cache','mj_performance_log'];
$importantFiles = ['index.php','login.php','register.php','dashboard.php','my-leads.php','leads.php','admin/leads.php','admin/dashboard.php','admin/review-queue.php','admin/system-test.php','install/index.php','install/update.php','assets/css/mj-v304.css','assets/css/mj-v30427-forms-location.css','assets/js/mj-v30427-form-ux.js','api/location-fast.php','admin/auto-repair.php','admin/error-logs.php','includes/mj_auto_repair.php','assets/css/mj-v30428-popup-speed.css','assets/js/mj-v30428-popup-speed.js'];
$folders = ['uploads','assets','includes','install/runtime'];
?><!doctype html><html><head><meta charset="utf-8"><title>MJ Marketplace System Test</title><meta name="viewport" content="width=device-width,initial-scale=1"><style>
body{font-family:Arial,sans-serif;background:#f6f8fb;margin:0;color:#1f2937}.wrap{max-width:1180px;margin:0 auto;padding:24px}.card{background:#fff;border:1px solid #e5e7eb;border-radius:14px;padding:18px;margin-bottom:16px;box-shadow:0 8px 20px rgba(0,0,0,.04)}h1{margin:0 0 8px}.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:12px}.ok{color:#047857}.bad{color:#b91c1c}.warn{color:#b45309}.pill{display:inline-block;padding:4px 9px;border-radius:999px;font-size:12px;background:#eef2ff}table{width:100%;border-collapse:collapse}td,th{padding:9px;border-bottom:1px solid #eee;text-align:left}th{background:#f9fafb}.btn{display:inline-block;background:#111827;color:#fff;text-decoration:none;padding:9px 13px;border-radius:8px;margin-right:8px}.muted{color:#6b7280}
</style></head><body><div class="wrap"><h1>MJ Marketplace System Test</h1><p class="muted">v3.0.4.2.8 full audit, popup UX, auto repair and debug check. This page does not delete or change data.</p>
<div class="card"><h2>Database Connection</h2><?php if($pdo): ?><p class="ok">OK - database connected.</p><?php else: ?><p class="bad">Database not connected.</p><?php foreach($errors as $e) echo '<p class="bad">'.h($e).'</p>'; endif; ?></div>
<?php if($pdo): ?><div class="card"><h2>Required Tables</h2><table><tr><th>Table</th><th>Status</th><th>Rows</th></tr><?php foreach($requiredTables as $t): $c=table_count($pdo,$t); ?><tr><td><?=h($t)?></td><td><?=($c!==false?'<span class="ok">OK</span>':'<span class="bad">Missing/Error</span>')?></td><td><?=h($c===false?'-':$c)?></td></tr><?php endforeach; ?></table></div>
<div class="card"><h2>Important Columns</h2><table><tr><th>Check</th><th>Status</th></tr><?php $cols=[['seller_leads','seller_user_id'],['seller_leads','source_type'],['mj_seller_leads','seller_user_id'],['mj_seller_leads','customer_email'],['mj_orders','payment_status'],['mj_companies','verification_status'],['mj_notifications','is_read'],['mj_form_duplicate_guard','guard_hash'],['mj_form_submission_log','form_key'],['mj_security_events','event_type'],['mj_location_cache','mj_error_logs','mj_auto_repair_logs','mj_page_cache','mj_count_cache','mj_performance_log','cache_key']]; foreach($cols as $cc): ?><tr><td><?=h($cc[0].'.'.$cc[1])?></td><td><?=(col_exists($pdo,$cc[0],$cc[1])?'<span class="ok">OK</span>':'<span class="bad">Missing</span>')?></td></tr><?php endforeach; ?></table></div><?php endif; ?>
<div class="card"><h2>Important Page Files</h2><table><tr><th>File</th><th>Status</th></tr><?php foreach($importantFiles as $f): $ok=file_exists($root.'/'.$f); ?><tr><td><?=h($f)?></td><td><?=$ok?'<span class="ok">OK</span>':'<span class="bad">Missing</span>'?></td></tr><?php endforeach; ?></table></div>
<div class="card"><h2>Writable Folders</h2><table><tr><th>Folder</th><th>Status</th></tr><?php foreach($folders as $f): $path=$root.'/'.$f; $exists=is_dir($path); $w=$exists && is_writable($path); ?><tr><td><?=h($f)?></td><td><?=$w?'<span class="ok">Writable</span>':($exists?'<span class="warn">Exists, not writable</span>':'<span class="bad">Missing</span>')?></td></tr><?php endforeach; ?></table></div>
<div class="card"><h2>Useful Links</h2><a class="btn" href="../">Frontend</a><a class="btn" href="dashboard.php">Admin Dashboard</a><a class="btn" href="leads.php">Admin Leads</a><a class="btn" href="../my-leads.php">User Leads</a><a class="btn" href="../install/update.php">Updater</a></div>
</div></body></html>
