/** * Comments functionality for facility management */ document.addEventListener('DOMContentLoaded', function() { console.log('Comments.js loaded'); // Initialize comment modal handlers initializeCommentModals(); // Set up form handlers setupCommentFormHandlers(); }); /** * Initialize comment modals */ function initializeCommentModals() { // Status modal (comments view) const statusModal = document.getElementById('statusModal'); if (statusModal) { statusModal.addEventListener('show.bs.modal', function(event) { console.log('Comments modal is about to show'); // Get the button that triggered the modal const button = event.relatedTarget; // Get the facility ID from the data attribute const facilityId = button.getAttribute('data-facility-id'); console.log('Facility ID for comments:', facilityId); if (!facilityId) { console.error('No facility ID found for comments'); return; } // Set the facility ID in the comment form const commentForm = document.getElementById('commentForm'); if (commentForm) { const facilityIdInput = commentForm.querySelector('#commentFacilityId'); if (facilityIdInput) { facilityIdInput.value = facilityId; } } // Load facility comments loadFacilityComments(facilityId); }); } // Edit comment modal const editCommentModal = document.getElementById('editCommentModal'); if (editCommentModal) { editCommentModal.addEventListener('show.bs.modal', function(event) { console.log('Edit comment modal is about to show'); // The button that triggered the modal will pass data via dataset const button = event.relatedTarget; const commentId = button.getAttribute('data-comment-id'); const commentText = button.getAttribute('data-comment-text'); console.log('Comment ID:', commentId, 'Comment text:', commentText); // Set the comment ID and text in the form const editForm = document.getElementById('editCommentForm'); if (editForm) { const commentIdInput = editForm.querySelector('#editCommentId'); const commentTextArea = editForm.querySelector('#editCommentText'); if (commentIdInput && commentTextArea) { commentIdInput.value = commentId; commentTextArea.value = commentText; } } }); } } /** * Set up comment form handlers */ function setupCommentFormHandlers() { // Comment form handler const commentForm = document.getElementById('commentForm'); if (commentForm) { setupCommentFormHandler(commentForm); } // Edit comment form handler const editCommentForm = document.getElementById('editCommentForm'); if (editCommentForm) { setupEditCommentFormHandler(editCommentForm); } } /** * Set up a single comment form handler * @param {HTMLFormElement} commentForm - The comment form element */ function setupCommentFormHandler(commentForm) { commentForm.addEventListener('submit', async function(e) { e.preventDefault(); // Prevent duplicate submissions if (this.submitting) { return; } this.submitting = true; // Check if user is authenticated if (!isAuthenticated()) { alert('You must be logged in to add comments'); this.submitting = false; return; } const formData = new FormData(this); // Get form data const commentText = formData.get('commentText'); const facilityId = formData.get('facilityId'); console.log('Comment form data:', { facilityId, commentText }); try { console.log('Sending comment request...'); // Use the API client to add a status comment const data = await window.api.addFacilityStatus(facilityId, commentText); console.log('Comment response:', data); if (data.success) { console.log('Comment added successfully'); // Reset the form this.reset(); // Reload comments to show the new one loadFacilityComments(facilityId); } else { console.error('Comment failed:', data.error); alert(data.error || 'Failed to add comment'); } } catch (error) { console.error('Error adding comment:', error); alert('Failed to add comment: ' + error.message); } finally { this.submitting = false; } }); } /** * Set up a single edit comment form handler * @param {HTMLFormElement} editCommentForm - The edit comment form element */ function setupEditCommentFormHandler(editCommentForm) { editCommentForm.addEventListener('submit', async function(e) { e.preventDefault(); // Prevent duplicate submissions if (this.submitting) { return; } this.submitting = true; // Check if user is authenticated if (!isAuthenticated()) { alert('You must be logged in to edit comments'); this.submitting = false; return; } const formData = new FormData(this); // Get form data const commentText = formData.get('editCommentText'); const commentId = formData.get('commentId'); const facilityId = document.getElementById('commentFacilityId').value; console.log('Edit comment form data:', { commentId, facilityId, commentText }); try { console.log('Sending edit comment request...'); // Use the API client to update a status comment const data = await window.api.updateFacilityStatus(commentId, commentText, facilityId); console.log('Edit comment response:', data); if (data.success) { console.log('Comment edited successfully'); // Close the edit modal const editModal = bootstrap.Modal.getInstance(document.getElementById('editCommentModal')); if (editModal) { editModal.hide(); } // Reload comments to show the updated one loadFacilityComments(facilityId); } else { console.error('Edit comment failed:', data.error); alert(data.error || 'Failed to edit comment'); } } catch (error) { console.error('Error editing comment:', error); alert('Failed to edit comment: ' + error.message); } finally { this.submitting = false; } }); } /** * Creates a comment form dynamically for authenticated users * @param {string} facilityId - The facility ID */ function createCommentFormForAuthenticatedUser(facilityId) { // Check if user is authenticated if (!isAuthenticated()) { return `
Loading comments...
${escapeHtml(comment.comment)}