diff --git a/script.js b/script.js index 4ae2956..18c9f3a 100644 --- a/script.js +++ b/script.js @@ -376,27 +376,50 @@ function addEquipment(event) { const period = parseInt(formData.get('period')); const nextCheck = calculateNextDateValue(lastCheck, period); - const newItem = { - id: Date.now(), - name: formData.get('name'), - category: formData.get('category'), - inventoryNumber: formData.get('inventoryNumber'), - location: formData.get('location'), - responsible: formData.get('responsible'), - lastCheck: lastCheck, - period: period, - nextCheck: nextCheck, - note: formData.get('note') || '', - status: 'ok' - }; - - equipment.push(newItem); - saveEquipment(); - resetForm(); - updateAll(); - switchTab('all'); - - alert('Оборудование успешно добавлено!'); + if (editingId) { + // Edit existing item + const index = equipment.findIndex(e => e.id === editingId); + if (index !== -1) { + equipment[index] = { + ...equipment[index], + name: formData.get('name'), + category: formData.get('category'), + inventoryNumber: formData.get('inventoryNumber'), + location: formData.get('location'), + responsible: formData.get('responsible'), + lastCheck: lastCheck, + period: period, + nextCheck: nextCheck, + note: formData.get('note') || '' + }; + saveEquipment(); + updateAll(); + resetForm(); + switchTab('all'); + } + editingId = null; + } else { + // Add new item + const newItem = { + id: Date.now(), + name: formData.get('name'), + category: formData.get('category'), + inventoryNumber: formData.get('inventoryNumber'), + location: formData.get('location'), + responsible: formData.get('responsible'), + lastCheck: lastCheck, + period: period, + nextCheck: nextCheck, + note: formData.get('note') || '', + status: 'ok' + }; + + equipment.push(newItem); + saveEquipment(); + resetForm(); + updateAll(); + switchTab('all'); + } } function calculateNextDateValue(lastCheck, periodMonths) { @@ -419,6 +442,11 @@ function calculateNextDate() { function resetForm() { document.getElementById('add-form').reset(); document.getElementById('next-check').value = ''; + editingId = null; + + // Reset button text + const submitBtn = document.getElementById('add-form').querySelector('button[type="submit"]'); + submitBtn.innerHTML = 'Сохранить'; } function openDrawer(id) { @@ -492,10 +520,16 @@ function openDrawer(id) { `; actions.innerHTML = ` - +