forked from rendznicoy/Simple-Inventory-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
295 lines (233 loc) · 10.5 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', function () {
var modal = document.getElementById('stock-modal');
var editModal = document.getElementById('edit-stock-modal');
var newStockCloseBtn = document.querySelector("#stock-modal .close");
var editingRowIndex;
var productCountInput = document.getElementById('product-count');
productCountInput.addEventListener('input', function () {
if (parseInt(productCountInput.value) < 0) {
productCountInput.value = 0;
}
});
var editProductCountInput = document.getElementById('edit-product-count');
editProductCountInput.addEventListener('input', function () {
if (parseInt(editProductCountInput.value) < 0) {
editProductCountInput.value = 0;
}
});
newStockCloseBtn.addEventListener('click', function () {
clearInputValues();
modal.style.display = 'none';
});
var editStockCloseBtn = document.querySelector("#edit-stock-modal .close");
editStockCloseBtn.addEventListener('click', function () {
closeEditModal();
});
function disableProductIdInput(editForm) {
var productIdInput = editForm ? document.getElementById('product-id-edit') : document.getElementById('product-id');
productIdInput.readOnly = true;
}
function createEditButton() {
var editButton = document.createElement('button');
editButton.className = 'edit-btn';
editButton.innerHTML = '<img src="/Main/Images/edit.png" alt="Edit">';
editButton.onclick = function () {
editRow(this);
};
return editButton;
}
function disableProductIdInput(disable) {
document.getElementById('product-id-edit').readOnly = disable;
}
function editRow(button) {
var row = button.closest('tr');
editingRowIndex = row.rowIndex;
var productId = row.cells[1].textContent;
openEditModal();
disableProductIdInput();
document.getElementById('product-id-edit').value = productId;
document.getElementById('product-name-edit').value = row.cells[2].textContent;
document.getElementById('product-category-edit').value = row.cells[3].textContent;
document.getElementById('product-brand-edit').value = row.cells[4].textContent;
document.getElementById('product-notes-edit').value = row.cells[5].textContent;
document.getElementById('product-count-edit').value = row.cells[6].textContent;
}
function openStockModal() {
disableProductIdInput(false);
modal.style.display = 'block';
}
var newStockBtn = document.querySelector(".add-btn .add");
newStockBtn.addEventListener('click', function () {
openStockModal();
});
function openEditModal() {
editModal.style.display = 'block';
}
document.getElementById('editSubmitButton').addEventListener('click', function (event) {
event.preventDefault();
var productName = document.getElementById('edit-product-name').value;
var category = document.getElementById('edit-product-category').value;
var brand = document.getElementById('edit-product-brand').value;
var notes = document.getElementById('edit-product-notes').value;
var count = document.getElementById('edit-product-count').value;
if (!productName || !category || !brand || !count) {
alert('Please fill in all required fields.');
return;
}
var editedRow = document.querySelector('.table').rows[editingRowIndex];
if (editedRow) {
var originalCount = parseInt(editedRow.cells[5].textContent);
editedRow.cells[1].textContent = productName;
editedRow.cells[2].textContent = category;
editedRow.cells[3].textContent = brand;
editedRow.cells[4].textContent = notes;
editedRow.cells[5].textContent = count;
if (originalCount > 0 && count == 0) {
editedRow.cells[6].textContent = 'Out of Stock';
} else {
editedRow.cells[6].textContent = 'In Stock';
}
document.getElementById('edit-stock-modal').style.display = 'none';
closeEditModal();
clearEditInputValues();
}
});
function closeEditModal() {
clearInputValues('edit');
editModal.style.display = 'none';
}
var closeBtn = document.querySelector("#edit-stock-modal .close");
closeBtn.addEventListener('click', function () {
closeEditModal();
});
window.addEventListener('click', function (event) {
if (event.target === editModal) {
closeEditModal();
}
});
var editForm = document.getElementById('editStockForm');
editForm.addEventListener('submit', function (event) {
event.preventDefault();
var editedProductName = document.getElementById('edit-product-name').value;
var editedProductCategory = document.getElementById('edit-product-category').value;
var editedProductBrand = document.getElementById('edit-product-brand').value;
var editedProductNotes = document.getElementById('edit-product-notes').value;
var editedProductCount = document.getElementById('edit-product-count').value;
var table = document.querySelector('.table');
var rows = table.querySelectorAll('tr');
var productId = document.getElementById('edit-product-id').value;
var editedRow;
for (var i = 1; i < rows.length; i++) {
var currentProductId = rows[i].cells[0].textContent;
if (currentProductId === productId) {
editedRow = rows[i];
break;
}
}
if (editedRow) {
editedRow.cells[2].innerHTML = editedProductCategory;
editedRow.cells[3].innerHTML = editedProductBrand;
editedRow.cells[4].innerHTML = editedProductNotes;
editedRow.cells[5].innerHTML = editedProductCount;
}
document.getElementById('edit-stock-modal').style.display = 'none';
});
window.openStockModal = function () {
modal.style.display = 'block';
};
function clearInputValues() {
document.getElementById('product-id').value = '';
document.getElementById('product-name').value = '';
document.getElementById('product-category').value = '';
document.getElementById('product-brand').value = '';
document.getElementById('product-notes').value = '';
document.getElementById('product-count').value = '';
}
function clearEditInputValues() {
document.getElementById('edit-product-name').value = '';
document.getElementById('edit-product-category').value = '';
document.getElementById('edit-product-brand').value = '';
document.getElementById('edit-product-notes').value = '';
document.getElementById('edit-product-count').value = '';
}
var newStockCloseBtn = document.querySelector("#stock-modal .close");
newStockCloseBtn.addEventListener('click', function () {
clearInputValues();
modal.style.display = 'none';
});
window.addEventListener('click', function (event) {
if (event.target === modal) {
modal.style.display = 'none';
}
});
var form = document.getElementById('newStockForm');
form.addEventListener('submit', function (event) {
event.preventDefault();
var productId = document.getElementById('product-id').value;
var productName = document.getElementById('product-name').value;
var category = document.getElementById('product-category').value;
var brand = document.getElementById('product-brand').value;
var notes = document.getElementById('product-notes').value;
var count = document.getElementById('product-count').value;
var table = document.querySelector('.table');
var rows = table.querySelectorAll('tr');
var existingRow;
for (var i = 1; i < rows.length; i++) {
var currentProductId = rows[i].cells[0].textContent;
if (currentProductId === productId) {
existingRow = rows[i];
break;
}
}
if (existingRow) {
existingRow.cells[1].innerHTML = productName;
existingRow.cells[2].innerHTML = category;
existingRow.cells[3].innerHTML = brand;
existingRow.cells[4].innerHTML = notes;
existingRow.cells[5].innerHTML = count;
existingRow.cells[6].innerHTML = (count == 0) ? 'Out of Stock' : 'In Stock';
} else {
var newRow = table.insertRow(-1);
var productIdCell = newRow.insertCell(0);
var productNameCell = newRow.insertCell(1);
var categoryCell = newRow.insertCell(2);
var brandCell = newRow.insertCell(3);
var notesCell = newRow.insertCell(4);
var countCell = newRow.insertCell(5);
var statusCell = newRow.insertCell(6);
var checkBoxCell = newRow.insertCell(7);
productIdCell.innerHTML = productId;
productNameCell.innerHTML = productName;
categoryCell.innerHTML = category;
brandCell.innerHTML = brand;
notesCell.innerHTML = notes;
countCell.innerHTML = count;
statusCell.innerHTML = (count == 0) ? 'Out of Stock' : 'In Stock';
checkBoxCell.appendChild(createEditButton());
}
clearInputValues();
modal.style.display = 'none';
});
});
/*jQuery.expr[':'].contains = function (a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
var firebaseConfig = {
apiKey: "AIzaSyDwfuUpnlPk4ccotRyAkQKU58KGOCbdGrs",
authDomain: "simple-inventory-manager-7180f.firebaseapp.com",
projectId: "simple-inventory-manager-7180f",
storageBucket: "simple-inventory-manager-7180f.appspot.com",
messagingSenderId: "660012179034",
appId: "1:660012179034:web:d581aeb13178459126c263",
measurementId: "G-1PPTLXZWBM"
};
firebase.initializeApp(firebaseConfig);
var database = firebase.database();
function writeToFirebase(product) {
const productsRef = database.ref('products');
productsRef.child(product.productId).set(product)
.then(() => console.log('Data written to Firebase'))
.catch(error => console.error('Error writing data to Firebase:', error));
}
writeToFirebase(product);*/