0 ? product.sizes[0] : null, isHalfHalf: false, halfLeftProduct: null, halfRightProduct: null, selectedExtras: this.extras.map(extra => ({ ...extra, quantity: 0 })) // Initialize extras for selection }; if (product.category === 'pizza' && product.sizes.length > 0) { this.selectedProduct.halfLeftProduct = product; // Default to current pizza this.selectedProduct.halfRightProduct = product; // Default to current pizza } this.showProductModal = true; }, // Calculate current item price for modal display calculateCurrentItemPrice() { if (!this.selectedProduct || !this.selectedProduct.selectedSize) return 0; let basePrice = 0; if (this.selectedProduct.isHalfHalf && this.selectedProduct.halfLeftProduct && this.selectedProduct.halfRightProduct) { const leftPrice = this.getProductPriceForSize(this.selectedProduct.halfLeftProduct, this.selectedProduct.selectedSize.id); const rightPrice = this.getProductPriceForSize(this.selectedProduct.halfRightProduct, this.selectedProduct.selectedSize.id); basePrice = Math.max(leftPrice, rightPrice); // Rule: take the price of the most expensive half } else { basePrice = this.selectedProduct.selectedSize.price; } let extrasPrice = this.selectedProduct.selectedExtras.reduce((sum, extra) => sum + (extra.price * extra.quantity), 0); return (basePrice + extrasPrice) * this.selectedProduct.quantity; }, // Helper to get a product's price for a specific size getProductPriceForSize(product, sizeId) { const size = product.sizes.find(s => s.id === sizeId); return size ? size.price : 0; }, // Find a product by its ID findProductById(productId) { return this.products.find(p => p.id === productId); }, // Add item to cart addToCart() { if (!this.selectedProduct || !this.selectedProduct.selectedSize) return; const itemToAdd = { productId: this.selectedProduct.id, productName: this.selectedProduct.name, productImage: this.selectedProduct.image, quantity: this.selectedProduct.quantity, sizeId: this.selectedProduct.selectedSize.id, sizeName: this.selectedProduct.selectedSize.name, unitPrice: this.calculateCurrentItemPrice() / this.selectedProduct.quantity, // Price per single item isHalfHalf: this.selectedProduct.isHalfHalf, halfLeftProductId: this.selectedProduct.isHalfHalf ? this.selectedProduct.halfLeftProduct.id : null, halfLeftProductName: this.selectedProduct.isHalfHalf ? this.selectedProduct.halfLeftProduct.name : null, halfRightProductId: this.selectedProduct.isHalfHalf ? this.selectedProduct.halfRightProduct.id : null, halfRightProductName: this.selectedProduct.isHalfHalf ? this.selectedProduct.halfRightProduct.name : null, extras: this.selectedProduct.selectedExtras.filter(extra => extra.quantity > 0).map(extra => ({ id: extra.id, name: extra.name, quantity: extra.quantity, price: extra.price })) }; // Check if item already exists in cart and update quantity const existingItemIndex = this.cart.findIndex(item => item.productId === itemToAdd.productId && item.sizeId === itemToAdd.sizeId && item.isHalfHalf === itemToAdd.isHalfHalf && item.halfLeftProductId === itemToAdd.halfLeftProductId && item.halfRightProductId === itemToAdd.halfRightProductId && JSON.stringify(item.extras) === JSON.stringify(itemToAdd.extras) ); if (existingItemIndex !== -1) { this.cart[existingItemIndex].quantity += itemToAdd.quantity; } else { this.cart.push(itemToAdd); } this.saveCart(); this.showProductModal = false; }, // Remove item from cart removeFromCart(index) { this.cart.splice(index, 1); this.saveCart(); }, // Update quantity in cart updateCartItemQuantity(index, newQuantity) { if (newQuantity > 0) { this.cart[index].quantity = newQuantity; } else { this.cart.splice(index, 1); } this.saveCart(); }, // Calculate total cart price cartTotal() { return this.cart.reduce((sum, item) => { let itemBasePrice = item.unitPrice; // unitPrice already includes half-half logic let itemExtrasPrice = item.extras.reduce((extraSum, extra) => extraSum + (extra.price * extra.quantity), 0); return sum + ((itemBasePrice + itemExtrasPrice) * item.quantity); }, 0); }, // Close all modals closeModals() { this.showProductModal = false; this.showCartModal = false; this.showOrderForm = false; this.showAdminModal = false; this.orderConfirmed = false; }, // Admin Panel Access tryAdminAccess() { if (this.adminPassword === 'admin123') { // Simple password for demo window.location.href = '/admin/kds'; } else { this.adminError = 'Contraseña incorrecta.'; } }, // Submit Order async submitOrder() { if (this.cart.length === 0) { alert('Tu carrito está vacío.'); return; } if (!this.customerName || !this.customerPhone || (this.deliveryType === 'delivery' && !this.customerAddress)) { alert('Por favor, completa todos los campos requeridos.'); return; } const orderData = { customerName: this.customerName, customerPhone: this.customerPhone, customerAddress: this.deliveryType === 'delivery' ? this.customerAddress : 'Recogida en local', deliveryType: this.deliveryType, paymentMethod: this.paymentMethod, notes: this.notes, totalAmount: this.cartTotal(), items: this.cart.map(item => ({ productId: item.productId, quantity: item.quantity, sizeId: item.sizeId, isHalfHalf: item.isHalfHalf, halfLeftProductId: item.halfLeftProductId, halfRightProductId: item.halfRightProductId, extras: item.extras.map(extra => ({ id: extra.id, quantity: extra.quantity })) })) }; try { const response = await fetch('/order', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }, body: JSON.stringify(orderData) }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || 'Error al procesar el pedido.'); } const result = await response.json(); console.log('Pedido enviado:', result); this.orderConfirmed = true; this.cart = []; this.saveCart(); // Optionally, clear form fields after successful order this.customerName = ''; this.customerPhone = ''; this.customerAddress = ''; this.notes = ''; this.deliveryType = 'delivery'; this.paymentMethod = 'cash'; } catch (error) { console.error('Error al enviar el pedido:', error); alert('Hubo un error al enviar tu pedido: ' + error.message); } }, // Share order via WhatsApp shareOrderWhatsApp() { let message = `¡Hola! Mi pedido en Pizzería Deliziosa:\n\n`; this.cart.forEach(item => { let itemDesc = `${item.quantity}x ${item.productName} (${item.sizeName})`; if (item.isHalfHalf) { itemDesc += ` - Mitad ${item.halfLeftProductName} / Mitad ${item.halfRightProductName}`; } if (item.extras.length > 0) { itemDesc += ` + Extras: ${item.extras.map(e => `${e.quantity}x ${e.name}`).join(', ')}`; } message += `- ${itemDesc}\n`; }); message += `\nTotal: ${this.cartTotal().toFixed(2)}€\n`; message += `\nDatos de Contacto:\nNombre: ${this.customerName}\nTeléfono: ${this.customerPhone}\n`; message += `Tipo de entrega: ${this.deliveryType === 'delivery' ? 'Domicilio' : 'Recogida en local'}\n`; if (this.deliveryType === 'delivery') { message += `Dirección: ${this.customerAddress}\n`; } message += `Método de pago: ${this.paymentMethod === 'cash' ? 'Efectivo' : 'Tarjeta'}\n`; if (this.notes) { message += `Notas: ${this.notes}\n`; } message += `\n¡Gracias!`; const whatsappUrl = `https://wa.me/XXXXXXXXXXX?text=${encodeURIComponent(message)}`; // Replace XXXXXXXXXXX with your WhatsApp number window.open(whatsappUrl, '_blank'); }, // Share order via Email shareOrderEmail() { const subject = encodeURIComponent('Pedido Pizzería Deliziosa'); let body = `¡Hola! Mi pedido en Pizzería Deliziosa:\n\n`; this.cart.forEach(item => { let itemDesc = `${item.quantity}x ${item.productName} (${item.sizeName})`; if (item.isHalfHalf) { itemDesc += ` - Mitad ${item.halfLeftProductName} / Mitad ${item.halfRightProductName}`; } if (item.extras.length > 0) { itemDesc += ` + Extras: ${item.extras.map(e => `${e.quantity}x ${e.name}`).join(', ')}`; } body += `- ${itemDesc}\n`; }); body += `\nTotal: ${this.cartTotal().toFixed(2)}€\n`; body += `\nDatos de Contacto:\nNombre: ${this.customerName}\nTeléfono: ${this.customerPhone}\n`; body += `Tipo de entrega: ${this.deliveryType === 'delivery' ? 'Domicilio' : 'Recogida en local'}\n`; if (this.deliveryType === 'delivery') { body += `Dirección: ${this.customerAddress}\n`; } body += `Método de pago: ${this.paymentMethod === 'cash' ? 'Efectivo' : 'Tarjeta'}\n`; if (this.notes) { body += `Notas: ${this.notes}\n`; } body += `\n¡Gracias!`; const mailtoUrl = `mailto:tu_email@example.com?subject=${subject}&body=${encodeURIComponent(body)}`; // Replace tu_email@example.com with your email window.location.href = mailtoUrl; }, // Filter products by category filteredProducts(category) { return this.products.filter(p => p.category === category); }, // Get pizza product options for half-half selection getPizzaOptions() { return this.products.filter(p => p.category === 'pizza'); } }" x-init="init()">

Pizzería Deliziosa

Pizzas

Bebidas

Postres

Imagen de producto

Tu Carrito

Tu carrito está vacío.

Total:

Detalles del Pedido

¡Pedido Confirmado!

Gracias por tu pedido. Lo estamos preparando.

Total:

Acceso a Administración