document.addEventListener('DOMContentLoaded', () => { initModals(); }); let activeNotepadId = null; // Tracks which notepad is currently selected function initModals() { // --- Create Notepad Logic --- const modalPad = document.getElementById('modal-create-pad'); const btnOpenPad = document.getElementById('btn-create-pad'); const formPad = document.getElementById('form-create-pad'); if(btnOpenPad) { btnOpenPad.addEventListener('click', () => { modalPad.classList.remove('hidden'); }); } if(formPad) { formPad.addEventListener('submit', async (e) => { e.preventDefault(); const name = document.getElementById('pad-name').value; const isPublic = document.getElementById('pad-public').checked; const color = document.getElementById('pad-color').value; try { const res = await fetch('/api/notepads', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, is_public: isPublic, color: color }) }); if (res.ok) { const newPad = await res.json(); modalPad.classList.add('hidden'); formPad.reset(); // Transition to the new notepad if (typeof window.SpotNotes.loadNotesForPad === 'function') window.SpotNotes.loadNotesForPad(newPad.id, newPad.name, newPad.is_public, newPad.owner_id); } else { alert('Failed to create notepad'); } } catch (err) { console.error(err); } }); } // --- Create Note Logic --- const modalNote = document.getElementById('modal-create-note'); const formNote = document.getElementById('form-create-note'); if(formNote) { formNote.addEventListener('submit', async (e) => { e.preventDefault(); if (!activeNotepadId) { alert("No notepad selected!"); return; } const title = document.getElementById('note-title').value; const content = document.getElementById('note-content').value; // Use current map center const center = window.SpotNotes.map.getCenter(); try { const res = await fetch('/api/notes', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title, content, lat: center.lat, lng: center.lng, notepad_id: activeNotepadId }) }); if (res.ok) { modalNote.classList.add('hidden'); formNote.reset(); // Refresh notes if (typeof window.SpotNotes.loadNotesForPad === 'function') window.SpotNotes.loadNotesForPad(activeNotepadId); } else { alert('Failed to create note'); } } catch (err) { console.error(err); } }); } // Close buttons document.querySelectorAll('.close-modal').forEach(btn => { btn.addEventListener('click', (e) => { e.target.closest('.modal').classList.add('hidden'); }); }); } // Helper to open edit notepad modal (called from index.js) window.openEditPadModal = function(id, currentName, currentPublic, currentColor = '#3388ff') { document.getElementById('pad-id-edit').value = id; document.getElementById('pad-name-edit').value = currentName; document.getElementById('pad-public-edit').checked = currentPublic; document.getElementById('pad-color-edit').value = currentColor; document.getElementById('modal-edit-pad').classList.remove('hidden'); }; // Helper to open edit note modal (called from index.js) window.openEditNoteModal = function(id, currentTitle, currentContent) { document.getElementById('note-id-edit').value = id; document.getElementById('note-title-edit').value = currentTitle; document.getElementById('note-content-edit').value = currentContent; document.getElementById('modal-edit-note').classList.remove('hidden'); }; // Helper to open note modal (called from index.js) function openCreateNoteModal(notepadId) { activeNotepadId = notepadId; document.getElementById('modal-create-note').classList.remove('hidden'); } en'); }