<?php include __DIR__ . '/../includes/header.php'; ?>

<?php
$ranking_pages = [
		1 => [
				['rank' => 1, 'nickname' => 'FX336408...', 'asset' => '716,658.52', 'pnl' => '-4,990.61', 'pnl_class' => 'neg', 'win_rate' => '70.00%', 'yield' => '-0.69%', 'yield_class' => 'neg', 'broker' => 'Blackwell Global', 'broker_key' => 'blackwell', 'location' => 'Beijing', 'trend' => 'up', 'logo' => ''],
				['rank' => 2, 'nickname' => 'FX546508...', 'asset' => '690,561.4', 'pnl' => '+2,133.6', 'pnl_class' => 'pos', 'win_rate' => '74.29%', 'yield' => '0.31%', 'yield_class' => 'pos', 'broker' => 'Markets4you', 'broker_key' => 'markets4you', 'location' => 'Ho Chi Ming City', 'trend' => 'down', 'logo' => ''],
				['rank' => 3, 'nickname' => 'FX466676...', 'asset' => '575,326.8', 'pnl' => '+15,314', 'pnl_class' => 'pos', 'win_rate' => '83.33%', 'yield' => '2.73%', 'yield_class' => 'pos', 'broker' => 'Menara Mas Futures', 'broker_key' => 'menara', 'location' => 'Hong Kong', 'trend' => 'up', 'logo' => ''],
				['rank' => 4, 'nickname' => 'FX364985...', 'asset' => '510,001.56', 'pnl' => '+2,938.83', 'pnl_class' => 'pos', 'win_rate' => '66.69%', 'yield' => '0.58%', 'yield_class' => 'pos', 'broker' => 'IFS Markets', 'broker_key' => 'ifs', 'location' => 'Hong Kong', 'trend' => 'down', 'logo' => ''],
				['rank' => 5, 'nickname' => 'FX176386...', 'asset' => '320,770.1', 'pnl' => '+669.87', 'pnl_class' => 'pos', 'win_rate' => '90.00%', 'yield' => '0.21%', 'yield_class' => 'pos', 'broker' => 'Markets4you', 'broker_key' => 'markets4you', 'location' => 'Singapore', 'trend' => 'up', 'logo' => ''],
				['rank' => 6, 'nickname' => 'FX102997...', 'asset' => '300,284.66', 'pnl' => '-9,483.02', 'pnl_class' => 'neg', 'win_rate' => '56.11%', 'yield' => '-3.06%', 'yield_class' => 'neg', 'broker' => 'BCR', 'broker_key' => 'bcr', 'location' => 'Hong Kong', 'trend' => 'up', 'logo' => ''],
				['rank' => 7, 'nickname' => 'FX546508...', 'asset' => '280,157.38', 'pnl' => '+1,287.63', 'pnl_class' => 'pos', 'win_rate' => '68.00%', 'yield' => '0.46%', 'yield_class' => 'pos', 'broker' => 'Markets4you', 'broker_key' => 'markets4you', 'location' => 'Ho Chi Ming City', 'trend' => 'up', 'logo' => ''],
				['rank' => 8, 'nickname' => 'FX233000...', 'asset' => '267,229.39', 'pnl' => '+5,409.15', 'pnl_class' => 'pos', 'win_rate' => '68.17%', 'yield' => '2.07%', 'yield_class' => 'pos', 'broker' => 'Ultima', 'broker_key' => 'ultima', 'location' => 'Taipei', 'trend' => 'down', 'logo' => ''],
				['rank' => 9, 'nickname' => 'FX176386...', 'asset' => '266,774.35', 'pnl' => '+1,286.45', 'pnl_class' => 'pos', 'win_rate' => '80.65%', 'yield' => '0.48%', 'yield_class' => 'pos', 'broker' => 'Markets4you', 'broker_key' => 'markets4you', 'location' => 'Singapore', 'trend' => 'up', 'logo' => ''],
				['rank' => 10, 'nickname' => 'FX383702...', 'asset' => '251,008.66', 'pnl' => '+832.77', 'pnl_class' => 'pos', 'win_rate' => '60.27%', 'yield' => '0.33%', 'yield_class' => 'pos', 'broker' => 'squaredfinancial', 'broker_key' => 'squared', 'location' => 'Taipei', 'trend' => 'up', 'logo' => ''],
		],
		2 => [
				['rank' => 1, 'nickname' => 'FX387303...', 'asset' => '242,984.88', 'pnl' => '+3,909.67', 'pnl_class' => 'pos', 'win_rate' => '82.04%', 'yield' => '1.64%', 'yield_class' => 'pos', 'broker' => 'MAGIC COMPASS', 'broker_key' => 'magic', 'location' => 'Beijing', 'trend' => 'up', 'logo' => ''],
				['rank' => 2, 'nickname' => 'FX118587...', 'asset' => '236,055.9', 'pnl' => '+19,034.83', 'pnl_class' => 'pos', 'win_rate' => '78.68%', 'yield' => '8.77%', 'yield_class' => 'pos', 'broker' => 'MONETA MARKETS', 'broker_key' => 'moneta', 'location' => 'Hong Kong', 'trend' => 'down', 'logo' => ''],
				['rank' => 3, 'nickname' => 'FX135616...', 'asset' => '229,228.62', 'pnl' => '-857.09', 'pnl_class' => 'neg', 'win_rate' => '72.37%', 'yield' => '-0.37%', 'yield_class' => 'neg', 'broker' => 'EBC FINANCIAL GROUP', 'broker_key' => 'ebc', 'location' => 'Beijing', 'trend' => 'up', 'logo' => ''],
				['rank' => 4, 'nickname' => 'FX114831...', 'asset' => '228,918.63', 'pnl' => '+5,064.22', 'pnl_class' => 'pos', 'win_rate' => '66.67%', 'yield' => '2.26%', 'yield_class' => 'pos', 'broker' => 'LiteForex', 'broker_key' => 'liteforex', 'location' => 'Dubai', 'trend' => 'down', 'logo' => ''],
				['rank' => 5, 'nickname' => 'FX176386...', 'asset' => '214,393.69', 'pnl' => '+1,237.51', 'pnl_class' => 'pos', 'win_rate' => '84.09%', 'yield' => '0.58%', 'yield_class' => 'pos', 'broker' => 'Markets4you', 'broker_key' => 'markets4you', 'location' => 'Singapore', 'trend' => 'up', 'logo' => ''],
				['rank' => 6, 'nickname' => 'FX423953...', 'asset' => '197,957.92', 'pnl' => '-75.24', 'pnl_class' => 'neg', 'win_rate' => '71.70%', 'yield' => '-0.04%', 'yield_class' => 'neg', 'broker' => 'EBC FINANCIAL GROUP', 'broker_key' => 'ebc', 'location' => 'Beijing', 'trend' => 'up', 'logo' => ''],
				['rank' => 7, 'nickname' => 'FX423953...', 'asset' => '173,334.75', 'pnl' => '-95.1', 'pnl_class' => 'neg', 'win_rate' => '71.30%', 'yield' => '-0.05%', 'yield_class' => 'neg', 'broker' => 'EBC FINANCIAL GROUP', 'broker_key' => 'ebc', 'location' => 'Beijing', 'trend' => 'down', 'logo' => ''],
				['rank' => 8, 'nickname' => 'FX128098...', 'asset' => '169,209.09', 'pnl' => '+648.62', 'pnl_class' => 'pos', 'win_rate' => '100.00%', 'yield' => '0.38%', 'yield_class' => 'pos', 'broker' => 'darwinex', 'broker_key' => 'darwinex', 'location' => 'Dubai', 'trend' => 'up', 'logo' => ''],
				['rank' => 9, 'nickname' => 'FX321604...', 'asset' => '164,240.55', 'pnl' => '+13,048.95', 'pnl_class' => 'pos', 'win_rate' => '62.63%', 'yield' => '8.63%', 'yield_class' => 'pos', 'broker' => 'AXEL', 'broker_key' => 'axel', 'location' => 'Shanghai', 'trend' => 'down', 'logo' => ''],
				['rank' => 10, 'nickname' => 'FX173208...', 'asset' => '155,224.7', 'pnl' => '-92,433.4', 'pnl_class' => 'neg', 'win_rate' => '72.30%', 'yield' => '-37.32%', 'yield_class' => 'neg', 'broker' => 'IFS Markets', 'broker_key' => 'ifs', 'location' => 'Virginia', 'trend' => 'up', 'logo' => ''],
		],
];

