function iniciarCalculadoraAlfombras(){const anchoInput = document.querySelector("#ancho");
const altoInput = document.querySelector("#alto");
const precioElemento = document.querySelector("#precio-calculado");
const botonFormulario = document.querySelector("#mostrar-formulario");if(!anchoInput || !altoInput || !precioElemento || !botonFormulario){
return false;
}// PRECIO POR METRO CUADRADO (IVA INCLUIDO)
const precioM2 = 12.10;// PRECIO MÍNIMO
const precioMinimo = 25;function calcularPrecio(){// VALORES
let ancho = parseFloat(anchoInput.value) || 0;
let alto = parseFloat(altoInput.value) || 0;// PASAR CM A M²
let metrosCuadrados = (ancho * alto) / 10000;// CALCULAR TOTAL
let total = metrosCuadrados * precioM2;// APLICAR PRECIO MÍNIMO
if(total > 0 && total < precioMinimo){
total = precioMinimo;
}// MOSTRAR PRECIO
precioElemento.innerHTML = total.toFixed(2) + " €";// MOSTRAR / OCULTAR BOTÓN
if(total > 0){botonFormulario.style.display = "flex";} else {botonFormulario.style.display = "none";}// CAMPOS OCULTOS ELEMENTORconst anchoHidden = document.querySelector('[name="form_fields[ancho_hidden]"]');
const altoHidden = document.querySelector('[name="form_fields[alto_hidden]"]');
const precioHidden = document.querySelector('[name="form_fields[precio_hidden]"]');if(anchoHidden){
anchoHidden.value = ancho;
}if(altoHidden){
altoHidden.value = alto;
}if(precioHidden){
precioHidden.value = total.toFixed(2) + " €";
}}// EVENTOS INPUT
anchoInput.addEventListener("input", calcularPrecio);
altoInput.addEventListener("input", calcularPrecio);// CALCULAR AL CARGAR
calcularPrecio();// BOTÓN MOSTRAR FORMULARIO
botonFormulario.addEventListener("click", function(){const formulario = document.querySelector(".formulario-oculto");if(formulario){formulario.style.display = "block";formulario.scrollIntoView({
behavior: "smooth",
block: "start"
});}});return true;}// ESPERAR A ELEMENTOR
const calculadoraObserver = setInterval(function(){const iniciada = iniciarCalculadoraAlfombras();if(iniciada){
clearInterval(calculadoraObserver);
}}, 500);