-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.js
More file actions
336 lines (314 loc) · 8.75 KB
/
inventory.js
File metadata and controls
336 lines (314 loc) · 8.75 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
let inventory = [
{
id: 1,
name: "Chicken Shawarma",
price: 149,
quantity: 25,
image: "images/Chicken Shawarma.webp",
url: "/menu/chicken-shawarma",
},
{
id: 2,
name: "Chicken Sandwich",
price: 149,
quantity: 18,
image: "images/Chicken Sandwich.webp",
url: "/menu/chicken-sandwich",
},
{
id: 3,
name: "Chicken Puff",
price: 149,
quantity: 12,
image: "images/Chicken Puff.webp",
url: "/menu/chicken-puff",
},
{
id: 4,
name: "Chicken Patty Burger",
price: 149,
quantity: 8,
image: "images/Chicken Patty Burger.webp",
url: "/menu/chicken-patty-burger",
},
{
id: 5,
name: "Veg Burger",
price: 149,
quantity: 15,
image: "images/Veg Burger.webp",
url: "/menu/veg-burger",
},
{
id: 6,
name: "Chicken 65 Roll",
price: 149,
quantity: 20,
image: "images/Chicken 65 Roll.webp",
url: "/menu/chicken-65-roll",
},
{
id: 7,
name: "Chicken Biryani",
price: 149,
quantity: 10,
image: "images/Chicken Biryani.webp",
url: "/menu/chicken-biryani",
},
{
id: 8,
name: "Chicken Fried Rice",
price: 149,
quantity: 14,
image: "images/Chicken Fried Rice.webp",
url: "/menu/chicken-fried-rice",
},
{
id: 9,
name: "Chicken Schezwan Fried Rice",
price: 149,
quantity: 16,
image: "images/Chicken Schezwan Fried Rice.webp",
url: "/menu/chicken-schezwan-fried-rice",
},
{
id: 10,
name: "Chicken Noodles",
price: 149,
quantity: 22,
image: "images/Chicken Noodles.webp",
url: "/menu/chicken-noodles",
},
{
id: 11,
name: "Chicken Schezwan Noodles",
price: 149,
quantity: 18,
image: "images/Chicken Schezwan Noodles.webp",
url: "/menu/chicken-schezwan-noodles",
},
{
id: 12,
name: "White Sauce Pasta",
price: 149,
quantity: 12,
image: "images/White Sauce Pasta.webp",
url: "/menu/white-sauce-pasta",
},
{
id: 13,
name: "Choco Lava Cake",
price: 149,
quantity: 8,
image: "images/Choco Lava Cake.webp",
url: "/menu/choco-lava-cake",
},
{
id: 14,
name: "Chocolate Donut",
price: 149,
quantity: 25,
image: "images/Chocolate Donut.webp",
url: "/menu/chocolate-donut",
},
{
id: 15,
name: "Campa",
price: 10,
quantity: 50,
image: "images/Campa.webp",
url: "/menu/campa",
},
{
id: 16,
name: "Maaza",
price: 10,
quantity: 45,
image: "images/Maaza.webp",
url: "/menu/maaza",
},
{
id: 17,
name: "Bisleri Water Bottle",
price: 10,
quantity: 100,
image: "images/Bisleri Water Bottle.webp",
url: "/menu/bisleri-water-bottle",
},
];
let editingItemId = null;
function renderInventory() {
const tbody = document.getElementById("inventoryTableBody");
const searchTerm = document.getElementById("searchInput").value.toLowerCase();
const filteredInventory = inventory.filter((item) =>
item.name.toLowerCase().includes(searchTerm)
);
tbody.innerHTML = filteredInventory
.map(
(item) => `
<tr>
<td>
<img src="${item.image}" alt="${
item.name
}" class="item-image"
onerror="this.src='https://images.unsplash.com/photo-1546833999-b9f581a1996d?w=300&h=200&fit=crop'">
</td>
<td class="item-name">${item.name}</td>
<td class="item-price">₹${item.price}</td>
<td>
<div class="quantity-controls">
<button class="quantity-btn" onclick="updateQuantity(${
item.id
}, -1)">-</button>
<span class="quantity-display">${
item.quantity
}</span>
<button class="quantity-btn" onclick="updateQuantity(${
item.id
}, 1)">+</button>
</div>
</td>
<td>
<span class="stock-badge ${getStockClass(
item.quantity
)}">
${getStockStatus(item.quantity)}
</span>
</td>
<td>
<a href="${
item.url
}" target="_blank" style="color: #667eea; text-decoration: none;">
${item.url}
</a>
</td>
<td>
<button class="btn btn-edit" onclick="editItem(${
item.id
})">
✏️ Edit
</button>
<button class="btn btn-danger" onclick="deleteItem(${
item.id
})" style="margin-left: 10px;">
🗑️ Delete
</button>
</td>
</tr>
`
)
.join("");
updateStats();
}
function getStockStatus(quantity) {
if (quantity === 0) return "Out of Stock";
if (quantity <= 5) return "Low Stock";
return "In Stock";
}
function getStockClass(quantity) {
if (quantity === 0) return "stock-out";
if (quantity <= 5) return "stock-low";
return "stock-in";
}
function updateStats() {
const totalItems = inventory.length;
const lowStock = inventory.filter(
(item) => item.quantity > 0 && item.quantity <= 5
).length;
const outOfStock = inventory.filter((item) => item.quantity === 0).length;
const totalValue = inventory.reduce(
(sum, item) => sum + item.price * item.quantity,
0
);
document.getElementById("totalItems").textContent = totalItems;
document.getElementById("lowStock").textContent = lowStock;
document.getElementById("outOfStock").textContent = outOfStock;
document.getElementById("totalValue").textContent = `₹${totalValue}`;
}
function updateQuantity(id, change) {
const item = inventory.find((item) => item.id === id);
if (item) {
item.quantity = Math.max(0, item.quantity + change);
renderInventory();
}
}
function deleteItem(id) {
if (confirm("Are you sure you want to delete this item?")) {
inventory = inventory.filter((item) => item.id !== id);
renderInventory();
}
}
function openAddModal() {
editingItemId = null;
document.getElementById("modalTitle").textContent = "Add New Item";
document.getElementById("itemForm").reset();
document.getElementById("itemModal").style.display = "block";
}
function editItem(id) {
const item = inventory.find((item) => item.id === id);
if (item) {
editingItemId = id;
document.getElementById("modalTitle").textContent = "Edit Item";
document.getElementById("itemName").value = item.name;
document.getElementById("itemPrice").value = item.price;
document.getElementById("itemQuantity").value = item.quantity;
document.getElementById("itemImage").value = item.image;
document.getElementById("itemUrl").value = item.url;
document.getElementById("itemModal").style.display = "block";
}
}
function closeModal() {
document.getElementById("itemModal").style.display = "none";
editingItemId = null;
}
document.getElementById("itemForm").addEventListener("submit", function (e) {
e.preventDefault();
const name = document.getElementById("itemName").value;
const price = parseFloat(document.getElementById("itemPrice").value);
const quantity = parseInt(document.getElementById("itemQuantity").value);
const image =
document.getElementById("itemImage").value || `images/${name}.webp`;
const url =
document.getElementById("itemUrl").value ||
`/menu/${name.toLowerCase().replace(/\s+/g, "-")}`;
if (editingItemId) {
// Edit existing item
const item = inventory.find((item) => item.id === editingItemId);
if (item) {
item.name = name;
item.price = price;
item.quantity = quantity;
item.image = image;
item.url = url;
}
} else {
// Add new item
const newItem = {
id: Date.now(),
name,
price,
quantity,
image,
url,
};
inventory.push(newItem);
}
closeModal();
renderInventory();
});
document
.getElementById("searchInput")
.addEventListener("input", renderInventory);
// Close modal when clicking outside
window.addEventListener("click", function (e) {
const modal = document.getElementById("itemModal");
if (e.target === modal) {
closeModal();
}
});
// Initial render
renderInventory();
// Optional — a function to get latest inventory
export function getInventory() {
return inventory;
}