<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$rate_value = isset($exchange_rate['exchange_rate']) ? (float)$exchange_rate['exchange_rate'] : 0.0;
$rate_symbol = isset($exchange_rate['currency_symbol']) ? (string)$exchange_rate['currency_symbol'] : 'Bs';
$price_divisa_size = isset($user_preferences['price_divisa_size']) ? (float)$user_preferences['price_divisa_size'] : 4.10;
$price_bs_size = isset($user_preferences['price_bs_size']) ? (float)$user_preferences['price_bs_size'] : 1.45;
$current_price = isset($lookup_result['price_divisa']) ? (float)$lookup_result['price_divisa'] : 0.0;
$current_price_bs = $current_price * $rate_value;
$discount_divisa = isset($discount_percent_divisa) ? (float)$discount_percent_divisa : (isset($discount_percent) ? (float)$discount_percent : 0.0);
$discount_bs = isset($discount_percent_bs) ? (float)$discount_percent_bs : (isset($discount_percent) ? (float)$discount_percent : 0.0);
$current_price_discounted = $current_price - ($current_price * ($discount_divisa / 100));
$current_price_bs_discounted = $current_price_bs - ($current_price_bs * ($discount_bs / 100));
$history_total_divisa = 0.0;
$history_total_bs = 0.0;
$history_total_divisa_discounted = 0.0;
$history_total_bs_discounted = 0.0;
foreach ((array)$history as $history_item)
{
	$item_price_divisa = isset($history_item['price_divisa']) ? (float)$history_item['price_divisa'] : 0.0;
	$item_price_bs = $item_price_divisa * $rate_value;
	$history_total_divisa += $item_price_divisa;
	$history_total_bs += $item_price_bs;
	$history_total_divisa_discounted += $item_price_divisa - ($item_price_divisa * ($discount_divisa / 100));
	$history_total_bs_discounted += $item_price_bs - ($item_price_bs * ($discount_bs / 100));
}
?>
<section class="panel visor-screen">
	<div
		id="visor-discount-toast"
		style="position:fixed; top:18px; left:50%; transform:translate(-50%, -12px); z-index:30; padding:10px 16px; border-radius:16px; background:rgba(20,41,64,0.92); color:#fff; font-size:calc(.92rem * var(--visor-text-size)); font-weight:700; box-shadow:0 10px 24px rgba(20,41,64,0.18); opacity:0; pointer-events:none; transition:opacity .18s ease, transform .18s ease;"
	>
		Descuento activo: Divisa <?php echo number_format($discount_divisa, 2, ',', '.'); ?>% | Bs <?php echo number_format($discount_bs, 2, ',', '.'); ?>%
	</div>

	<div style="display:flex; align-items:flex-start; justify-content:space-between; gap:12px; margin-bottom:14px;">
		<div style="font-size:calc(13px * var(--visor-text-size)); font-weight:700; color:var(--visor-text);">
			BCV: <?php echo number_format($rate_value, 2, ',', '.'); ?> <?php echo htmlspecialchars($rate_symbol, ENT_QUOTES, 'UTF-8'); ?>
		</div>
		<a class="btn btn-outline icon-btn no-discount-toggle" href="<?php echo site_url(!empty($auth_user) ? 'inicio' : 'login'); ?>" aria-label="Inicio">
			<i class="ion-ios-home-outline"></i>
		</a>
	</div>

	<div style="text-align:center; margin-top: 20px; padding: 8px 0 12px;">
		<?php if (!empty($lookup_result['name'])) { ?>
			<div style="font-size: calc(1rem * var(--visor-product-name-size)); font-weight: 700; margin-bottom: 6px; color: var(--visor-product-name-color);"><?php echo htmlspecialchars($lookup_result['name'], ENT_QUOTES, 'UTF-8'); ?></div>
			<?php if (!empty($lookup_result['subtitle'])) { ?>
				<div style="font-size: calc(.95rem * var(--visor-text-size)); color: var(--visor-text); margin-bottom: 10px;"><?php echo htmlspecialchars($lookup_result['subtitle'], ENT_QUOTES, 'UTF-8'); ?></div>
			<?php } ?>
		<?php } ?>
		<div
			class="discountable-price"
			data-original="<?php echo number_format($current_price, 2, ',', '.'); ?>"
			data-discounted="<?php echo number_format($current_price_discounted, 2, ',', '.'); ?>"
			style="font-size: calc(1rem * var(--visor-price-divisa-size)); font-weight: 700; line-height: 1; color: var(--visor-price-divisa-color);"
		><?php echo number_format($current_price, 2, ',', '.'); ?></div>
		<div
			class="discountable-price"
			data-original="BCV: <?php echo number_format($current_price_bs, 2, ',', '.'); ?> <?php echo htmlspecialchars($rate_symbol, ENT_QUOTES, 'UTF-8'); ?>"
			data-discounted="BCV: <?php echo number_format($current_price_bs_discounted, 2, ',', '.'); ?> <?php echo htmlspecialchars($rate_symbol, ENT_QUOTES, 'UTF-8'); ?>"
			style="margin-top: 8px; font-size: calc(1rem * var(--visor-price-bs-size)); color: var(--visor-price-bs-color);"
		>
			BCV: <?php echo number_format($current_price_bs, 2, ',', '.'); ?> <?php echo htmlspecialchars($rate_symbol, ENT_QUOTES, 'UTF-8'); ?>
		</div>
	</div>

	<form method="post" action="<?php echo site_url('visor'); ?>" autocomplete="off">
		<div class="actions" style="margin-top:20px;">
			<div class="field" style="flex:1; margin-top:0;">
				<input id="visor-reference-input" class="no-discount-toggle" type="text" name="reference" value="<?php echo htmlspecialchars(isset($reference) ? $reference : '', ENT_QUOTES, 'UTF-8'); ?>" placeholder="Ingrese la referencia (codigo)" autocomplete="off" autofocus style="background: var(--visor-box); font-size: calc(1rem * var(--visor-text-size)); color: var(--visor-text);" />
			</div>
			<button class="btn no-discount-toggle" type="submit" style="background: var(--visor-button-color); color: var(--visor-button-ink); font-size: calc(1rem * var(--visor-button-size));">Consultar</button>
		</div>
	</form>

	<?php if (!empty($lookup_error)) { ?>
		<div style="margin-top:14px; padding:12px 14px; border-radius:16px; background: #fdecec; color: #8a1f1f; font-size: calc(.95rem * var(--visor-text-size));">
			<?php echo htmlspecialchars($lookup_error, ENT_QUOTES, 'UTF-8'); ?>
		</div>
	<?php } ?>

	<div class="history">
		<div style="display:flex; align-items:center; justify-content:space-between; gap:10px; margin-bottom:12px;">
			<strong style="font-size: calc(1rem * var(--visor-text-size)); color: var(--visor-text);">Historico (sesion actual)</strong>
			<a
				class="btn no-discount-toggle"
				href="<?php echo site_url('visor/limpiar'); ?>"
				style="background: var(--visor-button-color); color: var(--visor-button-ink); font-size: calc(1rem * var(--visor-button-size)); box-shadow: none;"
			>Limpiar</a>
		</div>
		<?php if (empty($history)) { ?>
			<div class="empty">Sin consultas aun.</div>
		<?php } else { ?>
			<?php foreach ($history as $history_item) { ?>
				<?php
				$item_price_divisa = isset($history_item['price_divisa']) ? (float)$history_item['price_divisa'] : 0.0;
				$item_price_bs = $item_price_divisa * $rate_value;
				$item_price_divisa_discounted = $item_price_divisa - ($item_price_divisa * ($discount_divisa / 100));
				$item_price_bs_discounted = $item_price_bs - ($item_price_bs * ($discount_bs / 100));
				?>
				<div style="display:grid; grid-template-columns: 1fr auto; gap:10px; padding:10px 0;">
					<div>
						<div style="font-weight:700; font-size: calc(1rem * var(--visor-text-size)); color: var(--visor-text);"><?php echo htmlspecialchars(isset($history_item['name']) ? $history_item['name'] : '', ENT_QUOTES, 'UTF-8'); ?></div>
						<div style="font-size:calc(12px * var(--visor-text-size)); color:var(--visor-text);"><?php echo htmlspecialchars(isset($history_item['reference']) ? $history_item['reference'] : '', ENT_QUOTES, 'UTF-8'); ?></div>
					</div>
					<div style="text-align:right;">
						<div
							class="discountable-price"
							data-original="<?php echo number_format($item_price_divisa, 2, ',', '.'); ?>"
							data-discounted="<?php echo number_format($item_price_divisa_discounted, 2, ',', '.'); ?>"
							style="font-weight:700; color: var(--visor-text);"
						><?php echo number_format($item_price_divisa, 2, ',', '.'); ?></div>
						<div
							class="discountable-price"
							data-original="<?php echo number_format($item_price_bs, 2, ',', '.'); ?>"
							data-discounted="<?php echo number_format($item_price_bs_discounted, 2, ',', '.'); ?>"
							style="font-size:calc(12px * var(--visor-text-size)); color:var(--visor-text);"
						><?php echo number_format($item_price_bs, 2, ',', '.'); ?></div>
					</div>
				</div>
			<?php } ?>
			<div style="display:grid; grid-template-columns: 1fr auto auto; gap:10px; padding-top:12px; font-weight:700; color: var(--visor-text);">
				<div>Total:</div>
				<div
					class="discountable-price"
					data-original="<?php echo number_format($history_total_divisa, 2, ',', '.'); ?>"
					data-discounted="<?php echo number_format($history_total_divisa_discounted, 2, ',', '.'); ?>"
				><?php echo number_format($history_total_divisa, 2, ',', '.'); ?></div>
				<div
					class="discountable-price"
					data-original="<?php echo number_format($history_total_bs, 2, ',', '.'); ?>"
					data-discounted="<?php echo number_format($history_total_bs_discounted, 2, ',', '.'); ?>"
				><?php echo number_format($history_total_bs, 2, ',', '.'); ?></div>
			</div>
		<?php } ?>
	</div>