$broker_logo_map = [
		'xm' => '../assets/images/ea/xm.png',
		'ec' => '../assets/images/ea/ec.png',
		'exness' => '../assets/images/ea/exness.png',
		'fbs' => '../assets/images/ea/fbs.png',
		'ic' => '../assets/images/ea/ic.png',
		'lirunex' => '../assets/images/ea/lirunex.png',
		'robo' => '../assets/images/ea/robo.png',
		'tmgm' => '../assets/images/ea/tmgm.png',
		'vantage' => '../assets/images/ea/vantage.png',
		'all' => '../assets/images/ea/all.png',
];

function ea_logo_path(array $broker_logo_map, string $broker_key, string $broker_name): string
{
		if (isset($broker_logo_map[$broker_key])) {
				return $broker_logo_map[$broker_key];
		}

		$normalized = strtolower(preg_replace('/[^a-z0-9]+/', '', $broker_name));
		$candidate = __DIR__ . '/../assets/images/ea/' . $normalized . '.png';
		if (is_file($candidate)) {
				return '../assets/images/ea/' . $normalized . '.png';
		}

		return '';
}

$tab_labels = ['A week', 'Nearly one month', 'Past 3 Months', 'Last year', 'ALL'];
?>

<style>
	:root {
		--ea-bg: #f7f7f8;
		--ea-surface: #ffffff;
		--ea-surface-soft: #fbfbfc;
		--ea-line: #ececec;
		--ea-line-strong: #d7d7d7;
		--ea-text: #2d2f33;
		--ea-muted: #8a8f98;
		--ea-gold: #f6c44f;
		--ea-gold-soft: #fff3ce;
		--ea-blue: #6f85d8;
		--ea-green: #36a67a;
		--ea-red: #f05363;
		--ea-shadow: 0 1px 2px rgba(40, 44, 52, 0.03), 0 12px 30px rgba(40, 44, 52, 0.06);
	}

	body {
		background: var(--ea-bg);
		color: var(--ea-text);
	}

	.ea-page {
		max-width: 1380px;
		margin: 0 auto;
		padding: 18px 18px 56px;
	}

	.ea-breadcrumb {
		display: flex;
		align-items: center;
		gap: 8px;
		font-size: 14px;
		color: var(--ea-muted);
		margin: 2px 0 18px;
	}

	.ea-breadcrumb a {
		color: var(--ea-muted);
		text-decoration: none;
	}

	.ea-breadcrumb a:hover {
		color: var(--ea-text);
	}

	.ea-shell {
		background: var(--ea-surface);
		border: 1px solid var(--ea-line);
		box-shadow: var(--ea-shadow);
	}

	.ea-tabs {
		display: inline-flex;
		align-items: center;
		border: 1px solid var(--ea-line);
		background: var(--ea-surface);
		margin: 0 0 14px;
	}

	.ea-tab {
		appearance: none;
		border: 0;
		background: transparent;
		color: #b0b4bb;
		font-size: 14px;
		font-weight: 700;
		padding: 10px 24px;
		border-right: 1px solid var(--ea-line);
		cursor: pointer;
		transition: 0.2s ease;
	}

	.ea-tab:last-child {
		border-right: 0;
	}

	.ea-tab:hover {
		color: var(--ea-text);
		background: #fbfbfc;
	}

	.ea-tab.active {
		color: #222;
		background: #fff;
		box-shadow: inset 0 -2px 0 var(--ea-gold);
	}

	.ea-table-wrap {
		overflow-x: auto;
		background: var(--ea-surface);
	}

	.ea-table {
		width: 100%;
		min-width: 1100px;
		border-collapse: collapse;
	}

	.ea-table thead th {
		padding: 16px 14px;
		font-size: 12px;
		font-weight: 700;
		color: #a0a4ac;
		text-align: left;
		border-top: 1px solid #f3f3f3;
		border-bottom: 1px solid #9aa0a7;
		white-space: nowrap;
		background: #fff;
	}

	.ea-table th.center,
	.ea-table td.center {
		text-align: center;
	}

	.ea-table tbody td {
		padding: 17px 14px;
		border-bottom: 1px solid #f3f3f3;
		vertical-align: middle;
		font-size: 14px;
	}

	.ea-table tbody tr:hover td {
		background: #fcfcfd;
	}

	.ea-rank {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		width: 18px;
		height: 18px;
		border-radius: 4px;
		background: #f1f2f6;
		color: #838893;
		font-size: 11px;
		font-weight: 800;
	}

	.ea-rank.top {
		background: #ffe7ad;
		color: #85620e;
	}

	.ea-nickname {
		color: #8d9199;
		font-size: 13px;
		font-weight: 700;
	}

	.ea-num {
		font-size: 16px;
		font-weight: 700;
		letter-spacing: 0.01em;
	}

	.ea-num.neg,
	.ea-yield.neg {
		color: var(--ea-red);
	}

	.ea-num.pos,
	.ea-yield.pos {
		color: var(--ea-green);
	}

	.ea-broker {
		display: inline-flex;
		align-items: center;
		gap: 8px;
		font-weight: 700;
		color: #44474d;
		white-space: nowrap;
	}

	.ea-broker-logo {
		width: 16px;
		height: 16px;
		border-radius: 3px;
		border: 1px solid #e4e6ea;
		background: #fff;
		object-fit: contain;
		flex: 0 0 auto;
	}

	.ea-broker-fallback {
		width: 16px;
		height: 16px;
		border-radius: 3px;
		background: linear-gradient(135deg, #6078ea, #8f9ee7);
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 8px;
		font-weight: 800;
		letter-spacing: 0.02em;
	}

	.ea-location {
		display: inline-flex;
		align-items: center;
		gap: 6px;
		color: #575d66;
		font-weight: 500;
	}

	.ea-flag {
		width: 18px;
		height: 12px;
		border-radius: 1px;
		object-fit: cover;
		box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.04);
	}

	.ea-sort {
		display: inline-flex;
		flex-direction: column;
		margin-left: 6px;
		vertical-align: middle;
		line-height: 0.75;
		color: #c6c8ce;
		font-size: 8px;
	}

	.ea-trend {
		width: 72px;
		height: 28px;
		display: inline-block;
	}

	.ea-pagination {
		display: flex;
		justify-content: center;
		gap: 8px;
		margin: 24px 0 0;
	}

	.ea-page-btn {
		width: 34px;
		height: 34px;
		border: 1px solid #e4e4e7;
		background: #fff;
		color: #8d9199;
		border-radius: 2px;
		font-weight: 700;
		cursor: pointer;
		transition: 0.2s ease;
	}

	.ea-page-btn:hover {
		border-color: #d8d8dd;
		color: #333;
	}

	.ea-page-btn.active {
		background: var(--ea-gold);
		border-color: #efc14c;
		color: #222;
	}

	.ea-page-note {
		display: flex;
		justify-content: flex-end;
		gap: 6px;
		align-items: center;
		margin-top: 10px;
		font-size: 11px;
		color: #b0b4bb;
	}

	.ea-page-note strong {
		color: #8d9199;
	}

	.ea-sort-panel {
		display: flex;
		align-items: center;
		justify-content: space-between;
		gap: 16px;
		padding: 10px 0 16px;
	}

	.ea-empty-row td {
		padding: 28px 14px;
		text-align: center;
		color: #9aa0a7;
	}

	@media (max-width: 900px) {
		.ea-page {
			padding-left: 14px;
			padding-right: 14px;
		}

		.ea-tabs {
			width: 100%;
			overflow-x: auto;
		}
	}
