<!DOCTYPE html>
<html lang="es">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Link Creado</title>
	<style>
		body { margin:0; font-family:Segoe UI, sans-serif; background:#f7f8fc; color:#1e293b; }
		.wrap { max-width:780px; margin:0 auto; padding:34px 16px; }
		.card { background:#fff; border:1px solid #e2e8f0; border-radius:14px; padding:22px; box-shadow:0 7px 24px rgba(2,6,23,.06); }
		.url-row { display:flex; gap:8px; align-items:stretch; margin:8px 0 16px; }
		.url { flex:1; background:#f1f5f9; padding:12px; border-radius:10px; word-break:break-all; }
		.btn { display:inline-block; background:#0f766e; color:#fff; text-decoration:none; padding:10px 14px; border-radius:9px; margin-right:8px; }
		button.btn { border:none; cursor:pointer; margin-right:0; }
		@media (max-width: 640px) {
			.url-row { flex-direction:column; }
		}
	</style>
</head>
<body>
	<div class="wrap">
		<div class="card">
			<h1>Link generado correctamente</h1>
			<p><strong>Cliente:</strong> <?php echo html_escape($link['customer_name']); ?></p>
			<p><strong>Expira:</strong> <?php echo html_escape($link['expires_at']); ?></p>
			<div class="url-row">
				<div class="url" id="public-link-text"><?php echo html_escape($public_url); ?></div>
				<button type="button" class="btn" id="copy-link-btn">Copiar link</button>
			</div>
			<p>
				<a class="btn" href="<?php echo html_escape($public_url); ?>" target="_blank">Abrir catalogo</a>
				<a class="btn" href="<?php echo site_url('catalog/admin'); ?>">Volver al panel</a>
			</p>
		</div>
	</div>
	<script>
	(function() {
		var copyBtn = document.getElementById('copy-link-btn');
		var linkText = document.getElementById('public-link-text');
		if (!copyBtn || !linkText) return;
		var url = <?php echo json_encode($public_url); ?>;

		function copiedState() {
			copyBtn.textContent = 'Copiado';
			setTimeout(function() { copyBtn.textContent = 'Copiar link'; }, 1200);
		}

		copyBtn.addEventListener('click', function() {
			if (navigator.clipboard && window.isSecureContext) {
				navigator.clipboard.writeText(url).then(copiedState);
				return;
			}
			var ta = document.createElement('textarea');
			ta.value = url;
			ta.setAttribute('readonly', '');
			ta.style.position = 'absolute';
			ta.style.left = '-9999px';
			document.body.appendChild(ta);
			ta.select();
			document.execCommand('copy');
			document.body.removeChild(ta);
			copiedState();
		});
	})();
	</script>
</body>
</html>
