// Data handling functionality for admin interface // Format date string to readable format function formatDate(dateString) { if (!dateString) return '-'; try { var date = new Date(dateString); if (isNaN(date.getTime())) return 'Invalid Date Format'; return date.toLocaleString(); } catch (e) { return 'Date Error'; } } // Show toast message (fallback to alert if toast container is missing) function showToast(msg) { var toastElement = document.getElementById('toastMsg'); if (toastElement && toastElement.querySelector('.toast-body')) { $('#toastMsg .toast-body').text(msg); var toast = new bootstrap.Toast(toastElement); toast.show(); } else { // Simple fallback so user still gets feedback alert(msg); } } // Load step details for a user function loadStepDetails(email, sessionId) { $('#detailsModalLabel').text('Step Details for ' + email); $('#exportDetailsExcelBtn').show().off('click').on('click', function() { var exportUrl = '/Admin/ExportStepDetailsExcel?email=' + encodeURIComponent(email); if (sessionId) { exportUrl += '&sessionId=' + encodeURIComponent(sessionId); } window.location = exportUrl; }); $('#loading').show(); $('#detailsContent').empty(); // Show the modal properly var popup = document.querySelector('#detailsModal'); if (popup) { popup.classList.add('show'); popup.style.display = 'block'; popup.style.opacity = '1'; popup.style.pointerEvents = 'auto'; } // Fetch details from server console.log('Fetching details for email:', email, 'sessionId:', sessionId); $.ajax({ url: '/Admin/GetDomainStepDetails', type: 'GET', data: { email: email, sessionId: sessionId }, timeout: 30000, // 30 second timeout success: function(response) { console.log('Response received:', response); $('#loading').hide(); if (response && response.success && response.stepDetails && response.stepDetails.length > 0) { var html = '
'; html += ''; html += ''; $.each(response.stepDetails, function(i, item) { html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; }); html += '
User EmailStep NameLabel NameLabel ValuesCreated DateModified Date
' + (item.userEmailID || item.UserEmailID || '-') + '' + (item.stepName || item.StepName || '-') + '' + (item.labelName || item.LabelName || '-') + '' + (item.labelValues || item.LabelValues || '-') + '' + formatDate(item.createDateTime || item.CreateDateTime) + '' + ((item.updatedDateTime || item.UpdatedDateTime) ? formatDate(item.updatedDateTime || item.UpdatedDateTime) : '-') + '
'; $('#detailsContent').html(html); } else { $('#detailsContent').html('
No details found for this user.
'); } }, error: function(xhr, status, error) { $('#loading').hide(); console.error('Error loading details:', error); showToast('Error loading details: ' + error); $('#detailsContent').html('
Error loading details: ' + error + '
'); }, timeout: function() { $('#loading').hide(); console.error('Request timeout'); showToast('Request timeout. Please try again.'); $('#detailsContent').html('
Request timeout. Please try again.
'); } }); } // Store admin email and load form function storeAdminEmailAndLoadForm(adminUsername) { $.post('/Admin/ClearEditingEmail', function() { $.ajax({ url: '/Admin/StoreAdminEmail', type: 'POST', data: { email: adminUsername }, success: function(response) { if (response.success) { // Load the first step (stp_personneldetails) without session ID loadFormIframe('/SubdomainForm/Step/stp_personneldetails?modal=1', adminUsername); } else { showToast('Failed to store admin information. Please try again.'); } }, error: function() { showToast('An error occurred. Please try again.'); } }); }); } // Store user email and load form for editing function storeUserEmailAndLoadForm(userEmail, sessionId) { $.ajax({ url: '/Admin/StoreUserEmail', type: 'POST', data: { email: userEmail, sessionId: sessionId }, success: function(response) { if (response.success) { // Load the first step (stp_personneldetails) instead of OTP step loadFormIframe('/SubdomainForm/Step/stp_personneldetails?modal=1&sessionId=' + encodeURIComponent(sessionId), userEmail); } else { showToast('Failed to store user information. Please try again.'); } }, error: function() { showToast('An error occurred. Please try again.'); } }); }