feat: редактирование и удаление оборудования
This commit is contained in:
parent
602ff94919
commit
bd9aedbdfe
103
script.js
103
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 = `
|
||||
<button class="kt-ai-btn" data-variant="primary" onclick="editEquipment(${item.id})">
|
||||
<svg class="kt-icon kt-icon-sm" aria-hidden="true"><use href="#pencil"></use></svg>
|
||||
Редактировать
|
||||
</button>
|
||||
<div style="display: flex; gap: var(--kt-ai-space-3);">
|
||||
<button class="kt-ai-btn" data-variant="primary" onclick="editEquipment(${item.id})">
|
||||
<svg class="kt-icon kt-icon-sm" aria-hidden="true"><use href="#pencil"></use></svg>
|
||||
Редактировать
|
||||
</button>
|
||||
<button class="kt-ai-btn" data-variant="danger" onclick="deleteEquipment(${item.id})">
|
||||
<svg class="kt-icon kt-icon-sm" aria-hidden="true"><use href="#trash"></use></svg>
|
||||
Удалить
|
||||
</button>
|
||||
</div>
|
||||
<button class="kt-ai-btn" data-variant="ghost" onclick="closeDrawer()">Закрыть</button>
|
||||
`;
|
||||
|
||||
@ -506,10 +540,13 @@ function closeDrawer() {
|
||||
document.getElementById('equipment-drawer').hidden = true;
|
||||
}
|
||||
|
||||
let editingId = null;
|
||||
|
||||
function editEquipment(id) {
|
||||
const item = equipment.find(e => e.id === id);
|
||||
if (!item) return;
|
||||
|
||||
editingId = id;
|
||||
closeDrawer();
|
||||
switchTab('add');
|
||||
|
||||
@ -524,8 +561,9 @@ function editEquipment(id) {
|
||||
document.getElementById('next-check').value = item.nextCheck;
|
||||
form.note.value = item.note || '';
|
||||
|
||||
// Remove old item
|
||||
equipment = equipment.filter(e => e.id !== id);
|
||||
// Change button text
|
||||
const submitBtn = form.querySelector('button[type="submit"]');
|
||||
submitBtn.innerHTML = '<svg class="kt-icon kt-icon-sm" aria-hidden="true"><use href="#check"></use></svg> Сохранить изменения';
|
||||
}
|
||||
|
||||
function exportToExcel() {
|
||||
@ -657,7 +695,7 @@ function escapeHtml(text) {
|
||||
.replace(/"/g, '"');
|
||||
}
|
||||
|
||||
async function saveEquipment() {
|
||||
async async function saveEquipment() {
|
||||
try {
|
||||
await fetch('api/items', {
|
||||
method: 'POST',
|
||||
@ -669,6 +707,15 @@ async function saveEquipment() {
|
||||
}
|
||||
}
|
||||
|
||||
function deleteEquipment(id) {
|
||||
if (!confirm('Вы уверены, что хотите удалить это оборудование?')) return;
|
||||
|
||||
equipment = equipment.filter(e => e.id !== id);
|
||||
saveEquipment();
|
||||
closeDrawer();
|
||||
updateAll();
|
||||
}
|
||||
|
||||
// Navigation
|
||||
document.querySelectorAll('[data-tab]').forEach(link => {
|
||||
link.addEventListener('click', (e) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user