document.addEventListener('DOMContentLoaded', function() { const mainContent = document.getElementById('main-content'); const appName = document.querySelector('.navbar-brand').textContent; // Global function for editing item from tree window.editItem = function(id) { const mockBtn = { getAttribute: (attr) => attr === 'data-id' ? id : null }; handleEditItem.call(mockBtn); }; // Function to toggle tree nodes window.toggleNode = function(element) { const li = element.closest('li'); const childUl = li.querySelector(':scope > ul'); if (childUl) { const isHidden = childUl.style.display === 'none'; childUl.style.display = isHidden ? 'block' : 'none'; const icon = element.querySelector('i'); if (icon) { if (isHidden) { icon.className = 'bi bi-folder-fill text-warning'; } else { icon.className = 'bi bi-folder text-warning'; } } } }; function setPageTitle(title) { document.title = `${appName} - ${title}`; } function showLoading() { mainContent.innerHTML = '
Huidige afbeelding:
Geen afbeelding
'; } const categorySelect = document.getElementById('edit_item_category_id'); categorySelect.innerHTML = ''; fetch('/api/categories/list') .then(resp => resp.json()) .then(categories => { categories.forEach(cat => { const option = document.createElement('option'); option.value = cat.id; option.textContent = cat.path; categorySelect.appendChild(option); }); categorySelect.value = data.category_id || ''; const modal = new bootstrap.Modal(document.getElementById('editItemModal')); const saveEditItemBtn = document.getElementById('saveEditItemBtn'); if (saveEditItemBtn) { saveEditItemBtn.addEventListener('click', handleSaveEditItem); } modal.show(); }); } else { alert('Part not found'); } }) .catch(error => { console.error('Error:', error); alert('Error fetching part'); }); } function handleDeleteItem() { const id = this.getAttribute('data-id'); if (confirm(window.translations.deletePartConfirm)) { fetch('/api/items/' + id, { method: 'DELETE' }) .then(response => response.json()) .then(data => { if (data.success) { this.closest('li').remove(); reloadTree(); // Reload tree after deletion } else { alert(data.error || 'Error deleting part'); } }) .catch(error => { console.error('Error:', error); alert('Error deleting part'); }); } } function handleAddCategory() { const formData = new FormData(this); fetch('/api/categories', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { fetchContent('/categories', false); reloadTree(); // Reload tree after adding } else { alert(data.error || 'Error adding category'); } }) .catch(error => { console.error('Error:', error); alert('Error adding category'); }); } function handleSaveEditItem() { const id = document.getElementById('edit_item_id').value; const name = document.getElementById('edit_item_name').value.trim(); const description = document.getElementById('edit_item_description').value; const categoryId = document.getElementById('edit_item_category_id').value; const location = document.getElementById('edit_location').value.trim(); const imageFile = document.getElementById('edit_image').files[0]; if (!name) { alert('Part name is required'); return; } const formData = new FormData(); formData.append('item_name', name); formData.append('item_description', description); formData.append('category_id', categoryId || ''); formData.append('location', location); if (imageFile) { formData.append('image', imageFile); } fetch('/api/items/' + id, { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { // Hide modal const modalElement = document.getElementById('editItemModal'); if (modalElement) { modalElement.classList.remove('show', 'showing'); modalElement.style.display = 'none'; modalElement.setAttribute('aria-hidden', 'true'); document.body.classList.remove('modal-open'); const backdrops = document.querySelectorAll(' .modal-backdrop'); backdrops.forEach(backdrop => backdrop.remove()); } fetchContent('/', false); reloadTree(); // Reload tree after updating } else { alert(data.error || 'Error updating part'); } }) .catch(error => { console.error('Update error:', error); alert('Error updating part: ' + error.message); }); } function handleSaveEditCategory() { const id = document.getElementById('edit_category_id').value; const name = document.getElementById('edit_category_name').value.trim(); const parentId = document.getElementById('edit_parent_category_id').value; if (!name) { alert('Category name is required'); return; } const data = { category_name: name, parent_category_id: parentId || null }; fetch('/api/categories/' + id, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) .then(response => response.json()) .then(data => { if (data.success) { // Hide modal const modalElement = document.getElementById('editCategoryModal'); if (modalElement) { modalElement.classList.remove('show', 'showing'); modalElement.style.display = 'none'; modalElement.setAttribute('aria-hidden', 'true'); document.body.classList.remove('modal-open'); const backdrops = document.querySelectorAll(' .modal-backdrop'); backdrops.forEach(backdrop => backdrop.remove()); } fetchContent('/categories', false); reloadTree(); // Reload tree after updating } else { alert(data.error || 'Error updating category'); } }) .catch(error => { console.error('Update error:', error); alert('Error updating category: ' + error.message); }); } function handleEditCategory() { const id = this.getAttribute('data-id'); fetch('/api/categories/' + id) .then(response => { if (!response.ok) { throw new Error('HTTP ' + response.status + ': ' + response.statusText); } return response.json(); }) .then(data => { if (data && !data.error) { document.getElementById('edit_category_id').value = data.id; document.getElementById('edit_category_name').value = data.name; const parentSelect = document.getElementById('edit_parent_category_id'); parentSelect.innerHTML = ''; fetch('/api/categories/list') .then(resp => resp.json()) .then(categories => { categories.forEach(cat => { if (cat.id != data.id) { const option = document.createElement('option'); option.value = cat.id; option.textContent = cat.path; parentSelect.appendChild(option); } }); parentSelect.value = data.parent_id || ''; const modal = new bootstrap.Modal(document.getElementById('editCategoryModal')); const saveEditCategoryBtn = document.getElementById('saveEditCategoryBtn'); if (saveEditCategoryBtn) { saveEditCategoryBtn.addEventListener('click', handleSaveEditCategory); } modal.show(); }); } else { alert(data.error || 'Category not found'); } }) .catch(error => { console.error('Error:', error); alert('Error fetching category: ' + error.message); }); } function handleDeleteCategory() { const id = this.getAttribute('data-id'); if (confirm(window.translations.deleteCategoryConfirm)) { fetch('/api/categories/' + id, { method: 'DELETE' }) .then(response => response.json()) .then(data => { if (data.success) { this.closest('li').remove(); reloadTree(); // Reload tree after deletion } else { alert(data.error || 'Error deleting category'); } }) .catch(error => { console.error('Error:', error); alert('Error deleting category'); }); } } });