-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsinit.js
More file actions
140 lines (119 loc) · 4.84 KB
/
Copy pathjsinit.js
File metadata and controls
140 lines (119 loc) · 4.84 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
document.addEventListener("DOMContentLoaded", async () => {
const supabaseUrl = "https://ekqcllkizcpdfnbnmean.supabase.co";
const supabaseKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImVrcWNsbGtpemNwZGZuYm5tZWFuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk1Njc5ODksImV4cCI6MjA2NTE0Mzk4OX0.4f3oyXY6cg2436L7NZn3BO3QqMFo2ehdEPdwAfQCMeQ";
const supabase = window.supabase.createClient(supabaseUrl, supabaseKey);
const tabla = document.getElementById("tablaInventario");
function ofuscarID(id) {
return id.slice(0, 4) + "..." + id.slice(-4);
}
async function cargarInventario() {
const { data, error } = await supabase.from("inventario").select("*");
if (error) {
console.error("Error al cargar inventario:", error.message);
return;
}
tabla.innerHTML = "";
data.forEach(item => {
const fila = document.createElement("tr");
fila.innerHTML = `
<td>${ofuscarID(item.id)}</td>
<td>${item.producto}</td>
<td>${item.stock}</td>
<td>${item.categoria}</td>
<td>S/. ${item.precio_unit?.toFixed(2) || "0.00"}</td>
<td>S/. ${item.valor_total?.toFixed(2) || "0.00"}</td>
<td>
<button onclick="editarProducto('${item.id}')">✏️</button>
<button onclick="eliminarProducto('${item.id}')">🗑️</button>
</td>
`;
tabla.appendChild(fila);
});
}
document.getElementById("formAgregar").addEventListener("submit", async function (e) {
e.preventDefault();
const producto = document.getElementById("producto").value.trim();
const stock = parseInt(document.getElementById("stock").value);
const categoria = document.getElementById("categoria").value.trim();
const precio_unit = parseFloat(document.getElementById("precio_unit").value);
const valor_total = stock * precio_unit;
if (producto && categoria && stock > 0 && precio_unit >= 0) {
const { error } = await supabase.from("inventario").insert([
{
producto,
stock,
categoria,
precio_unit,
valor_total
}
]);
if (error) {
alert("Error al agregar: " + error.message);
} else {
cargarInventario();
this.reset();
}
}
});
document.getElementById("formEliminar").addEventListener("submit", async function (e) {
e.preventDefault();
const producto = document.getElementById("nombreEliminar").value.trim();
if (producto) {
const { error } = await supabase.from("inventario").delete().eq("producto", producto);
if (error) {
alert("Error al eliminar: " + error.message);
} else {
cargarInventario();
this.reset();
}
}
});
window.eliminarProducto = async function(id) {
if (confirm("¿Seguro que deseas eliminar este producto?")) {
const { error } = await supabase.from("inventario").delete().eq("id", id);
if (error) {
alert("Error al eliminar: " + error.message);
} else {
cargarInventario();
}
}
};
window.editarProducto = async function(id) {
const { data, error } = await supabase.from("inventario").select("*").eq("id", id).single();
if (error) {
alert("Error al obtener el producto: " + error.message);
return;
}
// Rellenar el formulario con los datos
document.getElementById("producto").value = data.producto;
document.getElementById("stock").value = data.stock;
document.getElementById("categoria").value = data.categoria;
document.getElementById("precio_unit").value = data.precio_unit;
// Cambiar botón de agregar a actualizar temporalmente
const boton = document.querySelector("#formAgregar button");
boton.textContent = "Actualizar";
// Evitar múltiples listeners
const form = document.getElementById("formAgregar");
const nuevoForm = form.cloneNode(true);
form.parentNode.replaceChild(nuevoForm, form);
nuevoForm.addEventListener("submit", async function(e) {
e.preventDefault();
const producto = document.getElementById("producto").value.trim();
const stock = parseInt(document.getElementById("stock").value);
const categoria = document.getElementById("categoria").value.trim();
const precio_unit = parseFloat(document.getElementById("precio_unit").value);
const valor_total = stock * precio_unit;
const { error } = await supabase.from("inventario").update({
producto, stock, categoria, precio_unit, valor_total
}).eq("id", id);
if (error) {
alert("Error al actualizar: " + error.message);
} else {
cargarInventario();
nuevoForm.reset();
boton.textContent = "Agregar";
}
});
};
cargarInventario();
});