-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
298 lines (263 loc) · 9.74 KB
/
Copy pathscript.js
File metadata and controls
298 lines (263 loc) · 9.74 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
function toggleDark() {
const html = document.documentElement;
const isDark = html.classList.toggle("dark");
const btn = document.getElementById("dark-mode-btn");
// Cambiar el icono según el modo
if (isDark) {
btn.textContent = "light_mode";
} else {
btn.textContent = "dark_mode";
}
// Guardar preferencia en localStorage
localStorage.setItem("darkMode", isDark ? "enabled" : "disabled");
}
// Cargar preferencia guardada al cargar la página
document.addEventListener("DOMContentLoaded", () => {
const savedMode = localStorage.getItem("darkMode");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
if (savedMode === "enabled" || (!savedMode && prefersDark)) {
document.documentElement.classList.add("dark");
const btn = document.getElementById("dark-mode-btn");
if (btn) btn.textContent = "light_mode";
}
});
// Lista de productos iniciales con imágenes
const products = [
{
name: "Lechuga Mantecosa",
price: "$2.50",
cultivationCost: "$0.80",
description: "Hojas suaves, sabor mantecoso, ideal para ensaladas.",
tier: "Nivel 1 • Botella PET #04",
growth: 65,
stage: "Vegetativa",
moisture: 72,
water: "120ml / día",
icon: "eco",
image: "./assets/img/img1.jpeg"
},
{
name: "Albahaca Italiana",
price: "$1.80",
cultivationCost: "$0.60",
description: "Aroma intenso, perfecta para pesto y salsas.",
tier: "Nivel 2 • Botella PET #08",
growth: 25,
stage: "Plántula",
moisture: 68,
water: "85ml / día",
icon: "local_florist",
image: "./assets/img/img2.jpeg"
},
{
name: "Espinaca Baby",
price: "$3.00",
cultivationCost: "$1.20",
description: "Hojas tiernas, alto contenido de hierro.",
tier: "Nivel 3 • Botella PET #12",
growth: 95,
stage: "Madura",
moisture: 70,
water: "140ml / día",
icon: "nutrition",
image: "./assets/img/img3.jpeg"
}
];
const cropList = document.getElementById("crop-list");
const addPlantBtn = document.getElementById("add-plant-btn");
function renderPlant(plant) {
const li = document.createElement("li");
li.className = "p-4 flex flex-col md:flex-row md:justify-between md:items-center gap-4";
li.innerHTML = `
<div class="flex items-center gap-4 min-w-[240px]">
${plant.image ?
`<img src="${plant.image}" alt="${plant.name}" class="w-16 h-16 rounded-full object-cover border-2 border-green-200 dark:border-green-800 shadow-sm" onerror="this.style.display='none'; this.nextElementSibling.style.display='flex';">
<div class="w-16 h-16 rounded-full bg-green-100 dark:bg-green-900 flex items-center justify-center text-green-700 dark:text-green-300" style="display:none;">
<span class="material-symbols-outlined text-3xl">${plant.icon}</span>
</div>` :
`<div class="w-16 h-16 rounded-full bg-green-100 dark:bg-green-900 flex items-center justify-center text-green-700 dark:text-green-300">
<span class="material-symbols-outlined text-3xl">${plant.icon}</span>
</div>`
}
<div>
<h4 class="text-lg font-bold text-slate-900 dark:text-slate-100">${plant.name}</h4>
<p class="text-sm text-slate-500 dark:text-slate-400">${plant.tier}</p>
${plant.price ? `<p class="text-sm text-green-600 font-semibold mt-1">Venta: ${plant.price}</p>` : ''}
${plant.cultivationCost ? `<p class="text-sm text-orange-500 font-semibold">Cultivo: ${plant.cultivationCost}</p>` : ''}
${plant.description ? `<p class="text-sm text-slate-600 dark:text-slate-400 mt-1">${plant.description}</p>` : ''}
</div>
</div>
<div class="grid grid-cols-2 md:grid-cols-3 gap-4 flex-1">
<div>
<p class="text-xs font-bold text-slate-400 dark:text-slate-500 uppercase mb-1">Etapa</p>
<div class="flex items-center gap-2">
<span class="text-sm font-bold dark:text-slate-200">${plant.stage}</span>
<div class="w-20 h-2 bg-gray-100 dark:bg-slate-700 rounded-full">
<div class="h-full bg-green-500 rounded-full" style="width: ${plant.growth}%;"></div>
</div>
</div>
</div>
<div>
<p class="text-xs font-bold text-slate-400 dark:text-slate-500 uppercase mb-1">Humedad</p>
<div class="flex items-center gap-1">
<span class="material-symbols-outlined text-blue-400 dark:text-blue-300 text-sm">water_drop</span>
<span class="text-sm font-bold dark:text-slate-200">${plant.moisture}%</span>
</div>
</div>
<div class="hidden md:block">
<p class="text-xs font-bold text-slate-400 dark:text-slate-500 uppercase mb-1">Consumo</p>
<p class="text-sm font-bold dark:text-slate-200">${plant.water}</p>
</div>
</div>
`;
cropList.appendChild(li);
}
products.forEach(renderPlant);
addPlantBtn.addEventListener("click", () => {
const newPlant = {
name: "Nueva Planta",
tier: "Nivel X • Botella PET #XX",
growth: 10,
stage: "Plántula",
moisture: 60,
water: "90ml / día",
icon: "spa"
};
renderPlant(newPlant);
});
// Mostrar resumen del sistema
function exportarDatos() {
const agua = document.getElementById("nivel-agua")?.textContent || "N/A";
const humedad = document.getElementById("humedad")?.textContent || "N/A";
const temperatura = document.getElementById("temperatura")?.textContent || "N/A";
const riego = document.getElementById("estado-riego")?.textContent || "N/A";
const plantas = Array.from(document.querySelectorAll("#crop-list li")).map(li => {
const nombre = li.querySelector("h4")?.textContent || "Planta";
const nivel = li.querySelector("div[style]")?.style.width || "0%";
return `${nombre} (${nivel} crecimiento)`;
});
const resumen = `
🔍 Estado actual del sistema EcoGrow:
💧 Agua: ${agua}
🌫️ Humedad: ${humedad}
🌡️ Temperatura: ${temperatura}
🚿 Riego: ${riego}
🌱 Plantas activas:
- ${plantas.join("\n- ")}
✔️ Datos obtenidos correctamente.
`;
alert(resumen);
}
// ====== VALIDACIONES DEL FORMULARIO ======
const form = document.getElementById("registration-form");
const nombre = document.getElementById("nombre");
const email = document.getElementById("email");
const password = document.getElementById("password");
const confirmPassword = document.getElementById("confirm-password");
const edad = document.getElementById("edad");
const submitBtn = document.getElementById("submit-btn");
// Expresión regular para validar email
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// Expresión regular para validar contraseña (8+ caracteres, 1 número, 1 carácter especial)
const passwordRegex = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,}$/;
function validateNombre() {
if (nombre.value === "") return true;
const isValid = nombre.value.length >= 3;
toggleError("error-nombre", !isValid);
toggleFieldStyle(nombre, isValid);
return isValid;
}
function validateEmail() {
if (email.value === "") return true;
const isValid = emailRegex.test(email.value);
toggleError("error-email", !isValid);
toggleFieldStyle(email, isValid);
return isValid;
}
function validatePassword() {
if (password.value === "") return true;
const isValid = passwordRegex.test(password.value);
toggleError("error-password", !isValid);
toggleFieldStyle(password, isValid);
return isValid;
}
function validateConfirmPassword() {
if (confirmPassword.value === "") return true;
const isValid = password.value === confirmPassword.value && confirmPassword.value !== "";
toggleError("error-confirm-password", !isValid);
toggleFieldStyle(confirmPassword, isValid);
return isValid;
}
function validateEdad() {
if (edad.value === "") return true;
const isValid = edad.value >= 18;
toggleError("error-edad", !isValid);
toggleFieldStyle(edad, isValid);
return isValid;
}
function toggleError(errorId, show) {
const errorElement = document.getElementById(errorId);
if (show) {
errorElement.classList.remove("hidden");
} else {
errorElement.classList.add("hidden");
}
}
function toggleFieldStyle(field, isValid) {
if (field.value === "") {
field.classList.remove("valid", "invalid");
} else if (isValid) {
field.classList.remove("invalid");
field.classList.add("valid");
} else {
field.classList.remove("valid");
field.classList.add("invalid");
}
}
function checkFormValidity() {
const allFilled = nombre.value !== "" && email.value !== "" && password.value !== "" && confirmPassword.value !== "" && edad.value !== "";
const allValid =
allFilled &&
validateNombre() &&
validateEmail() &&
validatePassword() &&
validateConfirmPassword() &&
validateEdad();
submitBtn.disabled = !allValid;
return allValid;
}
// Event listeners para validación en tiempo real
nombre.addEventListener("input", checkFormValidity);
email.addEventListener("input", checkFormValidity);
password.addEventListener("input", checkFormValidity);
confirmPassword.addEventListener("input", checkFormValidity);
edad.addEventListener("input", checkFormValidity);
// Manejar envío del formulario
form.addEventListener("submit", (e) => {
e.preventDefault();
if (checkFormValidity()) {
const mensaje = `
✅ ¡Registro exitoso!
Datos ingresados:
- Nombre: ${nombre.value}
- Email: ${email.value}
- Edad: ${edad.value} años
Gracias por registrarte en EcoGrow.
`;
alert(mensaje);
form.reset();
submitBtn.disabled = true;
}
});
// Reiniciar formulario
form.addEventListener("reset", () => {
setTimeout(() => {
submitBtn.disabled = true;
document.querySelectorAll("#registration-form input").forEach(input => {
input.classList.remove("valid", "invalid");
});
document.querySelectorAll("#registration-form p[id^='error-']").forEach(error => {
error.classList.add("hidden");
});
}, 0);
});