$(document).ready(function(){ const baseUrl = base_url + "/permits/getcomments"; fetchPermitApplicationComments(); // CREATE $(".submitPermitCommentBtn").click(function (e) { e.preventDefault(); // let formData = $(this).serialize(); var commentBody = $('.commentBody').val(); var applicationCode = $('.applicationCode').val(); const formData = new FormData(); formData.append('comment_body', commentBody); formData.append('application_code', applicationCode); $.ajax({ url: base_url + "/permits/addcomment", type: "POST", data: formData, processData: false, contentType: false, headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"), "Accept": "application/json" }, success: function (response) { if (response.success === true) { $.alert("Comment successfully submitted!"); fetchPermitApplicationComments(); } else{ $.alert(response.msg); } console.log(response); }, error: function (xhr) { alert("Error: " + xhr.responseText); } }); }); // READ (fetch all) function fetchPermitApplicationComments() { var applicationCode = $('.applicationCode').val(); const formData = new FormData(); formData.append('application_code', applicationCode); $.ajax({ url: baseUrl, type: "POST", data: formData, processData: false, contentType: false, headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"), "Accept": "application/json" }, success: function (data) { console.log("Fetched Comments:", data); // $("#permitTableBody").empty(); populateCommentsTimeline(data) // $.each(data, function (index, app) { // $("#permitTableBody").append(` // // ${app.id} // ${app.user_id} // ${app.application_code} // ${app.comment_body ?? ""} // // // // // // `); // }); }, error: function (xhr) { alert("Failed to fetch applications."); } }); } // UPDATE $(document).on("click", ".editBtn", function () { let id = $(this).data("id"); let updatedData = { comment_body: prompt("Enter new comment:") }; $.ajax({ url: `${baseUrl}/${id}`, type: "PUT", data: updatedData, headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"), "Accept": "application/json" }, success: function (response) { alert("Application Updated!"); console.log(response); fetchPermitApplicationComments(); }, error: function (xhr) { alert("Update failed: " + xhr.responseText); } }); }); // DELETE $(document).on("click", ".deleteBtn", function () { let id = $(this).data("id"); if (confirm("Are you sure you want to delete this application?")) { $.ajax({ url: `${baseUrl}/${id}`, type: "DELETE", headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"), "Accept": "application/json" }, success: function (response) { alert("Application Deleted!"); console.log(response); fetchPermitApplicationComments(); }, error: function (xhr) { alert("Delete failed: " + xhr.responseText); } }); } }); // Example AJAX setup assuming your response is stored in a variable called 'response' function populateCommentsTimeline(response) { const $timeline = $('#commentsTimeline'); // Clear out any old data or loading spinners $timeline.empty(); // Check if we have a successful response with data if (response.success && response.count > 0) { // Loop through each comment in the data array $.each(response.data, function(index, item) { // Format the date to look like "19 Jun 2026, 06:55 PM" const dateObj = new Date(item.created_date); const formattedDate = dateObj.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit', hour12: true }); // Construct the Timeline Item HTML const timelineHtml = `
${item.created_by} ${formattedDate}

${item.app_comments}

`; // Append it to the container $timeline.append(timelineHtml); }); } else { // Fallback UI if there are no comments yet $timeline.append(`

No history or comments found for this application.

`); } } });