<?php

$coin = getdbosql('db_coins', "symbol='VEX'");
if (!$coin) {
	echo '<section class="vexon-telemetry-card"><div class="vexon-empty-state">VEX coin configuration is not ready yet.</div></section>';
	return;
}

$count = getparam('count');
$count = $count ? min(100, intval($count)) : 12;
$rows = getdbolist('db_blocks', "coin_id={$coin->id} ORDER BY id DESC LIMIT $count");

echo <<<END
<section class="vexon-telemetry-card">
	<div class="vexon-card-heading">
		<div>
			<span class="vexon-card-kicker">Recent rewards</span>
			<h2>Vexon Blocks</h2>
		</div>
		<img src="/vexon/ui/images/vexon-logo.png" alt="Vexon">
	</div>
END;

if (empty($rows)) {
	echo '<div class="vexon-empty-state">Waiting for the first VEX block. New, immature, confirmed, and rejected blocks will appear here.</div>';
	echo '</section>';
	return;
}

echo '<table class="vexon-stat-table">';
echo '<thead><tr><th>Height</th><th>Reward</th><th>Difficulty</th><th>Found</th><th>Status</th><th>Hash</th></tr></thead><tbody>';

foreach ($rows as $block) {
	$height = intval($block->height);
	$reward = bitcoinvaluetoa($block->amount).' VEX';
	$difficulty = Itoa2($block->difficulty, 3);
	$found = $block->time ? datetoa2($block->time).' ago' : '-';
	$status = htmlspecialchars($block->category, ENT_QUOTES, 'UTF-8');
	$hash = htmlspecialchars(substr($block->blockhash, 0, 14), ENT_QUOTES, 'UTF-8');
	$hashCell = $block->blockhash ? '<a href="/explorer/block?hash='.htmlspecialchars($block->blockhash, ENT_QUOTES, 'UTF-8').'">'.$hash.'...</a>' : '-';

	echo "<tr><td>{$height}</td><td>{$reward}</td><td>{$difficulty}</td><td>{$found}</td><td><span class=\"vexon-status-pill\">{$status}</span></td><td>{$hashCell}</td></tr>";
}

echo '</tbody></table>';
echo '</section>';