</style>

<div class="ea-page">
	<div class="ea-breadcrumb">
		<a href="<?php echo $base_url; ?>/index.php">Home</a>
		<span>-</span>
		<a href="<?php echo $base_url; ?>/vps/vps.php">VPS</a>
		<span>-</span>
		<span>EA Rankings</span>
	</div>

	<div class="ea-sort-panel">
		<div class="ea-tabs" role="tablist" aria-label="EA rankings time ranges">
			<?php foreach ($tab_labels as $index => $label): ?>
				<button type="button" class="ea-tab <?= $index === 0 ? 'active' : '' ?>" data-tab="<?= $index + 1 ?>"><?= htmlspecialchars($label) ?></button>
			<?php endforeach; ?>
		</div>
	</div>

	<div class="ea-shell">
		<div class="ea-table-wrap">
			<table class="ea-table">
				<thead>
					<tr>
						<th> </th>
						<th>Nickname</th>
						<th>Asset(USD)<span class="ea-sort"><span>▲</span><span>▼</span></span></th>
						<th>Total PnL(USD)<span class="ea-sort"><span>▲</span><span>▼</span></span></th>
						<th class="center">Win Rate<span class="ea-sort"><span>▲</span><span>▼</span></span></th>
						<th class="center">Yield<span class="ea-sort"><span>▲</span><span>▼</span></span></th>
						<th>Brokers</th>
						<th>VPS location</th>
						<th class="center">Yield Trend</th>
					</tr>
				</thead>
				<tbody id="eaRankingBody"></tbody>
			</table>
		</div>
	</div>

	<div class="ea-pagination" id="eaPagination" aria-label="EA rankings pagination"></div>

	<div class="ea-page-note">
		<span>The data is provided by</span>
		<strong>Trader Safety Data</strong>
	</div>
