Changed around line 1
+ let inventory = [];
+ let currentEditIndex = null;
+
+ const inventoryList = document.getElementById('inventoryList');
+ const itemModal = document.getElementById('itemModal');
+ const itemForm = document.getElementById('itemForm');
+ const addItemBtn = document.getElementById('addItem');
+ const cancelBtn = document.getElementById('cancelBtn');
+ const searchInput = document.getElementById('search');
+
+ function renderInventory() {
+ inventoryList.innerHTML = '';
+ inventory.forEach((item, index) => {
+ const itemRow = document.createElement('div');
+ itemRow.className = 'item-row';
+ itemRow.innerHTML = `
+
$${item.cost.toFixed(2)}
+
$${item.price.toFixed(2)}
+ `;
+ inventoryList.appendChild(itemRow);
+ });
+ }
+
+ function addItem(item) {
+ inventory.push(item);
+ renderInventory();
+ }
+
+ function editItem(index) {
+ currentEditIndex = index;
+ const item = inventory[index];
+ itemForm.name.value = item.name;
+ itemForm.factory.value = item.factory;
+ itemForm.cost.value = item.cost;
+ itemForm.price.value = item.price;
+ itemForm.stock.value = item.stock;
+ itemModal.showModal();
+ }
+
+ function deleteItem(index) {
+ inventory.splice(index, 1);
+ renderInventory();
+ }
+
+ itemForm.addEventListener('submit', (e) => {
+ e.preventDefault();
+ const item = {
+ name: itemForm.name.value,
+ factory: itemForm.factory.value,
+ cost: parseFloat(itemForm.cost.value),
+ price: parseFloat(itemForm.price.value),
+ stock: parseInt(itemForm.stock.value)
+ };
+
+ if (currentEditIndex !== null) {
+ inventory[currentEditIndex] = item;
+ currentEditIndex = null;
+ } else {
+ addItem(item);
+ }
+
+ itemModal.close();
+ renderInventory();
+ itemForm.reset();
+ });
+
+ addItemBtn.addEventListener('click', () => {
+ currentEditIndex = null;
+ itemModal.showModal();
+ });
+
+ cancelBtn.addEventListener('click', () => {
+ itemModal.close();
+ itemForm.reset();
+ });
+
+ searchInput.addEventListener('input', () => {
+ const searchTerm = searchInput.value.toLowerCase();
+ const filtered = inventory.filter(item =>
+ item.name.toLowerCase().includes(searchTerm) ||
+ item.factory.toLowerCase().includes(searchTerm)
+ );
+ inventoryList.innerHTML = '';
+ filtered.forEach((item, index) => {
+ const itemRow = document.createElement('div');
+ itemRow.className = 'item-row';
+ itemRow.innerHTML = `
+
$${item.cost.toFixed(2)}
+
$${item.price.toFixed(2)}
+ `;
+ inventoryList.appendChild(itemRow);
+ });
+ });
+
+ // Initial render
+ renderInventory();