File size: 7,525 Bytes
6fd26a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Main JavaScript for DronesLibres.ai

// Shopping Cart Logic
class ShoppingCart {
    constructor() {
        this.items = JSON.parse(localStorage.getItem('droneCart')) || [];
        this.updateCartUI();
    }

    addItem(id, name, price, quantity = 1) {
        const existingItem = this.items.find(item => item.id === id);
        if (existingItem) {
            existingItem.quantity += quantity;
        } else {
            this.items.push({ id, name, price, quantity });
        }
        this.save();
        this.updateCartUI();
        this.showNotification(`✓ ${name} agregado al carro`);
    }

    removeItem(id) {
        this.items = this.items.filter(item => item.id !== id);
        this.save();
        this.updateCartUI();
    }

    updateQuantity(id, quantity) {
        const item = this.items.find(item => item.id === id);
        if (item) {
            item.quantity = quantity;
            if (item.quantity <= 0) {
                this.removeItem(id);
            } else {
                this.save();
                this.updateCartUI();
            }
        }
    }

    getTotal() {
        return this.items.reduce((total, item) => total + (item.price * item.quantity), 0);
    }

    getItemCount() {
        return this.items.reduce((count, item) => count + item.quantity, 0);
    }

    save() {
        localStorage.setItem('droneCart', JSON.stringify(this.items));
    }

    updateCartUI() {
        const cartCount = document.getElementById('cart-count');
        const cartTotal = document.getElementById('cart-total');
        const cartItems = document.getElementById('cart-items');
        
        if (cartCount) cartCount.textContent = this.getItemCount();
        if (cartTotal) cartTotal.textContent = `$${this.getTotal().toLocaleString('es-ES')}`;
        
        // Update cart items in sidebar if exists
        if (cartItems && document.querySelector('drone-cart')) {
            cartItems.innerHTML = this.items.map(item => `
                <div class="flex items-center justify-between p-4 border-b">
                    <div>
                        <h4 class="font-semibold">${item.name}</h4>
                        <p class="text-gray-600">$${item.price.toLocaleString('es-ES')} x ${item.quantity}</p>
                    </div>
                    <div class="flex items-center gap-2">
                        <button class="decrease-item px-2 py-1 bg-gray-200 rounded" data-id="${item.id}">-</button>
                        <span>${item.quantity}</span>
                        <button class="increase-item px-2 py-1 bg-gray-200 rounded" data-id="${item.id}">+</button>
                        <button class="remove-item ml-4 text-red-500" data-id="${item.id}">
                            <i data-feather="trash-2" class="w-4 h-4"></i>
                        </button>
                    </div>
                </div>
            `).join('');
            feather.replace({ 'data-feather': 'trash-2' });
        }
    }

    showNotification(message) {
        // Create notification element
        const notification = document.createElement('div');
        notification.className = 'fixed top-4 right-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg z-50 transform translate-x-0 transition-transform duration-300';
        notification.textContent = message;
        document.body.appendChild(notification);
        
        // Remove after 3 seconds
        setTimeout(() => {
            notification.style.transform = 'translateX(100%)';
            setTimeout(() => notification.remove(), 300);
        }, 3000);
    }
}

// Initialize cart
const cart = new ShoppingCart();

// DOM Content Loaded
document.addEventListener('DOMContentLoaded', function() {
    // Add to cart buttons
    document.addEventListener('click', function(e) {
        if (e.target.classList.contains('add-to-cart')) {
            const button = e.target;
            const id = button.dataset.id;
            const name = button.dataset.name;
            const price = parseInt(button.dataset.price);
            
            cart.addItem(id, name, price, 1);
        }
        
        // Cart sidebar interactions
        if (e.target.classList.contains('decrease-item') || e.target.closest('.decrease-item')) {
            const button = e.target.classList.contains('decrease-item') ? e.target : e.target.closest('.decrease-item');
            const id = button.dataset.id;
            const item = cart.items.find(item => item.id === id);
            if (item) cart.updateQuantity(id, item.quantity - 1);
        }
        
        if (e.target.classList.contains('increase-item') || e.target.closest('.increase-item')) {
            const button = e.target.classList.contains('increase-item') ? e.target : e.target.closest('.increase-item');
            const id = button.dataset.id;
            const item = cart.items.find(item => item.id === id);
            if (item) cart.updateQuantity(id, item.quantity + 1);
        }
        
        if (e.target.classList.contains('remove-item') || e.target.closest('.remove-item')) {
            const button = e.target.classList.contains('remove-item') ? e.target : e.target.closest('.remove-item');
            const id = button.dataset.id;
            cart.removeItem(id);
        }
    });

    // Toggle cart sidebar
    document.addEventListener('click', function(e) {
        if (e.target.closest('#cart-toggle')) {
            const cartSidebar = document.querySelector('drone-cart');
            if (cartSidebar) {
                const isHidden = cartSidebar.style.right === '-400px' || !cartSidebar.style.right;
                cartSidebar.style.right = isHidden ? '0' : '-400px';
                
                // Update overlay
                const overlay = document.getElementById('cart-overlay');
                if (overlay) {
                    overlay.classList.toggle('hidden', !isHidden);
                    overlay.classList.toggle('opacity-0', !isHidden);
                }
            }
        }
    });

    // Close cart when clicking overlay
    document.addEventListener('click', function(e) {
        if (e.target.id === 'cart-overlay') {
            const cartSidebar = document.querySelector('drone-cart');
            const overlay = document.getElementById('cart-overlay');
            if (cartSidebar) cartSidebar.style.right = '-400px';
            if (overlay) {
                overlay.classList.add('hidden', 'opacity-0');
            }
        }
    });

    // Initialize Feather Icons
    if (typeof feather !== 'undefined') {
        feather.replace();
    }
});

// API Integration for drone parts (example)
async function fetchDroneParts(category) {
    try {
        // This would be replaced with actual API endpoint
        const response = await fetch(`https://api.example.com/drone-parts?category=${category}`);
        const data = await response.json();
        return data;
    } catch (error) {
        console.error('Error fetching drone parts:', error);
        return [];
    }
}

// Form validation for customizer
function validateCustomizerForm() {
    const frames = document.querySelectorAll('input[name="frame"]:checked');
    const motors = document.querySelectorAll('input[name="motor"]:checked');
    
    if (frames.length === 0) {
        alert('Por favor selecciona un frame.');
        return false;
    }
    
    if (motors.length === 0) {
        alert('Por favor selecciona motores.');
        return false;
    }
    
    return true;
}