</div>

<script>
	const eaRankingPages = <?= json_encode($ranking_pages, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ?>;
	let currentEaPage = 1;

	function eaFlagPath(location) {
		const key = String(location).toLowerCase();
		if (key.includes('beijing')) return 'assets/images/flags/cn.png';
		if (key.includes('hong kong')) return 'assets/images/flags/hk.png';
		if (key.includes('singapore')) return 'assets/images/flags/sg.png';
		if (key.includes('taipei')) return 'assets/images/flags/tw.png';
		if (key.includes('dubai')) return 'assets/images/flags/ae.png';
		if (key.includes('virginia')) return 'assets/images/flags/us.png';
		return '';
	}

	function trendMarkup(direction) {
		if (direction === 'down') {
			return '<svg class="ea-trend" viewBox="0 0 72 28" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 16 C12 16, 16 8, 22 8 S34 18, 40 18 S52 4, 58 4 S66 12, 68 12" stroke="#6f85d8" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/></svg>';
		}

		return '<svg class="ea-trend" viewBox="0 0 72 28" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 20 C10 12, 16 8, 22 8 S34 18, 40 14 S52 3, 58 6 S66 10, 68 8" stroke="#6f85d8" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/></svg>';
	}

	function brokerLogoMarkup(row) {
		const logo = row.logo || '';
		const initials = row.broker.split(' ').map(part => part[0]).join('').slice(0, 2).toUpperCase();
		if (logo) {
			return `<img class="ea-broker-logo" src="${logo}" alt="${row.broker}" onerror="this.replaceWith(Object.assign(document.createElement('span'), {className:'ea-broker-fallback', textContent:'${initials}'}))">`;
		}
		return `<span class="ea-broker-fallback">${initials}</span>`;
	}

	function setActiveTabs(activeIndex) {
		document.querySelectorAll('.ea-tab').forEach(button => {
			button.classList.toggle('active', String(button.dataset.tab) === String(activeIndex));
		});
	}

	function renderPage(pageNumber) {
		currentEaPage = String(pageNumber);
		const rows = eaRankingPages[pageNumber] || [];
		const tbody = document.getElementById('eaRankingBody');
		const pagination = document.getElementById('eaPagination');
		tbody.innerHTML = rows.map(row => {
			const flagPath = eaFlagPath(row.location);
			const rankClass = row.rank <= 3 ? 'ea-rank top' : 'ea-rank';

			return `
				<tr>
					<td class="center"><span class="${rankClass}">${row.rank}</span></td>
					<td><span class="ea-nickname">${row.nickname}</span></td>
					<td><span class="ea-num">${row.asset}</span></td>
					<td><span class="ea-num ${row.pnl_class}">${row.pnl}</span></td>
					<td class="center"><span class="ea-num">${row.win_rate}</span></td>
					<td class="center"><span class="ea-yield ${row.yield_class}">${row.yield}</span></td>
					<td><span class="ea-broker">${brokerLogoMarkup(row)}${row.broker}</span></td>
					<td><span class="ea-location">${flagPath ? `<img class="ea-flag" src="${flagPath}" alt="${row.location}">` : ''}${row.location}</span></td>
					<td class="center">${trendMarkup(row.trend)}</td>
				</tr>
			`;
		}).join('');

		pagination.innerHTML = Object.keys(eaRankingPages).map(key => `
			<button type="button" class="ea-page-btn ${String(key) === String(pageNumber) ? 'active' : ''}" data-page="${key}">${key}</button>
		`).join('');

		pagination.querySelectorAll('.ea-page-btn').forEach(button => {
			button.addEventListener('click', () => renderPage(button.dataset.page));
		});

		setActiveTabs(1);
	}

	document.querySelectorAll('.ea-tab').forEach(button => {
		button.addEventListener('click', () => setActiveTabs(button.dataset.tab));
	});

	renderPage(1);
</script>

<?php include __DIR__ . '/../includes/footer.php'; ?>
