fix: возвращена рабочая версия addEquipment

This commit is contained in:
Nurs 2026-09-16 12:07:10 +00:00
parent 4917f34338
commit cccf382818

View File

@ -367,19 +367,19 @@ function updateCounts() {
} }
} }
async function addEquipment(event) { function addEquipment(event) {
event.preventDefault(); event.preventDefault();
const form = event.target; const form = event.target;
const formData = new FormData(form); const formData = new FormData(form);
const lastCheck = formData.get('lastCheck'); // Если режим редактирования - обрабатываем отдельно
const period = parseInt(formData.get('period'));
const nextCheck = calculateNextDateValue(lastCheck, period);
if (editingId) { if (editingId) {
// Edit existing item
const index = equipment.findIndex(e => e.id === editingId); const index = equipment.findIndex(e => e.id === editingId);
if (index !== -1) { if (index !== -1) {
const lastCheck = formData.get('lastCheck');
const period = parseInt(formData.get('period'));
const nextCheck = calculateNextDateValue(lastCheck, period);
equipment[index] = { equipment[index] = {
...equipment[index], ...equipment[index],
name: formData.get('name'), name: formData.get('name'),
@ -392,45 +392,39 @@ async function addEquipment(event) {
nextCheck: nextCheck, nextCheck: nextCheck,
note: formData.get('note') || '' note: formData.get('note') || ''
}; };
await saveEquipment(); saveEquipment();
updateAll(); updateAll();
resetForm(); resetForm();
switchTab('all'); switchTab('all');
} }
editingId = null; editingId = null;
} else { return;
// Add new item - add to local array first
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);
updateAll();
// Then save to server
try {
await fetch('api/items', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(equipment)
});
} catch (e) {
console.error('Save error:', e);
}
resetForm();
switchTab('all');
} }
// Добавление нового оборудования (как работало раньше)
const lastCheck = formData.get('lastCheck');
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');
} }
function calculateNextDateValue(lastCheck, periodMonths) { function calculateNextDateValue(lastCheck, periodMonths) {
@ -706,9 +700,9 @@ function escapeHtml(text) {
.replace(/"/g, '"'); .replace(/"/g, '"');
} }
async async function saveEquipment() { async function saveEquipment() {
try { try {
await fetch('api/items', { fetch('api/items', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(equipment) body: JSON.stringify(equipment)