<?php

if (!$coin) return;

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

$tx = vexon_explorer_rpc($remote, 'getrawtransaction', array($txhash, 1));
if (!$tx || !is_array($tx))
	$tx = vexon_explorer_rpc($remote, 'gettransaction', array($txhash));

echo '<section class="vexon-page-title">';
echo '<div><p class="vexon-kicker">Explorer</p><h1>Transaction Details</h1></div>';
echo '<p>Inspect VEX transaction metadata, inputs, outputs, and payout destinations.</p>';
echo '</section>';

if (!$tx || !is_array($tx)) {
	echo '<section class="vexon-telemetry-card">';
	echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Transaction lookup</span><h2>Transaction not found</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
	echo '<div class="vexon-empty-state">The local Vexon node has not indexed this transaction yet, or the transaction hash is incorrect.</div>';
	echo '<p class="vexon-hash vexon-muted">Requested: '.vexon_explorer_h($txhash).'</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 transaction hash, block hash, height, or address">';
	echo '<input type="submit" value="Search Explorer" class="main-submit-button">';
	echo '</form>';
	echo '</section>';
	return;
}

$txid = arraySafeVal($tx, 'txid', $txhash);
$blockHash = arraySafeVal($tx, 'blockhash', '');
$confirmations = intval(arraySafeVal($tx, 'confirmations', 0));
$blockTime = arraySafeVal($tx, 'blocktime', arraySafeVal($tx, 'time', 0));
$vins = arraySafeVal($tx, 'vin', array());
if (!is_array($vins)) $vins = array();
$outs = vexon_explorer_tx_outputs($tx);
$outputs = $outs['outputs'];
$totalOutput = $outs['total'];

echo '<section class="vexon-telemetry-card">';
echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Transaction</span><h2>'.vexon_explorer_h(vexon_explorer_short($txid, 18, 14)).'</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
echo '<div class="vexon-metric-grid vexon-metric-grid-five">';
echo '<div><span>Confirmations</span><strong>'.$confirmations.'</strong></div>';
echo '<div><span>Inputs</span><strong>'.count($vins).'</strong></div>';
echo '<div><span>Outputs</span><strong>'.count($outputs).'</strong></div>';
echo '<div><span>Total output</span><strong>'.vexon_explorer_amount($totalOutput, 8).' VEX</strong></div>';
echo '<div><span>Time</span><strong>'.vexon_explorer_h(vexon_explorer_time($blockTime)).'</strong></div>';
echo '</div>';

echo '<div class="vexon-detail-grid">';
echo '<div><span>Transaction hash</span><code>'.vexon_explorer_h($txid).'</code></div>';
if ($blockHash)
	echo '<div><span>Block hash</span><code><a href="/explorer?hash='.vexon_explorer_h($blockHash).'">'.vexon_explorer_h($blockHash).'</a></code></div>';
echo '<div><span>Version</span><code>'.vexon_explorer_h(arraySafeVal($tx, 'version', '')) .'</code></div>';
echo '<div><span>Lock time</span><code>'.vexon_explorer_h(arraySafeVal($tx, 'locktime', 0)).'</code></div>';
echo '</div>';
echo '</section>';

echo '<section class="vexon-explorer-grid">';
echo '<div class="vexon-telemetry-card">';
echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Inputs</span><h2>Sources</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
if (!count($vins)) {
	echo '<div class="vexon-empty-state">No input data returned by the node.</div>';
} else {
	echo '<div class="vexon-table-scroll"><table class="vexon-stat-table vexon-explorer-table"><thead><tr><th>#</th><th>Source</th><th>Amount</th></tr></thead><tbody>';
	foreach($vins as $i=>$vin) {
		$source = isset($vin['coinbase']) ? 'Generation' : arraySafeVal($vin, 'txid', 'Unknown');
		$amount = arraySafeVal($vin, 'amountin', '');
		echo '<tr>';
		echo '<td>'.($i+1).'</td>';
		echo '<td class="vexon-hash">'.vexon_explorer_h($source).'</td>';
		echo '<td>'.($amount === '' ? 'Waiting' : vexon_explorer_amount($amount, 8).' VEX').'</td>';
		echo '</tr>';
	}
	echo '</tbody></table></div>';
}
echo '</div>';

echo '<div class="vexon-telemetry-card">';
echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Outputs</span><h2>Destinations</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
if (!count($outputs)) {
	echo '<div class="vexon-empty-state">No spendable outputs were returned by the node.</div>';
} else {
	echo '<div class="vexon-table-scroll"><table class="vexon-stat-table vexon-explorer-table"><thead><tr><th>#</th><th>Address</th><th>Value</th><th>Type</th></tr></thead><tbody>';
	foreach($outputs as $i=>$out) {
		$addresses = arraySafeVal($out, 'addresses', array());
		$addressText = count($addresses) ? implode(', ', $addresses) : 'Script output';
		echo '<tr>';
		echo '<td>'.($i+1).'</td>';
		echo '<td class="vexon-hash">';
		if (count($addresses) == 1)
			echo '<a href="/explorer?address='.vexon_explorer_h($addresses[0]).'">'.vexon_explorer_h($addresses[0]).'</a>';
		else
			echo vexon_explorer_h($addressText);
		echo '</td>';
		echo '<td>'.vexon_explorer_amount(arraySafeVal($out, 'value', 0), 8).' VEX</td>';
		echo '<td>'.vexon_explorer_h(arraySafeVal($out, 'type', '')).'</td>';
		echo '</tr>';
	}
	echo '</tbody></table></div>';
}
echo '</div>';
echo '</section>';
