<?php

$algo = VEXON_DEFAULT_ALGO;
user()->setState('vexon-algo', $algo);

JavascriptFile('/vexon/ui/js/auto_refresh.js');

$pool_url = htmlspecialchars(VEXON_STRATUM_URL, ENT_QUOTES, 'UTF-8');
$stratum_port = getAlgoPort($algo);
if (!$stratum_port) $stratum_port = 4499;

$fee = floatval(vexon_fee($algo));
$min_payout = floatval(VEXON_PAYMENTS_MINI);
$payout_label = htmlspecialchars(VEXON_PAYOUT_SCHEDULE_LABEL, ENT_QUOTES, 'UTF-8');
$payout_note = htmlspecialchars(VEXON_PAYOUT_NOTE, ENT_QUOTES, 'UTF-8');
$wallet_url = htmlspecialchars(VEXON_WALLET_URL, ENT_QUOTES, 'UTF-8');
$docs_url = htmlspecialchars(VEXON_DOCS_URL, ENT_QUOTES, 'UTF-8');

echo <<<END
<div id="resume_update_button" class="vexon-refresh-paused" onclick="auto_page_resume();" style="display: none;">
	Auto refresh is paused. Click to resume.
</div>

<section class="vexon-page-title">
	<div>
		<p class="vexon-kicker">VEX Mining</p>
		<h1>Mining Operations</h1>
	</div>
	<p>Connect Blake256r14 ASIC miners, watch accepted shares, and track VEX rewards from one clean dashboard.</p>
</section>

<section class="vexon-status-strip">
	<div><span>Mining endpoint</span><strong>{$pool_url}:{$stratum_port}</strong></div>
	<div><span>Algorithm</span><strong>Blake256r14</strong></div>
	<div><span>Minimum payout</span><strong>{$min_payout} VEX</strong></div>
	<div><span>Payout</span><strong>{$payout_label}</strong></div>
</section>

<section class="vexon-workbench">
	<div class="vexon-connect-panel">
		<div class="vexon-panel-label">ASIC setup</div>
		<h2>Miner configuration</h2>
		<div class="vexon-endpoint-row">
			<code id="vexon-mining-endpoint">stratum+tcp://{$pool_url}:{$stratum_port}</code>
			<button type="button" onclick="vexonCopyMiningEndpoint(this)">Copy</button>
		</div>
		<div class="vexon-connection-grid">
			<span>Username</span><strong>VEX_ADDRESS.worker</strong>
			<span>Password</span><strong>123</strong>
			<span>Pool fee</span><strong>{$fee}%</strong>
			<span>Worker tracking</span><strong>Use .001, .002, .D1</strong>
		</div>
	</div>

	<div class="vexon-payout-card">
		<div class="vexon-panel-label">Supported hardware</div>
		<h2>Blake256r14 ASIC profile</h2>
		<div class="vexon-hardware-list">
			<div><span>WhatsMiner D1</span><strong>40 TH/s</strong></div>
			<div><span>Antminer DR5</span><strong>35 TH/s</strong></div>
		</div>
		<p>For most firmware, start with password <b>123</b>. Use fixed difficulty only when your miner requires it.</p>
	</div>

	<div class="vexon-payout-card">
		<div class="vexon-panel-label">Payout rule</div>
		<h2>{$payout_label}</h2>
		<p>{$payout_note}</p>
		<p>Balances are paid when they reach at least <b>{$min_payout} VEX</b> and the related blocks are mature.</p>
		<a class="vexon-link-button" href="{$wallet_url}" target="_blank" rel="noopener">Open wallet</a>
		<a class="vexon-link-button" href="{$docs_url}" target="_blank" rel="noopener">Mining guide</a>
	</div>
</section>

<section class="vexon-mining-dashboard">
	<div class="vexon-mining-top">
		<div id="mining_results" class="vexon-live-box"><div class="vexon-loading">Loading Vexon mining data...</div></div>
		<aside class="vexon-side-panel vexon-mining-side">
		<div id="pool_current_results"><div class="vexon-loading">Loading pool status...</div></div>
		</aside>
	</div>
	<div class="vexon-mining-wide">
		<div id="found_results" class="vexon-live-box"><div class="vexon-loading">Loading Vexon block flow...</div></div>
	</div>
</section>

<script>
function page_refresh()
{
	pool_current_refresh();
	mining_refresh();
	found_refresh();
}

function pool_current_ready(data) { $('#pool_current_results').html(data); }
function pool_current_refresh() { $.get("/site/current_results", '', pool_current_ready); }

function mining_ready(data) { $('#mining_results').html(data); }
function mining_refresh() { $.get("/site/mining_results", '', mining_ready); }

function found_ready(data) { $('#found_results').html(data); }
function found_refresh() { $.get("/site/found_results", '', found_ready); }

function vexonSetCopyState(button, text)
{
	if (!button) return;
	var original = button.getAttribute('data-original-label') || button.textContent;
	button.setAttribute('data-original-label', original);
	button.textContent = text;
	window.clearTimeout(button.vexonCopyTimer);
	button.vexonCopyTimer = window.setTimeout(function() {
		button.textContent = original;
	}, 1800);
}

function vexonFallbackCopy(text)
{
	var area = document.createElement('textarea');
	area.value = text;
	area.setAttribute('readonly', 'readonly');
	area.style.position = 'fixed';
	area.style.left = '-9999px';
	document.body.appendChild(area);
	area.select();
	var ok = false;
	try {
		ok = document.execCommand('copy');
	} catch (e) {
		ok = false;
	}
	document.body.removeChild(area);
	return ok;
}

function vexonCopyText(text, button)
{
	if (navigator.clipboard && navigator.clipboard.writeText) {
		navigator.clipboard.writeText(text).then(function() {
			vexonSetCopyState(button, 'Copied');
		}).catch(function() {
			vexonSetCopyState(button, vexonFallbackCopy(text) ? 'Copied' : 'Copy failed');
		});
		return;
	}
	vexonSetCopyState(button, vexonFallbackCopy(text) ? 'Copied' : 'Copy failed');
}

function vexonCopyMiningEndpoint(button)
{
	var endpoint = document.getElementById('vexon-mining-endpoint');
	if (!endpoint) return;
	vexonCopyText(endpoint.textContent.trim(), button);
}
</script>
END;
