class DroneCart extends HTMLElement { constructor() { super(); this.cartItems = JSON.parse(localStorage.getItem('droneCart')) || []; } connectedCallback() { this.attachShadow({ mode: 'open' }); this.render(); this.addEventListeners(); this.style.position = 'fixed'; this.style.top = '0'; this.style.right = '-400px'; this.style.width = '400px'; this.style.height = '100vh'; this.style.zIndex = '1001'; this.style.transition = 'right 0.3s ease-in-out'; } render() { this.shadowRoot.innerHTML = `

Tu Carro

${this.cartItems.length > 0 ? this.renderCartItems() : this.renderEmptyCart()}
${this.cartItems.length > 0 ? this.renderCartFooter() : ''}
`; if (typeof feather !== 'undefined') { feather.replace(); } } renderCartItems() { return this.cartItems.map(item => `
${item.name}

${item.name}

$${item.price.toLocaleString('es-ES')}

${item.quantity}
`).join(''); } renderEmptyCart() { return `

Carro vacío

Agrega algunos drones para empezar

Personalizar Drone
`; } renderCartFooter() { const subtotal = this.cartItems.reduce((sum, item) => sum + (item.price * item.quantity), 0); const shipping = subtotal > 1000 ? 0 : 99; const total = subtotal + shipping; return ` `; } addEventListeners() { // Close cart this.shadowRoot.getElementById('close-cart')?.addEventListener('click', () => { this.style.right = '-400px'; document.getElementById('cart-overlay')?.classList.add('hidden', 'opacity-0'); }); // Cart item interactions this.shadowRoot.addEventListener('click', (e) => { const target = e.target.closest('button'); if (!target) return; const id = target.dataset.id; const item = this.cartItems.find(item => item.id === id); if (target.classList.contains('decrease-btn')) { if (item && item.quantity > 1) { item.quantity--; } else { this.cartItems = this.cartItems.filter(item => item.id !== id); } } else if (target.classList.contains('increase-btn')) { if (item) item.quantity++; } else if (target.classList.contains('remove-btn')) { this.cartItems = this.cartItems.filter(item => item.id !== id); } else if (target.id === 'checkout-btn') { alert('Redirigiendo al proceso de pago...'); return; } this.updateCart(); this.render(); if (typeof feather !== 'undefined') feather.replace(); }); // Listen for cart updates from customizer document.addEventListener('add-custom-build', (e) => { const { id, name, price } = e.detail; const existingItem = this.cartItems.find(item => item.id === id); if (existingItem) { existingItem.quantity++; } else { this.cartItems.push({ id, name, price, quantity: 1 }); } this.updateCart(); this.render(); if (typeof feather !== 'undefined') feather.replace(); }); // Listen for cart toggles document.addEventListener('open-cart', () => { this.style.right = '0'; // Create overlay if not exists let overlay = document.getElementById('cart-overlay'); if (!overlay) { overlay = document.createElement('div'); overlay.id = 'cart-overlay'; overlay.className = 'fixed inset-0 bg-black/50 z-50 transition-opacity opacity-0 hidden'; overlay.style.backdropFilter = 'blur(4px)'; document.body.appendChild(overlay); } overlay.classList.remove('hidden'); setTimeout(() => overlay.classList.remove('opacity-0'), 10); }); } updateCart() { localStorage.setItem('droneCart', JSON.stringify(this.cartItems)); // Dispatch event to update navbar count document.dispatchEvent(new CustomEvent('cart-updated')); } } customElements.define('drone-cart', DroneCart);