-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
175 lines (150 loc) · 5.18 KB
/
script.js
File metadata and controls
175 lines (150 loc) · 5.18 KB
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
let cart = [];
let cartTotal = 0;
document.addEventListener("DOMContentLoaded", () => {
// Menú móvil
const menuToggle = document.querySelector(".menu-toggle");
const navMenu = document.querySelector(".nav-menu");
if (menuToggle && navMenu) {
menuToggle.addEventListener("click", () => {
navMenu.classList.toggle("open");
});
navMenu.querySelectorAll("a").forEach(link => {
link.addEventListener("click", () => navMenu.classList.remove("open"));
});
}
// Carrito lateral
const cartPanel = document.getElementById("cart-panel");
const cartIconBtn = document.querySelector(".cart-icon-btn");
const cartCloseBtn = document.querySelector(".cart-close");
const cartCountEl = document.getElementById("cart-count");
const cartItemsEl = document.getElementById("cart-items");
const cartTotalEl = document.getElementById("cart-total");
const emptyTextEl = document.querySelector(".cart-empty-text");
function openCart() {
if (cartPanel) cartPanel.classList.add("open");
}
function closeCart() {
if (cartPanel) cartPanel.classList.remove("open");
}
if (cartIconBtn) cartIconBtn.addEventListener("click", openCart);
if (cartCloseBtn) cartCloseBtn.addEventListener("click", closeCart);
// Tabs del carrito
const cartTabs = document.querySelectorAll(".cart-tab");
const cartTabContents = document.querySelectorAll(".cart-tab-content");
cartTabs.forEach(tab => {
tab.addEventListener("click", () => {
const target = tab.dataset.tab;
cartTabs.forEach(t => t.classList.remove("active"));
cartTabContents.forEach(c => c.classList.remove("active"));
tab.classList.add("active");
document
.querySelector(`.cart-tab-content[data-tab-content="${target}"]`)
.classList.add("active");
});
});
// Agregar al carrito
const addButtons = document.querySelectorAll(".add-to-cart");
addButtons.forEach(btn => {
btn.addEventListener("click", () => {
const name = btn.dataset.name;
const price = parseFloat(btn.dataset.price || "0");
if (!name || isNaN(price)) return;
const existing = cart.find(item => item.name === name);
if (existing) {
existing.qty += 1;
} else {
cart.push({ name, price, qty: 1 });
}
updateCartUI();
openCart();
});
});
function updateCartUI() {
// Cantidad
const totalItems = cart.reduce((sum, item) => sum + item.qty, 0);
if (cartCountEl) cartCountEl.textContent = totalItems.toString();
// Lista
if (!cartItemsEl) return;
cartItemsEl.innerHTML = "";
if (cart.length === 0) {
if (emptyTextEl) emptyTextEl.style.display = "block";
} else {
if (emptyTextEl) emptyTextEl.style.display = "none";
cart.forEach((item, index) => {
const li = document.createElement("li");
li.innerHTML = `
<span>${item.name} x${item.qty}</span>
<span>
$${(item.price * item.qty).toFixed(2)}
<button data-index="${index}" aria-label="Quitar del carrito">x</button>
</span>
`;
cartItemsEl.appendChild(li);
});
// Botones quitar
cartItemsEl.querySelectorAll("button").forEach(btn => {
btn.addEventListener("click", () => {
const i = parseInt(btn.dataset.index, 10);
if (!isNaN(i)) {
cart.splice(i, 1);
updateCartUI();
}
});
});
}
// Total
cartTotal = cart.reduce((sum, item) => sum + item.price * item.qty, 0);
if (cartTotalEl) cartTotalEl.textContent = cartTotal.toFixed(2);
}
// PAYPAL
if (window.paypal) {
try {
window.paypal
.Buttons({
style: {
layout: "vertical",
color: "gold",
shape: "rect",
label: "paypal",
},
createOrder: function (data, actions) {
const amount = cartTotal > 0 ? cartTotal.toFixed(2) : "0.01"; // mínimo
return actions.order.create({
purchase_units: [
{
amount: {
value: amount,
currency_code: "USD",
},
description: "Planes Mily Productions",
},
],
});
},
onApprove: function (data, actions) {
return actions.order.capture().then(function () {
alert("¡Pago realizado con éxito! Nos pondremos en contacto contigo.");
cart = [];
updateCartUI();
});
},
onError: function (err) {
console.error("Error PayPal:", err);
},
})
.render("#paypal-button-container");
} catch (e) {
console.error("No se pudo inicializar PayPal:", e);
}
}
// FORMULARIO - MENSAJE DE AGRADECIMIENTO
const contactForm = document.querySelector(".contact-form");
const successMsg = document.getElementById("form-success");
if (contactForm && successMsg) {
contactForm.addEventListener("submit", function (e) {
e.preventDefault(); // Evita recarga
successMsg.style.display = "block"; // Mostrar mensaje
contactForm.reset(); // Limpiar formulario
});
}
});