document.addEventListener('DOMContentLoaded', () => { // General Admin Elements const loginModal = document.getElementById('login-modal'); const loginForm = document.getElementById('loginForm'); const passwordInput = document.getElementById('passwordInput'); const loginButton = document.getElementById('loginButton'); const adminContent = document.getElementById('admin-content'); // Tabs const tabs = document.querySelectorAll('.tabs li'); const tabContents = document.querySelectorAll('.tab-content'); // Photo Gallery Elements const uploadForm = document.getElementById('uploadForm'); const uploadButton = document.getElementById('uploadButton'); const uploadStatus = document.getElementById('uploadStatus'); const uploadProgress = document.getElementById('uploadProgress'); const tagsInput = document.getElementById('tagsInput'); const tagSuggestions = document.getElementById('tagSuggestions'); const quickTagButtons = document.getElementById('quickTagButtons'); const captionInput = document.getElementById('captionInput'); const captionToTagsButton = document.getElementById('captionToTags'); const manageGallery = document.getElementById('manage-gallery'); const editModal = document.getElementById('editModal'); const editPhotoId = document.getElementById('editPhotoId'); const editCaption = document.getElementById('editCaption'); const editTags = document.getElementById('editTags'); const saveChanges = document.getElementById('saveChanges'); const modalCloseButton = editModal.querySelector('.delete'); const modalCancelButton = editModal.querySelector('.modal-card-foot .button:not(.is-success)'); // Bulk Delete Modal const bulkDeleteModal = document.getElementById('bulkDeleteModal'); const confirmBulkDeleteBtn = document.getElementById('confirmBulkDelete'); const cancelBulkDeleteBtn = document.getElementById('cancelBulkDelete'); const bulkDeleteModalCloseBtn = bulkDeleteModal.querySelector('.delete'); const bulkDeleteCountEl = document.getElementById('bulk-delete-count'); const bulkCaption = document.getElementById('bulkCaption'); const bulkTags = document.getElementById('bulkTags'); const bulkAppendTags = document.getElementById('bulkAppendTags'); const applyBulkEdits = document.getElementById('applyBulkEdits'); const bulkDelete = document.getElementById('bulkDelete'); const selectAllPhotosBtn = document.getElementById('selectAllPhotos'); const clearSelectionBtn = document.getElementById('clearSelection'); const selectedCountEl = document.getElementById('selectedCount'); const bulkPanel = document.getElementById('bulkPanel'); let selectedPhotoIds = new Set(); let photos = []; // Store Status Elements const messageInput = document.getElementById('scrollingMessageInput'); const isClosedCheckbox = document.getElementById('isClosedCheckbox'); const closedMessageInput = document.getElementById('closedMessageInput'); const updateButton = document.getElementById('updateButton'); const responseDiv = document.getElementById('response'); const backendUrl = (() => { const { protocol } = window.location; const backendHostname = 'photobackend.beachpartyballoons.com'; return `${protocol}//${backendHostname}`; // No explicit port needed as it's on 443 })(); const LAST_TAGS_KEY = 'bpb-last-tags'; let adminPassword = ''; const storedPassword = localStorage.getItem('bpb-admin-password'); const getAdminPassword = () => adminPassword || localStorage.getItem('bpb-admin-password') || ''; const showAdmin = () => { adminContent.style.display = 'block'; loginModal.classList.remove('is-active'); }; const showLogin = (message) => { if (message) { passwordInput.value = ''; passwordInput.placeholder = message; } loginModal.classList.add('is-active'); }; const handleUnauthorized = () => { localStorage.removeItem('bpb-admin-password'); adminPassword = ''; showLogin('Enter password to continue'); }; // --- Password Protection --- function login(event) { event.preventDefault(); const passwordVal = passwordInput.value.trim(); if (!passwordVal) return; adminPassword = passwordVal; localStorage.setItem('bpb-admin-password', adminPassword); showAdmin(); fetchPhotos(); fetchStatus(); preloadLastTags(); } loginForm.addEventListener('submit', login); loginButton.addEventListener('click', login); if (storedPassword) { adminPassword = storedPassword; passwordInput.value = storedPassword; showAdmin(); fetchPhotos(); fetchStatus(); preloadLastTags(); } else { showLogin(); } // --- Tab Switching --- tabs.forEach(tab => { tab.addEventListener('click', () => { tabs.forEach(item => item.classList.remove('is-active')); tab.classList.add('is-active'); const target = document.getElementById(tab.dataset.tab); tabContents.forEach(content => content.style.display = 'none'); target.style.display = 'block'; }); }); // --- Photo Management --- async function fetchPhotos() { try { const response = await fetch(`${backendUrl}/photos`); if (response.status === 401) { handleUnauthorized(); return; } photos = await response.json(); const validIds = new Set(photos.map(p => p._id)); selectedPhotoIds = new Set(Array.from(selectedPhotoIds).filter(id => validIds.has(id))); updateTagSuggestions(); updateQuickTags(); renderManageGallery(); updateBulkUI(); } catch (error) { console.error('Error fetching photos:', error); } } function renderManageGallery() { manageGallery.innerHTML = ''; if (!photos.length) { manageGallery.innerHTML = '
No photos yet. Upload a photo to get started.
Caption: ${photo.caption}
Tags: ${photo.tags.join(', ')}