// La Plomería - Main JavaScript // Carrito de compras let cart = JSON.parse(localStorage.getItem('cart')) || []; // Función para agregar al carrito function addToCart(product) { const existingItem = cart.find(item => item.id === product.id); if (existingItem) { existingItem.quantity += 1; } else { cart.push({ ...product, quantity: 1 }); } localStorage.setItem('cart', JSON.stringify(cart)); updateCartUI(); showNotification('Producto agregado al carrito'); } // Actualizar UI del carrito function updateCartUI() { const cartBadge = document.getElementById('cart-badge'); const cartCount = document.getElementById('cart-count'); const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0); if (cartBadge) { cartBadge.textContent = totalItems; cartBadge.style.display = totalItems > 0 ? 'flex' : 'none'; } if (cartCount) { cartCount.textContent = totalItems; } } // Mostrar notificación function showNotification(message) { const notification = document.createElement('div'); notification.className = 'notification'; notification.textContent = message; notification.style.cssText = ` position: fixed; top: 80px; right: 20px; background: #28a745; color: white; padding: 1rem 2rem; border-radius: 0.5rem; box-shadow: 0 4px 12px rgba(0,0,0,0.2); z-index: 9999; animation: slideIn 0.3s ease; `; document.body.appendChild(notification); setTimeout(() => { notification.style.animation = 'slideOut 0.3s ease'; setTimeout(() => notification.remove(), 300); }, 3000); } // Filtrar productos function filterProducts(criteria) { const products = document.querySelectorAll('.product-card'); products.forEach(product => { const category = product.dataset.category; const brand = product.dataset.brand; const price = parseFloat(product.dataset.price); let show = true; if (criteria.category && criteria.category !== 'all' && category !== criteria.category) { show = false; } if (criteria.brand && brand !== criteria.brand) { show = false; } if (criteria.minPrice && price < criteria.minPrice) { show = false; } if (criteria.maxPrice && price > criteria.maxPrice) { show = false; } product.style.display = show ? 'block' : 'none'; }); } // Buscar productos function searchProducts(query) { const products = document.querySelectorAll('.product-card'); const searchTerm = query.toLowerCase(); products.forEach(product => { const title = product.querySelector('.product-title')?.textContent.toLowerCase() || ''; const description = product.querySelector('.product-description')?.textContent.toLowerCase() || ''; const matches = title.includes(searchTerm) || description.includes(searchTerm); product.style.display = matches ? 'block' : 'none'; }); } // Ordenar productos function sortProducts(sortBy) { const container = document.querySelector('.product-grid'); const products = Array.from(container.querySelectorAll('.product-card')); products.sort((a, b) => { switch(sortBy) { case 'price-low': return parseFloat(a.dataset.price) - parseFloat(b.dataset.price); case 'price-high': return parseFloat(b.dataset.price) - parseFloat(a.dataset.price); case 'name': return a.dataset.name.localeCompare(b.dataset.name); case 'newest': return new Date(b.dataset.date) - new Date(a.dataset.date); default: return 0; } }); products.forEach(product => container.appendChild(product)); } // Cambiar cantidad en detalle de producto function changeQuantity(change) { const input = document.getElementById('quantity'); const currentValue = parseInt(input.value) || 1; const newValue = Math.max(1, currentValue + change); input.value = newValue; updatePrice(newValue); } // Actualizar precio según cantidad function updatePrice(quantity) { const basePrice = parseFloat(document.getElementById('base-price')?.dataset.price || 0); const totalPrice = document.getElementById('total-price'); if (totalPrice) { totalPrice.textContent = `$${(basePrice * quantity).toFixed(2)}`; } } // Cambiar imagen principal en galería function changeMainImage(src) { const mainImage = document.getElementById('main-product-image'); if (mainImage) { mainImage.src = src; } document.querySelectorAll('.gallery-thumb').forEach(thumb => { thumb.classList.remove('active'); }); event.target.classList.add('active'); } // Validar formulario de contacto function validateContactForm(event) { event.preventDefault(); const form = event.target; const formData = new FormData(form); // Aquí iría la lógica de envío real console.log('Formulario enviado:', Object.fromEntries(formData)); showNotification('¡Mensaje enviado! Nos pondremos en contacto pronto.'); form.reset(); } // Smooth scroll para navegación document.addEventListener('DOMContentLoaded', function() { // Actualizar UI del carrito al cargar updateCartUI(); // Smooth scroll para links internos document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const target = document.querySelector(this.getAttribute('href')); if (target) { target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); // Navbar scroll effect const navbar = document.getElementById('navbar'); if (navbar) { window.addEventListener('scroll', function() { if (window.scrollY > 50) { navbar.classList.add('scrolled'); } else { navbar.classList.remove('scrolled'); } }); } // Initialize AOS if present if (typeof AOS !== 'undefined') { AOS.init({ duration: 1000, once: true, offset: 100 }); } // Hide loader const loader = document.getElementById('loader'); if (loader) { setTimeout(() => { loader.style.display = 'none'; }, 500); } }); // Animaciones de CSS adicionales const style = document.createElement('style'); style.textContent = ` @keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } } @keyframes slideOut { from { transform: translateX(0); opacity: 1; } to { transform: translateX(100%); opacity: 0; } } .navbar.scrolled { background: rgba(255, 255, 255, 0.95) !important; backdrop-filter: blur(10px); box-shadow: 0 4px 6px rgba(0,0,0,0.1); } `; document.head.appendChild(style);