Files
Portal/calculadora-ahorro-luz.html
2026-10-03 22:14:01 +00:00

280 lines
14 KiB
HTML

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculadora de Ahorro en tu Factura de Luz</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<style>
:root { --primary: #2563eb; --secondary: #f59e0b; --app-bg: #f8fafc; }
html, body { background: transparent; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
.fade-in { animation: fadeIn 0.5s ease-out forwards; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(15px); } to { opacity: 1; transform: translateY(0); } }
.dropzone { transition: all 0.2s ease-in-out; }
.dropzone.drag-over { border-color: var(--primary); background-color: #eff6ff; }
.spinner { border: 3px solid rgba(255,255,255,0.3); border-top-color: #fff; border-radius: 50%; width: 18px; height: 18px; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
/* Honeypot: oculto para humanos, visible para bots que leen el DOM */
.hp-field { position: absolute; left: -9999px; top: -9999px; width: 1px; height: 1px; overflow: hidden; }
</style>
</head>
<body class="bg-transparent text-slate-800">
<div class="max-w-xl mx-auto p-4 sm:p-6">
<!-- VISTA: FORMULARIO -->
<div id="vistaForm" class="bg-white rounded-[2rem] shadow-xl border border-slate-100 p-6 sm:p-8 fade-in">
<div class="flex items-center gap-3 mb-2">
<div class="bg-amber-100 p-2.5 rounded-2xl text-amber-600"><i data-lucide="zap" class="w-6 h-6"></i></div>
<div>
<h1 class="text-lg sm:text-xl font-black text-slate-900 leading-tight">¿Cuánto puedes ahorrar en tu factura de luz?</h1>
</div>
</div>
<p class="text-sm text-slate-500 font-medium mt-2 mb-6">Sube tu última factura y te decimos, en menos de 24h, si puedes pagar menos. Gratis y sin compromiso.</p>
<form id="leadForm" novalidate>
<!-- Honeypot anti-bot (no tocar) -->
<div class="hp-field" aria-hidden="true">
<label>No rellenar este campo</label>
<input type="text" id="honeypot" name="honeypot" tabindex="-1" autocomplete="off">
</div>
<div class="space-y-4">
<div>
<label class="text-[11px] font-bold text-slate-500 uppercase ml-1">Teléfono *</label>
<input type="tel" id="telefono" required placeholder="600 000 000" class="w-full mt-1 p-4 bg-slate-50 rounded-2xl border border-slate-200 text-sm font-bold text-slate-800 placeholder:text-slate-300 placeholder:font-medium focus:outline-none focus:border-blue-400 focus:ring-2 focus:ring-blue-100">
</div>
<div>
<label class="text-[11px] font-bold text-slate-500 uppercase ml-1">Email *</label>
<input type="email" id="email" required placeholder="tunombre@email.com" class="w-full mt-1 p-4 bg-slate-50 rounded-2xl border border-slate-200 text-sm font-bold text-slate-800 placeholder:text-slate-300 placeholder:font-medium focus:outline-none focus:border-blue-400 focus:ring-2 focus:ring-blue-100">
</div>
<div>
<label class="text-[11px] font-bold text-slate-500 uppercase ml-1">Código postal</label>
<input type="text" id="codigoPostal" inputmode="numeric" maxlength="5" placeholder="28001" class="w-full mt-1 p-4 bg-slate-50 rounded-2xl border border-slate-200 text-sm font-bold text-slate-800 placeholder:text-slate-300 placeholder:font-medium focus:outline-none focus:border-blue-400 focus:ring-2 focus:ring-blue-100">
</div>
<div>
<label class="text-[11px] font-bold text-slate-500 uppercase ml-1">Tu factura (PDF, JPG o PNG, máx. 6MB) *</label>
<div id="dropzone" class="dropzone mt-1 border-2 border-dashed border-slate-200 rounded-2xl p-6 text-center cursor-pointer bg-slate-50">
<input type="file" id="archivoInput" accept="application/pdf,image/jpeg,image/jpg,image/png" class="hidden">
<div id="dropzoneEmpty">
<i data-lucide="upload-cloud" class="w-8 h-8 mx-auto text-slate-400 mb-2"></i>
<p class="text-xs font-bold text-slate-500">Arrastra tu factura aquí o toca para elegir el archivo</p>
</div>
<div id="dropzoneFile" class="hidden">
<i data-lucide="file-check-2" class="w-8 h-8 mx-auto text-emerald-500 mb-2"></i>
<p id="archivoNombre" class="text-xs font-bold text-slate-700 break-all"></p>
<button type="button" id="btnQuitarArchivo" class="text-[11px] font-bold text-rose-500 uppercase mt-2">Quitar archivo</button>
</div>
</div>
<p id="archivoError" class="hidden text-[11px] font-bold text-rose-500 mt-1.5 ml-1"></p>
</div>
<label class="flex items-start gap-3 cursor-pointer pt-2">
<input type="checkbox" id="rgpd" required class="mt-1 w-4 h-4 shrink-0 accent-blue-600">
<span class="text-[11px] font-medium text-slate-500 leading-snug">He leído y acepto la <a href="https://www.marsalva.es/politica-de-privacidad" target="_blank" rel="noopener" class="text-blue-600 font-bold underline">política de privacidad</a> y autorizo el tratamiento de mis datos para recibir este estudio de ahorro. *</span>
</label>
<p id="formError" class="hidden text-xs font-bold text-rose-500 bg-rose-50 border border-rose-100 rounded-xl p-3"></p>
<button type="submit" id="btnEnviar" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-black py-4 rounded-2xl text-sm uppercase tracking-wide active:scale-[0.98] transition-all flex items-center justify-center gap-2 shadow-lg shadow-blue-600/20">
<span id="btnEnviarTexto">Quiero saber cuánto puedo ahorrar</span>
</button>
<p class="text-center text-[10px] font-medium text-slate-400 mt-1">No compartimos tus datos con terceros. Solo los usamos para enviarte tu estudio de ahorro.</p>
</div>
</form>
</div>
<!-- VISTA: ÉXITO -->
<div id="vistaExito" class="hidden bg-white rounded-[2rem] shadow-xl border border-slate-100 p-8 text-center fade-in">
<div class="bg-emerald-100 text-emerald-600 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-4">
<i data-lucide="check-check" class="w-8 h-8"></i>
</div>
<h2 class="text-lg font-black text-slate-900">¡Factura recibida!</h2>
<p id="mensajeExito" class="text-sm text-slate-500 font-medium mt-2">Te enviamos tu estudio de ahorro en menos de 24h.</p>
</div>
</div>
<script>
const API_URL = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
? 'http://localhost:3000'
: 'https://integrarepara-api.integrarepara.es';
const MAX_BYTES = 6 * 1024 * 1024;
const ALLOWED_TYPES = ['application/pdf', 'image/jpeg', 'image/jpg', 'image/png'];
let archivoSeleccionado = null;
const dropzone = document.getElementById('dropzone');
const archivoInput = document.getElementById('archivoInput');
const dropzoneEmpty = document.getElementById('dropzoneEmpty');
const dropzoneFile = document.getElementById('dropzoneFile');
const archivoNombre = document.getElementById('archivoNombre');
const archivoError = document.getElementById('archivoError');
function mostrarErrorArchivo(msg) {
archivoError.textContent = msg;
archivoError.classList.remove('hidden');
}
function limpiarErrorArchivo() {
archivoError.classList.add('hidden');
archivoError.textContent = '';
}
function seleccionarArchivo(file) {
limpiarErrorArchivo();
if (!file) return;
if (!ALLOWED_TYPES.includes(file.type)) {
mostrarErrorArchivo('Formato no soportado. Solo admitimos PDF, JPG o PNG.');
return;
}
if (file.size > MAX_BYTES) {
mostrarErrorArchivo('El archivo pesa demasiado (máximo 6MB).');
return;
}
archivoSeleccionado = file;
archivoNombre.textContent = file.name;
dropzoneEmpty.classList.add('hidden');
dropzoneFile.classList.remove('hidden');
}
dropzone.addEventListener('click', (e) => {
if (e.target.id !== 'btnQuitarArchivo') archivoInput.click();
});
archivoInput.addEventListener('change', () => seleccionarArchivo(archivoInput.files[0]));
document.getElementById('btnQuitarArchivo').addEventListener('click', (e) => {
e.stopPropagation();
archivoSeleccionado = null;
archivoInput.value = '';
dropzoneEmpty.classList.remove('hidden');
dropzoneFile.classList.add('hidden');
});
['dragover'].forEach(evt => dropzone.addEventListener(evt, (e) => {
e.preventDefault();
dropzone.classList.add('drag-over');
}));
['dragleave', 'drop'].forEach(evt => dropzone.addEventListener(evt, (e) => {
e.preventDefault();
dropzone.classList.remove('drag-over');
}));
dropzone.addEventListener('drop', (e) => {
const file = e.dataTransfer.files[0];
if (file) seleccionarArchivo(file);
});
function fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const result = reader.result;
const base64 = result.split(',')[1];
resolve(base64);
};
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
const formError = document.getElementById('formError');
function mostrarErrorForm(msg) {
formError.textContent = msg;
formError.classList.remove('hidden');
}
function limpiarErrorForm() {
formError.classList.add('hidden');
formError.textContent = '';
}
document.getElementById('leadForm').addEventListener('submit', async (e) => {
e.preventDefault();
limpiarErrorForm();
const telefono = document.getElementById('telefono').value.trim();
const email = document.getElementById('email').value.trim();
const codigoPostal = document.getElementById('codigoPostal').value.trim();
const rgpdAceptado = document.getElementById('rgpd').checked;
const honeypot = document.getElementById('honeypot').value.trim();
if (!telefono || !email) {
mostrarErrorForm('Por favor, completa el teléfono y el email.');
return;
}
if (!archivoSeleccionado) {
mostrarErrorForm('Por favor, adjunta tu factura.');
return;
}
if (!rgpdAceptado) {
mostrarErrorForm('Debes aceptar la política de privacidad para continuar.');
return;
}
const btn = document.getElementById('btnEnviar');
const btnTexto = document.getElementById('btnEnviarTexto');
btn.disabled = true;
btn.classList.add('opacity-70');
btnTexto.innerHTML = '<span class="spinner inline-block align-middle mr-2"></span> Enviando...';
try {
const archivoBase64 = await fileToBase64(archivoSeleccionado);
const res = await fetch(`${API_URL}/public/energy-quote`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email, telefono, codigoPostal, rgpdAceptado, honeypot,
archivoBase64, archivoMime: archivoSeleccionado.type,
}),
});
const data = await res.json();
if (!data.ok) {
mostrarErrorForm(data.error || 'No hemos podido procesar tu factura. Inténtalo de nuevo.');
btn.disabled = false;
btn.classList.remove('opacity-70');
btnTexto.textContent = 'Quiero saber cuánto puedo ahorrar';
return;
}
document.getElementById('mensajeExito').textContent = data.message || 'Te enviamos tu estudio de ahorro en menos de 24h.';
document.getElementById('vistaForm').classList.add('hidden');
document.getElementById('vistaExito').classList.remove('hidden');
lucide.createIcons();
notificarAlturaPadre();
} catch (err) {
mostrarErrorForm('Error de conexión. Inténtalo de nuevo en unos minutos.');
btn.disabled = false;
btn.classList.remove('opacity-70');
btnTexto.textContent = 'Quiero saber cuánto puedo ahorrar';
}
});
// Avisa a la página que nos embebe (iframe en marsalva.es) de la altura real,
// para que pueda ajustar el alto del iframe si escucha este mensaje.
function notificarAlturaPadre() {
try {
const altura = document.body.scrollHeight;
window.parent.postMessage({ tipo: 'calculadora-ahorro-luz-altura', altura }, '*');
} catch (e) { /* noop */ }
}
document.addEventListener('DOMContentLoaded', () => {
lucide.createIcons();
notificarAlturaPadre();
new ResizeObserver(notificarAlturaPadre).observe(document.body);
});
</script>
</body>
</html>