// Image management functionality let currentPage = 1; let totalPages = 1; // Load images with pagination async function loadImages(page = 1, tags = null) { if (!config.isConfigured()) { showAlert('Please configure your API settings first.', 'warning'); return; } const container = document.getElementById('imagesContainer'); container.innerHTML = '
Loading images...
'; try { const response = await apiClient.getImages(page, 20, tags); currentPage = page; totalPages = Math.ceil(response.total / (response.limit || 20)); // Handle structured response - extract images array from response object const images = response.images || response; displayImages(images); displayPagination(response); } catch (error) { handleApiError(error, 'loading images'); container.innerHTML = '
Failed to load images
'; } } // Display images in grid function displayImages(images) { const container = document.getElementById('imagesContainer'); if (!images || images.length === 0) { container.innerHTML = `

No images found

Upload your first image to get started!

`; return; } const imagesHtml = images.map(image => `
${escapeHtml(image.description || 'Image')}
${escapeHtml(truncateText(image.description || 'Untitled', 50))}

${formatDate(image.upload_date)}

${image.tags && image.tags.length > 0 ? `
${image.tags.map(tag => `${escapeHtml(tag)}`).join('')}
` : ''}
`).join(''); container.innerHTML = `
${imagesHtml}
`; } // Display pagination function displayPagination(response) { const container = document.getElementById('imagesContainer'); if (totalPages <= 1) return; const paginationHtml = ` `; container.insertAdjacentHTML('beforeend', paginationHtml); } // Show upload modal function showUploadModal() { const modalBody = `
Drag & drop an image here

or click to select a file

e.g., nature, landscape, sunset
`; const modalFooter = ` `; const modal = createModal('uploadModal', 'Upload Image', modalBody, modalFooter); // Setup file input and drag & drop const uploadArea = document.getElementById('uploadArea'); const fileInput = document.getElementById('imageFile'); uploadArea.addEventListener('click', () => fileInput.click()); fileInput.addEventListener('change', (e) => { if (e.target.files.length > 0) { handleFileSelection(e.target.files[0]); } }); setupFileDropZone(uploadArea, (files) => { if (files.length > 0) { handleFileSelection(files[0]); } }); modal.show(); } // Handle file selection async function handleFileSelection(file) { try { validateFile(file); const preview = await createImagePreview(file); const previewContainer = document.getElementById('imagePreview'); const previewImg = document.getElementById('previewImg'); previewImg.src = preview; previewContainer.style.display = 'block'; // Store file for upload document.getElementById('uploadForm').selectedFile = file; } catch (error) { showAlert(error.message, 'danger'); } } // Upload image async function uploadImage() { const form = document.getElementById('uploadForm'); const file = form.selectedFile; if (!file) { showAlert('Please select an image file', 'danger'); return; } const description = document.getElementById('imageDescription').value.trim(); const tagsInput = document.getElementById('imageTags').value.trim(); const tags = tagsInput ? tagsInput.split(',').map(tag => tag.trim()).filter(tag => tag) : []; const uploadButton = document.querySelector('#uploadModal .btn-primary'); setLoadingState(uploadButton); try { const formData = new FormData(); formData.append('file', file); formData.append('description', description); if (tags.length > 0) { formData.append('tags', tags.join(',')); } await apiClient.uploadImage(formData); showAlert('Image uploaded successfully!', 'success'); // Close modal and refresh images bootstrap.Modal.getInstance(document.getElementById('uploadModal')).hide(); removeModal('uploadModal'); loadImages(currentPage); } catch (error) { handleApiError(error, 'uploading image'); } finally { setLoadingState(uploadButton, false); } } // View image details async function viewImage(imageId) { try { const image = await apiClient.getImage(imageId); const modalBody = `
Description

${escapeHtml(image.description || 'No description')}

Details

Created: ${formatDate(image.upload_date)}

Size: ${formatFileSize(image.file_size)}

Type: ${image.content_type}

${image.tags && image.tags.length > 0 ? `
Tags
${image.tags.map(tag => `${escapeHtml(tag)}`).join('')}
` : ''} `; const modalFooter = ` `; const modal = createModal('viewImageModal', 'Image Details', modalBody, modalFooter); modal.show(); } catch (error) { handleApiError(error, 'loading image details'); } } // Edit image async function editImage(imageId) { try { const image = await apiClient.getImage(imageId); const modalBody = `
Enter tags separated by commas
`; const modalFooter = ` `; const modal = createModal('editImageModal', 'Edit Image', modalBody, modalFooter); modal.show(); } catch (error) { handleApiError(error, 'loading image for editing'); } } // Save image changes async function saveImageChanges(imageId) { const description = document.getElementById('editDescription').value.trim(); const tagsInput = document.getElementById('editTags').value.trim(); const tags = tagsInput ? tagsInput.split(',').map(tag => tag.trim()).filter(tag => tag) : []; const saveButton = document.querySelector('#editImageModal .btn-primary'); setLoadingState(saveButton); try { await apiClient.updateImage(imageId, { description, tags }); showAlert('Image updated successfully!', 'success'); // Close modal and refresh images bootstrap.Modal.getInstance(document.getElementById('editImageModal')).hide(); removeModal('editImageModal'); loadImages(currentPage); } catch (error) { handleApiError(error, 'updating image'); } finally { setLoadingState(saveButton, false); } } // Delete image function deleteImage(imageId) { confirmAction('Are you sure you want to delete this image? This action cannot be undone.', async () => { try { await apiClient.deleteImage(imageId); showAlert('Image deleted successfully!', 'success'); loadImages(currentPage); // Close any open modals const modals = ['viewImageModal', 'editImageModal']; modals.forEach(modalId => { const modalElement = document.getElementById(modalId); if (modalElement) { bootstrap.Modal.getInstance(modalElement)?.hide(); removeModal(modalId); } }); } catch (error) { handleApiError(error, 'deleting image'); } }); }