<?php

if (!$coin) return;

$this->pageTitle = $coin->name.' block explorer';
$logo = '/vexon/ui/images/vexon-logo.png';
$remote = new WalletRPC($coin);

$block = isset($block) && is_array($block) ? $block : false;
if (!$block && !empty($hash))
	$block = vexon_explorer_rpc($remote, 'getblock', array($hash));

$requested = !empty($hash) ? $hash : (isset($height) ? 'height '.$height : 'unknown block');

echo '<section class="vexon-page-title">';
echo '<div><p class="vexon-kicker">Explorer</p><h1>Block Details</h1></div>';
echo '<p>Inspect VEX block metadata, proof-of-work fields, and included transactions.</p>';
echo '</section>';

if (!$block || !is_array($block)) {
	echo '<section class="vexon-telemetry-card">';
	echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Block lookup</span><h2>Block not found</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
	echo '<div class="vexon-empty-state">'.vexon_explorer_h(isset($error) ? $error : 'The Vexon node did not return this block yet.').'</div>';
	echo '<p class="vexon-hash vexon-muted">Requested: '.vexon_explorer_h($requested).'</p>';
	echo '<form class="vexon-explorer-search" action="/explorer/search" method="POST">';
	echo '<input type="text" name="q" class="main-text-input" placeholder="Try another height, hash, txid, or VEX address">';
	echo '<input type="submit" value="Search Explorer" class="main-submit-button">';
	echo '</form>';
	echo '</section>';
	return;
}

$hash = arraySafeVal($block, 'hash', $hash);
$blockHeight = intval(arraySafeVal($block, 'height', 0));
$txs = arraySafeVal($block, 'tx', array());
if (!is_array($txs)) $txs = array();
$txCount = count($txs);
$prevHash = arraySafeVal($block, 'previousblockhash', '');
$nextHash = arraySafeVal($block, 'nextblockhash', '');

echo '<section class="vexon-telemetry-card">';
echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Block #'.$blockHeight.'</span><h2>'.vexon_explorer_h(vexon_explorer_short($hash, 18, 14)).'</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
echo '<div class="vexon-metric-grid vexon-metric-grid-five">';
echo '<div><span>Height</span><strong>'.$blockHeight.'</strong></div>';
echo '<div><span>Transactions</span><strong>'.$txCount.'</strong></div>';
echo '<div><span>Confirmations</span><strong>'.intval(arraySafeVal($block, 'confirmations', 0)).'</strong></div>';
echo '<div><span>Difficulty</span><strong>'.vexon_explorer_h(vexon_explorer_metric(arraySafeVal($block, 'difficulty', 0), 4)).'</strong></div>';
echo '<div><span>Size</span><strong>'.intval(arraySafeVal($block, 'size', 0)).' B</strong></div>';
echo '</div>';

echo '<div class="vexon-detail-grid">';
echo '<div><span>Block hash</span><code>'.vexon_explorer_h($hash).'</code></div>';
echo '<div><span>Merkle root</span><code>'.vexon_explorer_h(arraySafeVal($block, 'merkleroot', '')) .'</code></div>';
echo '<div><span>Bits</span><code>'.vexon_explorer_h(arraySafeVal($block, 'bits', '')).'</code></div>';
echo '<div><span>Nonce</span><code>'.vexon_explorer_h(arraySafeVal($block, 'nonce', '')).'</code></div>';
echo '<div><span>Version</span><code>'.vexon_explorer_h(arraySafeVal($block, 'version', '')).'</code></div>';
echo '<div><span>Time</span><code>'.vexon_explorer_h(vexon_explorer_time(arraySafeVal($block, 'time', 0))).'</code></div>';
if ($prevHash)
	echo '<div><span>Previous block</span><code><a href="/explorer?hash='.vexon_explorer_h($prevHash).'">'.vexon_explorer_h(vexon_explorer_short($prevHash, 18, 12)).'</a></code></div>';
if ($nextHash)
	echo '<div><span>Next block</span><code><a href="/explorer?hash='.vexon_explorer_h($nextHash).'">'.vexon_explorer_h(vexon_explorer_short($nextHash, 18, 12)).'</a></code></div>';
echo '</div>';
echo '</section>';

echo '<section class="vexon-telemetry-card">';
echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Block payload</span><h2>Transactions</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
if (!$txCount) {
	echo '<div class="vexon-empty-state">This block has no transaction list available from the local node.</div>';
	echo '</section>';
	return;
}

echo '<div class="vexon-table-scroll"><table class="vexon-stat-table vexon-explorer-table"><thead><tr>';
echo '<th>#</th><th>Transaction</th><th>Outputs</th><th>Total output</th><th>Coinbase</th>';
echo '</tr></thead><tbody>';

$n = 0;
foreach($txs as $txhash) {
	$n++;
	$tx = vexon_explorer_rpc($remote, 'getrawtransaction', array($txhash, 1));
	$outputCount = '';
	$totalOutput = '';
	$isCoinbase = '';
	if (is_array($tx)) {
		$outs = vexon_explorer_tx_outputs($tx);
		$outputCount = count($outs['outputs']);
		$totalOutput = vexon_explorer_amount($outs['total'], 8).' VEX';
		$vins = arraySafeVal($tx, 'vin', array());
		if (is_array($vins)) {
			foreach($vins as $vin) {
				if (isset($vin['coinbase'])) {
					$isCoinbase = 'Generation';
					break;
				}
			}
		}
	}

	echo '<tr>';
	echo '<td>'.$n.'</td>';
	echo '<td><a class="vexon-hash" href="/explorer?txid='.vexon_explorer_h($txhash).'">'.vexon_explorer_h($txhash).'</a></td>';
	echo '<td>'.vexon_explorer_h($outputCount === '' ? 'Indexed on demand' : $outputCount).'</td>';
	echo '<td>'.vexon_explorer_h($totalOutput === '' ? 'Waiting' : $totalOutput).'</td>';
	echo '<td>'.vexon_explorer_h($isCoinbase).'</td>';
	echo '</tr>';
}

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