<?php
function getOrderStatusClass($status) {
	switch ($status) {
		case 'completed': case 'processing': return 'success';
		case 'pending': case 'new': return 'warning';
		case 'cancelled': case 'refunded': return 'danger';
		case 'shipped': return 'info';
		default: return 'default';
	}
}
?>
<?php
$install_url = isset($tpl['option_arr']['o_install_url']) ? $tpl['option_arr']['o_install_url'] : '';
?>
<div class="container-fluid pjScCart pjScOrderDetails">

	<?php if (isset($tpl['status']) && $tpl['status'] == 'OK') {
		$payment_methods_arr = isset($tpl['payment_titles']) && is_array($tpl['payment_titles']) ? $tpl['payment_titles'] : __('payment_methods', true);
		if (!is_array($payment_methods_arr)) {
			$payment_methods_arr = array();
		}
		$order_statuses_arr = __('order_statuses', true);
		$orderPaymentStatus = isset($tpl['arr']['payment_status']) ? $tpl['arr']['payment_status'] : '';
		$pay_link = $orderPaymentStatus !== 'paid'
			&& $tpl['arr']['status'] !== 'cancelled'
			&& pjAppController::isOnlinePaymentMethod($tpl['arr']['payment_method'], isset($tpl['arr']['foreign_id']) ? $tpl['arr']['foreign_id'] : 1);
		$retry_url = $pay_link ? pjAppController::getPaymentRetryUrl($tpl['arr']) : '';
		$status_label = isset($order_statuses_arr[$tpl['arr']['status']]) ? $order_statuses_arr[$tpl['arr']['status']] : ucfirst(str_replace('_', ' ', $tpl['arr']['status']));
		$status_class = getOrderStatusClass($tpl['arr']['status']);
		$hasShippingAddress = !empty($tpl['arr']['s_name']) || !empty($tpl['arr']['s_address_1']) || !empty($tpl['arr']['s_city']);
	?>

	<!-- Back button -->
	<a href="<?php echo $install_url; ?>/orders" class="sc-od-back">
		<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>
		<?php echo __('front_back_to_orders', true) ?: 'Back to orders'; ?>
	</a>

	<!-- Order Header Card -->
	<div class="sc-od-header">
		<div class="sc-od-header-main">
			<div class="sc-od-header-left">
				<div class="sc-od-order-number"><?php echo pjSanitize::html($tpl['arr']['uuid'] ?? ''); ?></div>
				<div class="sc-od-order-date">
					<span class="sc-od-date-label"><?php echo __('front_order_created', true) ?: 'Order date'; ?>:</span>
					<?php echo date($tpl['option_arr']['o_date_format'] . ', ' . $tpl['option_arr']['o_time_format'], strtotime($tpl['arr']['created'])); ?>
				</div>
			</div>
			<div class="sc-od-header-right">
				<span class="sc-od-status sc-od-status--<?php echo $status_class; ?>"><?php echo pjSanitize::html($status_label); ?></span>
			</div>
		</div>
		<div class="sc-od-header-meta">
			<span class="sc-od-meta-item">
				<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="1" y="3" width="15" height="13"></rect><polygon points="16 8 20 8 23 11 23 16 16 16 16 8"></polygon><circle cx="5.5" cy="18.5" r="2.5"></circle><circle cx="18.5" cy="18.5" r="2.5"></circle></svg>
				<?php echo __('front_shipping', true) ?: 'Shipping'; ?>
			</span>
			<span class="sc-od-meta-item">
				<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="1" y="4" width="22" height="16" rx="2" ry="2"></rect><line x1="1" y1="10" x2="23" y2="10"></line></svg>
				<?php
				$payment_method = isset($tpl['arr']['payment_method']) ? $tpl['arr']['payment_method'] : '';
				echo pjSanitize::html(isset($payment_methods_arr[$payment_method]) ? $payment_methods_arr[$payment_method] : ucfirst(str_replace('_', ' ', $payment_method)));
				?>
			</span>
			<?php if ($pay_link && !empty($retry_url)) { ?>
			<a href="<?php echo pjSanitize::html($retry_url); ?>" class="sc-od-meta-item sc-od-pay-link">
				<?php echo __('front_order_pay_order', true) ?: 'Pay now'; ?>
			</a>
			<?php } ?>
		</div>
	</div>

	<!-- Products (cart-style layout) -->
	<div class="sc-od-card pjScCart-page">
		<?php
		$total_products = 0;
		$total_extras = 0;
		?>
		<table class="table table-striped">
			<thead>
				<tr>
					<th></th>
					<th class="hidden-xs"></th>
					<th width="100%">
						<div class="row">
							<div class="col-sm-5 mob-name"><?php echo __('front_product', true) ?: 'Product'; ?></div>
							<div class="col-sm-3 hidden-xs text-right"><?php echo __('front_price', true) ?: 'Price'; ?></div>
							<div class="col-sm-1 hidden-xs text-center"><?php echo __('front_quantity', true) ?: 'Qty'; ?></div>
							<div class="col-sm-3 text-right"><?php echo __('front_total', true) ?: 'Total'; ?></div>
						</div>
					</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ($tpl['os_arr'] as $i => $item) {
					// Order stock prices are stored exactly as charged during checkout.
					$unit_price = (float) $item['price'];
					$line_total = $unit_price * (int) $item['qty'];
					$total_products += $line_total;

					$img_path = !empty($item['product_image']) && is_file($item['product_image'])
						? PJ_INSTALL_URL . $item['product_image']
						: PJ_INSTALL_URL . PJ_IMG_PATH . 'frontend/80x106.png';
				?>
				<tr>
					<td class="hidden-xs sc-od-img-cell">
						<img class="product-img-larg" src="<?php echo $img_path; ?>" alt="<?php echo pjSanitize::html($item['name']); ?>">
					</td>
					<td class="hidden-xs" style="width:1px;"></td>
					<td>
						<div class="row top-pad1">
							<!-- Product name -->
							<div class="col-sm-5 mob-one">
								<span class="product-name"><?php echo pjSanitize::html($item['name']); ?></span>
								<?php
								if (isset($item['attr']) && !empty($item['attr'])) {
									$at = array();
									$a = explode(",", $item['attr']);
									foreach ($a as $v) {
										$t = explode("_", $v);
										if (isset($t[0], $t[1])) $at[$t[1]] = $t[0];
									}
									foreach ($at as $attr_parent_id => $attr_id) {
										foreach ($tpl['attr_arr'] as $attr) {
											if ($attr['id'] == $attr_parent_id) {
												foreach ($attr['child'] as $child) {
													if ($child['id'] == $attr_id) {
														printf('<div class="sc-od-attr">%s: %s</div>', pjSanitize::html($attr['name']), pjSanitize::html($child['name']));
														break;
													}
												}
											}
										}
									}
								}
								?>
							</div>
							<!-- Unit price -->
							<div class="col-sm-3 hidden-xs text-right sc-od-unit-price">
								<?php echo pjCurrency::formatPrice($unit_price); ?>
							</div>
							<!-- Quantity -->
							<div class="col-sm-1 hidden-xs text-center">
								<?php echo (int) $item['qty']; ?>
							</div>
							<!-- Line total -->
							<div class="col-sm-3 text-right mob-to">
								<span class="sc-current-price"><?php echo pjCurrency::formatPrice($line_total); ?></span>
							</div>
						</div>

						<?php
						// Display extras
						if (isset($item['extra']) && !empty($item['extra'])) {
							$extras_display = array();
							$a = explode(",", $item['extra']);
							foreach ($a as $eid) {
								$_arr = explode('.', $eid);
								if (count($_arr) == 2) {
									$e_id = $_arr[0] ?? '';
									$ei_id = $_arr[1] ?? '';
									foreach ($tpl['extra_arr'] as $extra) {
										if ($extra['id'] == $e_id && isset($extra['extra_items'])) {
											foreach ($extra['extra_items'] as $extra_item) {
												if ($extra_item['id'] == $ei_id) {
													$ep = (float) $extra_item['price'];
													$extras_display[] = array('name' => $extra_item['name'], 'price' => $ep, 'total' => $ep * (int) $item['qty']);
													$total_extras += $ep * (int) $item['qty'];
													break;
												}
											}
											break;
										}
									}
								} else {
									$parts = explode("|", $eid);
									$e_id = $parts[0] ?? '';
									if (empty($e_id)) continue;
									foreach ($tpl['extra_arr'] as $extra) {
										if ($extra['id'] == $e_id) {
											$ep = (float) $extra['price'];
											$extras_display[] = array('name' => $extra['name'], 'price' => $ep, 'total' => $ep * (int) $item['qty']);
											$total_extras += $ep * (int) $item['qty'];
											break;
										}
									}
								}
							}
							if (!empty($extras_display)) {
						?>
						<div class="row">
							<div class="col-sm-12 row-pading">
								<?php foreach ($extras_display as $ed) { ?>
								<div class="row mb-1 sc-extra-row">
									<div class="col-sm-5 sc-od-extra-name"><?php echo pjSanitize::html($ed['name']); ?></div>
									<div class="col-sm-3 hidden-xs sc-od-extra-price text-right"><?php echo pjCurrency::formatPrice($ed['price']); ?></div>
									<div class="col-sm-1 hidden-xs"></div>
									<div class="col-sm-3 sc-od-extra-price text-right"><?php echo pjCurrency::formatPrice($ed['total']); ?></div>
								</div>
								<?php } ?>
							</div>
						</div>
						<?php } } ?>
					</td>
				</tr>
				<?php } ?>
			</tbody>
		</table>

		<!-- Totals aligned with cart/price.php -->
		<?php
		$_insurance       = (float) ($tpl['arr']['insurance'] ?? 0);
		$_grand_total     = (float) ($tpl['arr']['total'] ?? 0);
		$shipping_amount  = (float) ($tpl['arr']['shipping'] ?? 0);
		$tax_amount       = (float) ($tpl['arr']['tax'] ?? 0);
		$_voucher_disc    = (float) ($tpl['arr']['discount'] ?? 0);
		?>
		<div class="sc-od-totals">
			<div class="sc-od-total-row">
				<span><?php echo __('front_sub_total', true) ?: 'Subtotal'; ?></span>
				<span><?php echo pjCurrency::formatPrice($tpl['arr']['price']); ?></span>
			</div>
			<?php if ($_voucher_disc > 0) { ?>
			<div class="sc-od-total-row">
				<span><?php echo __('front_discount_voucher', true) ?: 'Discount code'; ?><?php echo !empty($tpl['arr']['voucher']) ? ' <strong>' . pjSanitize::html($tpl['arr']['voucher']) . '</strong>' : ''; ?></span>
				<span class="sc-summary-discount">-<?php echo pjCurrency::formatPrice($_voucher_disc); ?></span>
			</div>
			<?php } ?>
			<?php if ($_insurance > 0) { ?>
			<div class="sc-od-total-row">
				<span><?php echo __('front_order_insurance', true) ?: 'Insurance'; ?></span>
				<span><?php echo pjCurrency::formatPrice($_insurance); ?></span>
			</div>
			<?php } ?>
			<?php if ($shipping_amount > 0) { ?>
			<div class="sc-od-total-row">
				<span><?php echo __('front_shipping', true) ?: 'Shipping'; ?></span>
				<span><?php echo pjCurrency::formatPrice($shipping_amount); ?></span>
			</div>
			<?php } ?>
			<?php if ($tax_amount > 0) { ?>
			<div class="sc-od-total-row">
				<span><?php echo __('front_tax', true) ?: 'Tax'; ?></span>
				<span><?php echo pjCurrency::formatPrice($tax_amount); ?></span>
			</div>
			<?php } ?>
			<div class="sc-od-total-row sc-od-total-row--grand">
				<span><?php echo __('front_order_total', true) ?: 'Total'; ?></span>
				<span><?php echo pjCurrency::formatPrice($_grand_total); ?></span>
			</div>
		</div>
	</div>

	<!-- Digital downloads (phase 4 + 6) — placed under Products for customer flow -->
	<?php if (!empty($tpl['digital_downloads'])):
		$dlDateFmt = !empty($tpl['option_arr']['o_date_format']) ? $tpl['option_arr']['o_date_format'] : 'd-m-Y';
		$dlTimeFmt = !empty($tpl['option_arr']['o_time_format']) ? $tpl['option_arr']['o_time_format'] : 'H:i';
		$dlBaseUrl = PJ_INSTALL_URL . 'index.php?controller=pjFront&action=pjActionDigitalGet&token=';
	?>
	<div id="digital-downloads" class="sc-od-card sc-od-downloads">
		<h3 class="sc-od-downloads-title"><?php echo __('front_digital_downloads_title', true) ?: 'Digital Downloads'; ?></h3>
		<p class="sc-od-downloads-intro"><?php echo __('front_digital_downloads_intro', true) ?: 'Download your purchased digital products below.'; ?></p>

		<div class="sc-od-downloads-list">
		<?php foreach ($tpl['digital_downloads'] as $dd):
			$ddCount = (int) $dd['download_count'];
			$ddMax   = ($dd['max_downloads'] !== null && $dd['max_downloads'] !== '') ? (int) $dd['max_downloads'] : null;
			$ddExpiresTs = !empty($dd['expires_at']) ? strtotime($dd['expires_at']) : null;
			$ddIsExpired = ($ddExpiresTs !== null && $ddExpiresTs < time());
			$ddIsMaxed   = ($ddMax !== null && $ddCount >= $ddMax);
			$ddDownloadable = (!$ddIsExpired && !$ddIsMaxed);
			$ddUrl = $dlBaseUrl . urlencode((string) $dd['token']);

			// Stat 1 — counter "remaining/max" of "∞"
			if ($ddMax !== null) {
				$ddCounterText = sprintf('%d/%d', max(0, $ddMax - $ddCount), $ddMax);
			} else {
				$ddCounterText = '∞';
			}
			// Stat 2 — valid until (label "Download until" -> value date only)
			$ddExpiryText = ($ddExpiresTs !== null)
				? date($dlDateFmt, $ddExpiresTs)
				: '—';
			// Stat 3 — last downloaded
			$ddLastText = '—';
			if (!empty($dd['last_downloaded_at'])) {
				$lastTs = strtotime($dd['last_downloaded_at']);
				if ($lastTs) {
					$ddLastText = date($dlDateFmt . ' ' . $dlTimeFmt, $lastTs);
				}
			}
		?>
			<div class="sc-od-download-item">
				<div class="sc-od-download-action">
					<?php if ($ddDownloadable): ?>
					<a href="<?php echo pjSanitize::html($ddUrl); ?>" class="pjScBtnPrimary sc-od-download-btn">
						<?php echo __('digital_download_btn', true) ?: 'Download'; ?>
					</a>
					<?php elseif ($ddIsMaxed): ?>
					<span class="sc-od-download-badge sc-od-download-badge--blocked">
						<?php echo __('front_digital_status_max', true) ?: 'Max downloads reached'; ?>
					</span>
					<?php elseif ($ddIsExpired): ?>
					<span class="sc-od-download-badge sc-od-download-badge--blocked">
						<?php echo __('front_digital_status_expired', true) ?: 'Expired'; ?>
					</span>
					<?php endif; ?>

					<?php if (!empty($dd['password'])): ?>
					<div class="sc-od-download-pw" data-pw="<?php echo pjSanitize::html($dd['password']); ?>">
						<span class="sc-od-download-pw-label"><?php echo __('digital_download_password_label', true) ?: 'Password'; ?>:</span>
						<code class="sc-od-download-pw-value"><?php echo pjSanitize::html($dd['password']); ?></code>
						<button type="button" class="sc-od-download-pw-copy" title="<?php echo __('front_digital_copied', true) ?: 'Copied!'; ?>" aria-label="<?php echo __('front_digital_copied', true) ?: 'Copied!'; ?>">
							<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
								<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
								<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
							</svg>
						</button>
					</div>
					<?php endif; ?>
				</div>

				<div class="sc-od-download-stats">
					<div class="sc-od-stat-card">
						<svg class="sc-od-stat-icon" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>
						<span class="sc-od-stat-label"><?php echo __('front_digital_label_count', true) ?: 'Downloads'; ?></span>
						<span class="sc-od-stat-text"><?php echo pjSanitize::html($ddCounterText); ?></span>
					</div>
					<div class="sc-od-stat-card">
						<svg class="sc-od-stat-icon" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg>
						<span class="sc-od-stat-label"><?php echo __('front_digital_label_until', true) ?: 'Valid until'; ?></span>
						<span class="sc-od-stat-text"><?php echo pjSanitize::html($ddExpiryText); ?></span>
					</div>
					<div class="sc-od-stat-card">
						<svg class="sc-od-stat-icon" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>
						<span class="sc-od-stat-label"><?php echo __('front_digital_label_last', true) ?: 'Last downloaded'; ?></span>
						<span class="sc-od-stat-text"><?php echo pjSanitize::html($ddLastText); ?></span>
					</div>
				</div>
			</div>
		<?php endforeach; ?>
		</div>
	</div>

	<script>
	(function() {
		var copiedLabel = <?php echo json_encode(__('front_digital_copied', true) ?: 'Copied!', JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); ?>;
		document.querySelectorAll('.sc-od-download-pw-copy').forEach(function(btn) {
			btn.addEventListener('click', function() {
				var wrap = btn.closest('.sc-od-download-pw');
				if (!wrap) return;
				var pw = wrap.getAttribute('data-pw') || '';
				if (!pw) return;
				var done = function() {
					btn.classList.add('is-copied');
					var oldTitle = btn.getAttribute('title');
					btn.setAttribute('title', copiedLabel);
					setTimeout(function() {
						btn.classList.remove('is-copied');
						if (oldTitle) btn.setAttribute('title', oldTitle);
					}, 1800);
				};
				if (navigator.clipboard && navigator.clipboard.writeText) {
					navigator.clipboard.writeText(pw).then(done, function() {});
				} else {
					var ta = document.createElement('textarea');
					ta.value = pw;
					ta.style.position = 'absolute';
					ta.style.left = '-9999px';
					document.body.appendChild(ta);
					ta.select();
					try { document.execCommand('copy'); done(); } catch (e) {}
					document.body.removeChild(ta);
				}
			});
		});
	})();
	</script>
	<?php endif; ?>

	<?php if (!empty($tpl['arr']['voucher'])) { ?>
	<div class="sc-od-card sc-od-card--compact">
		<div class="sc-od-card-title"><?php echo __('front_order_voucher', true) ?: 'Discount Code'; ?></div>
		<div class="sc-od-voucher-code"><?php echo pjSanitize::html($tpl['arr']['voucher']); ?></div>
	</div>
	<?php } ?>

	<?php
	$order_notes = isset($tpl['arr']['notes']) ? (string) $tpl['arr']['notes'] : '';
	$order_notes = trim(preg_replace('/<br\\s*\\/?>\\s*<b>(Deprecated|Warning|Notice)<\\/b>:[\\s\\S]*?<br\\s*\\/?>/i', '', $order_notes));
	if (!empty($order_notes)) { ?>
	<div class="sc-od-card sc-od-card--compact">
		<div class="sc-od-card-title"><?php echo __('front_order_notes', true) ?: 'Notes'; ?></div>
		<div class="sc-od-notes"><?php echo nl2br(pjSanitize::html($order_notes)); ?></div>
	</div>
	<?php } ?>

	<?php
	// Custom fields
	if (!empty($tpl['arr']['custom_fields'])) {
		$cf_values = json_decode($tpl['arr']['custom_fields'], true);
		if (is_array($cf_values)) {
			$cf_defs = pjCheckoutCustomFieldModel::factory()->findAll()->getData();
			$cf_map = array();
			foreach ($cf_defs as $d) $cf_map[$d['field_key']] = $d;
			$has_cf = false;
			foreach ($cf_values as $ck => $cv) {
				if (!empty($cv) || $cv === '0') { $has_cf = true; break; }
			}
			if ($has_cf) {
	?>
	<div class="sc-od-card sc-od-card--compact">
		<div class="sc-od-card-title"><?php echo __('front_custom_fields', true) ?: 'Additional Information'; ?></div>
		<div class="sc-od-field-grid">
			<?php foreach ($cf_values as $ck => $cv) {
				if (empty($cv) && $cv !== '0') continue;
				$cf_id = isset($cf_map[$ck]) ? $cf_map[$ck]['id'] : 0;
				$cf_label = $cf_id ? (__('cf_' . $cf_id . '_label', true) ?: $ck) : $ck;
				$cf_type = isset($cf_map[$ck]) ? $cf_map[$ck]['field_type'] : 'text';
				$display_val = ($cf_type === 'checkbox') ? ($cv ? (__('front_yes', true) ?: 'Yes') : (__('front_no', true) ?: 'No')) : pjSanitize::html($cv);
			?>
			<div class="sc-od-field">
				<div class="sc-od-field-label"><?php echo pjSanitize::html($cf_label); ?></div>
				<div class="sc-od-field-value"><?php echo $display_val; ?></div>
			</div>
			<?php } ?>
		</div>
	</div>
	<?php } } } ?>

	<!-- Addresses -->
	<?php
	// Always display billing address
	$b_firstname = $tpl['arr']['b_firstname'] ?? '';
	$b_name = $tpl['arr']['b_name'] ?? '';
	$b_fullname = trim($b_firstname . ' ' . $b_name);
	$b_addr = trim(($tpl['arr']['b_address_1'] ?? '') . ' ' . (@$tpl['arr']['b_house_number'] ?: ''));
	$has_billing = (!empty($b_fullname) || !empty($b_addr) || !empty($tpl['arr']['b_city']));

	$show_shipping = $hasShippingAddress;
	?>
	<?php if ($has_billing || $show_shipping) { ?>
	<div class="sc-od-address-grid<?php echo (!$show_shipping) ? ' sc-od-address-grid--single' : ''; ?>">
		<?php if ($has_billing) { ?>
		<div class="sc-od-card sc-od-card--compact">
			<div class="sc-od-card-title"><?php echo __('front_order_tab_billing_details', true) ?: 'Billing Address'; ?></div>
			<div class="sc-od-address">
				<?php if (!empty($b_fullname)) { ?><div class="sc-od-address-name"><?php echo pjSanitize::html($b_fullname); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['b_company'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['b_company']); ?></div><?php } ?>
				<?php if (!empty($b_addr)) { ?><div><?php echo pjSanitize::html($b_addr); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['b_address_2'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['b_address_2']); ?></div><?php } ?>
				<?php
				$b_location = array();
				if (!empty($tpl['arr']['b_zip'])) $b_location[] = pjSanitize::html($tpl['arr']['b_zip']);
				if (!empty($tpl['arr']['b_city'])) $b_location[] = pjSanitize::html($tpl['arr']['b_city']);
				if (!empty($b_location)) { ?><div><?php echo implode(' ', $b_location); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['b_state'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['b_state']); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['b_country'])) { ?><div class="sc-od-address-country"><?php echo pjSanitize::html($tpl['arr']['b_country']); ?></div><?php } ?>
			</div>
		</div>
		<?php } ?>

		<?php if ($show_shipping) {
			$shipping_title = __('front_order_tab_shipping_details', true) ?: 'Shipping Address';
			// If same_as=1 OR if shipping address is completely empty, show "same as billing address"
			$s_firstname_chk = $tpl['arr']['s_firstname'] ?? '';
			$s_name_chk = $tpl['arr']['s_name'] ?? '';
			$s_addr_chk = $tpl['arr']['s_address_1'] ?? '';
			$s_city_chk = $tpl['arr']['s_city'] ?? '';
			$shipping_is_empty = (empty(trim($s_firstname_chk)) && empty(trim($s_name_chk)) && empty(trim($s_addr_chk)) && empty(trim($s_city_chk)));
			if ((int) ($tpl['arr']['same_as'] ?? 0) === 1 || $shipping_is_empty) { ?>
		<div class="sc-od-card sc-od-card--compact">
			<div class="sc-od-card-title"><?php echo $shipping_title; ?></div>
			<div class="sc-od-address">
				<?php if (!empty($b_fullname)) { ?><div class="sc-od-address-name"><?php echo pjSanitize::html($b_fullname); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['b_company'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['b_company']); ?></div><?php } ?>
				<?php if (!empty($b_addr)) { ?><div><?php echo pjSanitize::html($b_addr); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['b_address_2'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['b_address_2']); ?></div><?php } ?>
				<?php if (!empty($b_location)) { ?><div><?php echo implode(' ', $b_location); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['b_state'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['b_state']); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['b_country'])) { ?><div class="sc-od-address-country"><?php echo pjSanitize::html($tpl['arr']['b_country']); ?></div><?php } ?>
			</div>
		</div>
		<?php } else {
				$s_firstname = $tpl['arr']['s_firstname'] ?? '';
				$s_name = $tpl['arr']['s_name'] ?? '';
				$s_fullname = trim($s_firstname . ' ' . $s_name);
				$s_addr = trim(($tpl['arr']['s_address_1'] ?? '') . ' ' . (@$tpl['arr']['s_house_number'] ?: ''));
		?>
		<div class="sc-od-card sc-od-card--compact">
			<div class="sc-od-card-title"><?php echo $shipping_title; ?></div>
			<div class="sc-od-address">
				<?php if (!empty($s_fullname)) { ?><div class="sc-od-address-name"><?php echo pjSanitize::html($s_fullname); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['s_company'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['s_company']); ?></div><?php } ?>
				<?php if (!empty($s_addr)) { ?><div><?php echo pjSanitize::html($s_addr); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['s_address_2'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['s_address_2']); ?></div><?php } ?>
				<?php
				$s_location = array();
				if (!empty($tpl['arr']['s_zip'])) $s_location[] = pjSanitize::html($tpl['arr']['s_zip']);
				if (!empty($tpl['arr']['s_city'])) $s_location[] = pjSanitize::html($tpl['arr']['s_city']);
				if (!empty($s_location)) { ?><div><?php echo implode(' ', $s_location); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['s_state'])) { ?><div><?php echo pjSanitize::html($tpl['arr']['s_state']); ?></div><?php } ?>
				<?php if (!empty($tpl['arr']['s_country'])) { ?><div class="sc-od-address-country"><?php echo pjSanitize::html($tpl['arr']['s_country']); ?></div><?php } ?>
			</div>
		</div>
		<?php } ?>
		<?php } ?>
	</div>
	<?php } ?>

	<!-- Actions -->
	<div class="sc-od-actions">
		<?php if (!empty($tpl['arr']['invoice_uuid'])) { ?>
		<a href="<?php echo PJ_INSTALL_URL; ?>index.php?controller=pjFrontPublic&action=pjActionOrderInvoice&uuid=<?php echo pjSanitize::html($tpl['arr']['uuid']); ?>"
			class="pjScBtnSecondary" target="_blank">
			<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>
				<?php echo __('front_print_invoice', true) ?: 'Print invoice'; ?>
		</a>
		<?php } ?>
		<?php if ($pay_link && !empty($retry_url)) { ?>
		<a href="<?php echo pjSanitize::html($retry_url); ?>" class="pjScBtnPrimary">
			<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="1" y="4" width="22" height="16" rx="2" ry="2"></rect><line x1="1" y1="10" x2="23" y2="10"></line></svg>
			<?php echo __('front_order_pay_order', true) ?: 'Pay Now'; ?>
		</a>
		<?php } ?>
		<button class="pjScBtnPrimary pjScBtnReorder"
			data-id="<?php echo $tpl['arr']['id']; ?>"
			data-confirm-title="<?php echo __('front_reorder_confirm_title', true) ?: 'Reorder'; ?>"
			data-confirm-text="<?php echo __('front_reorder_confirm_text', true) ?: 'Add the same products from this order to your cart?'; ?>"
			data-confirm-button="<?php echo __('front_button_reorder', true) ?: 'Reorder'; ?>"
			data-cancel-button="<?php echo __('btnCancel', true) ?: 'Cancel'; ?>"
			type="button">
			<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"></polyline><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path></svg>
			<?php echo __('front_button_reorder', true) ?: 'Reorder'; ?>
		</button>
	</div>

	<?php } else {
		$isLoggedIn = !empty($_SESSION['client_user']['id']);
	?>
	<div class="sc-od-empty">
		<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="12"></line><line x1="12" y1="16" x2="12.01" y2="16"></line></svg>
		<?php if (!$isLoggedIn) : ?>
		<p><?php echo __('front_order_login_required', true) ?: 'Please log in to view your order.'; ?></p>
		<div class="sc-button-group" style="margin-top:12px;justify-content:center;">
			<a href="<?php echo $install_url; ?>/orders" class="pjScBtnSecondary"><?php echo __('front_back_to_orders', true) ?: 'Back to orders'; ?></a>
			<a href="<?php echo $install_url; ?>/inloggen" class="pjScBtnPrimary"><?php echo __('front_login', true) ?: 'Log In'; ?></a>
		</div>
		<?php else : ?>
		<p><?php echo __('front_order_not_found', true) ?: 'Order not found.'; ?></p>
		<div class="sc-button-group" style="margin-top:12px;justify-content:center;">
			<a href="<?php echo $install_url; ?>/orders" class="pjScBtnSecondary"><?php echo __('front_back_to_orders', true) ?: 'Back to orders'; ?></a>
		</div>
		<?php endif; ?>
	</div>
	<?php } ?>
</div>