</section>
<script>
document.addEventListener('DOMContentLoaded', function () {
	var visorScreen = document.querySelector('.visor-screen');
	var input = document.getElementById('visor-reference-input');
	var discountablePrices = Array.prototype.slice.call(document.querySelectorAll('.discountable-price'));
	var discountToast = document.getElementById('visor-discount-toast');
	var discountActive = false;

	var setDiscountState = function (active) {
		if (discountActive === active) {
			return;
		}
		discountActive = active;
		discountablePrices.forEach(function (element) {
			element.textContent = active ? element.getAttribute('data-discounted') : element.getAttribute('data-original');
		});
		if (discountToast) {
			discountToast.style.opacity = active ? '1' : '0';
			discountToast.style.transform = active ? 'translate(-50%, 0)' : 'translate(-50%, -12px)';
		}
	};

	var shouldIgnoreDiscountTarget = function (target) {
		return !!(target && target.closest('.no-discount-toggle, button, a, input, textarea, select, label, form'));
	};

	if (input) {
		input.focus();
		input.value = '';
	}

	if (visorScreen) {
		visorScreen.addEventListener('pointerdown', function (event) {
			if (shouldIgnoreDiscountTarget(event.target)) {
				return;
			}
			setDiscountState(true);
		});

		['pointerup', 'pointercancel', 'pointerleave'].forEach(function (eventName) {
			visorScreen.addEventListener(eventName, function () {
				setDiscountState(false);
			});
		});

		visorScreen.addEventListener('touchstart', function (event) {
			if (shouldIgnoreDiscountTarget(event.target)) {
				return;
			}
			setDiscountState(true);
		}, { passive: true });

		['touchend', 'touchcancel'].forEach(function (eventName) {
			visorScreen.addEventListener(eventName, function () {
				setDiscountState(false);
			}, { passive: true });
		});
	}
});
</script>
