<?php
	$colors = json_decode($user_map_custom);
	$_hex = function($c) { return preg_match('/^#[0-9a-fA-F]{6}$/', $c ?? '') ? $c : '#000000'; };
	$_qsoconfirm = $_hex($colors->qsoconfirm->color ?? '');
	$_qso = $_hex($colors->qso->color ?? '');
	?>
	<style>
		.awardsBgSuccess {
			background-color: <?php echo $_qsoconfirm; ?> !important;
		}
		.awardsBgDanger {
			background-color: <?php echo $_qso; ?> !important;
		}
	</style>

<?php if (isset($vuccdxcc) && is_array($vuccdxcc) && count($vuccdxcc) > 0) {
	echo '<div class="alert alert-success w-auto d-inline-block" role="alert">';
	echo '<div style="display: flex; align-items: center; gap: 10px;">';
	echo '<span class="fas fa-info-circle fa-2x"></span>';
	echo '<div><strong>' . __("This gridsquare exists in the following DXCC(s):") . '</strong><br>';
	echo implode(', ', $vuccdxcc) . '</div>';
	echo '</div>';
	echo '</div>';
} ?>

<?php
	echo '<div class="text-center">';
	echo __('Legend:');
	echo '<pre>'.__("(Q)SL-Paper-Card").", ";
	echo __("(L)oTW").", ";
	echo __("(e)QSL").", ";
	echo __('QR(Z)-"confirmation"').", ";
	echo __("(C)lublog").", ";
	echo __("(W)orked").'</pre>';
	echo '</div>';
?>

<?php echo '
    <table style="width:100%" class="table-sm table table-bordered table-hover table-striped table-condensed text-center">
	    <thead>
			<tr>
				<th></th>';
			foreach($bands as $band) {
				echo '<th>' . $band . '</th>';
			}
    echo '</tr>
		</thead>
		<tbody>';
foreach ($result as $mode => $value) {

	$showRow = true;
		if ($reduced_mode) {
			$showRow = false;
			foreach ($value as $val) {
				if ($val != '-') {
					$showRow = true;
					break;
				}
			}
		}
	if (strtoupper($mode) == strtoupper($current_mode)) {
		$showRow = true;
	}

	if ($showRow) {
		echo '<tr><td>' . strtoupper($mode) . '</td>';

		$typeMapping = [
			'dxcc' => $dxcc,
			'iota' => $iota,
			'vucc' => substr(trim($grid), 0, 4),
			'cq' => $cqz,
			'was' => $was,
			'sota' => $sota,
			'wwff' => $wwff,
			'itu' => $ituz,
			'continent' => $continent,
			'pota' => $pota,
			'dxcc2' => $dxcc,
			'dok' => $dok
		];

		if ($type == 'dxcc') {
			$type = 'dxcc2';
		}

		foreach ($value as $key => $val) {
			$searchPhrase = isset($typeMapping[$type]) ? str_replace("&", "%26", $typeMapping[$type]) : '';

			$qslParam = ($val == 'W' || $val == '-') ? '' : ",\"$val\"";

			$linkinfo = $searchPhrase
				? "<a href='javascript:displayContacts(\"$searchPhrase\",\"$key\",\"All\",\"All\",\"$mode\",\"" . strtoupper($type) . "\"$qslParam)'>$val</a>"
				: $val;

			$tdClass = ($current_band == $key && strtoupper($current_mode) == strtoupper($mode))
				? "class='border-3 border-danger'"
				: '';

			$content = $val;
			if ($val == 'W') {
				$content = "<div class='bg-danger awardsBgDanger'>$linkinfo</div>";
			} elseif ($val != '-') {
				$content = "<div class='bg-success awardsBgSuccess'>$linkinfo</div>";
			}

			echo "<td $tdClass>$content</td>";
		}

		echo '</tr>';
	}
}
echo '</tbody></table>';
?>
