// IP History Modal functionality document.addEventListener('DOMContentLoaded', function() { const modal = document.getElementById('ip-history-modal'); const closeBtn = document.getElementById('close-ip-history-modal'); const content = document.getElementById('ip-history-content'); const ipAddressSpan = document.getElementById('modal-ip-address'); // Open modal when IP is clicked document.querySelectorAll('.ip-history-btn').forEach(btn => { btn.addEventListener('click', function() { const ip = this.getAttribute('data-ip'); ipAddressSpan.textContent = ip; modal.classList.remove('hidden'); modal.classList.add('flex'); loadIPHistory(ip); }); }); // Close modal closeBtn.addEventListener('click', function() { modal.classList.add('hidden'); modal.classList.remove('flex'); }); // Close modal when clicking outside modal.addEventListener('click', function(e) { if (e.target === modal) { modal.classList.add('hidden'); modal.classList.remove('flex'); } }); // Close modal with Escape key document.addEventListener('keydown', function(e) { if (e.key === 'Escape' && !modal.classList.contains('hidden')) { modal.classList.add('hidden'); modal.classList.remove('flex'); } }); function loadIPHistory(ip) { content.innerHTML = '
Loading...
'; fetch(`/ip/${encodeURIComponent(ip)}/history`) .then(response => response.json()) .then(data => { if (data.history && data.history.length > 0) { displayHistory(data.history); } else { content.innerHTML = '
No history found for this IP address.
'; } }) .catch(error => { console.error('Error loading IP history:', error); content.innerHTML = '
Error loading IP history. Please try again.
'; }); } function displayHistory(history) { let html = '
'; history.forEach((entry, index) => { const isAssigned = entry.action === 'assigned'; const icon = isAssigned ? 'fa-plus-circle text-green-500' : 'fa-minus-circle text-red-500'; const actionText = isAssigned ? 'Assigned' : 'Removed'; // Format timestamp let timestamp = 'Unknown'; if (entry.timestamp) { try { const date = new Date(entry.timestamp); timestamp = date.toLocaleString(); } catch (e) { timestamp = entry.timestamp; } } html += `
${actionText} to ${entry.device_name || 'Unknown'}
${entry.subnet_name || 'Unknown'} (${entry.subnet_cidr || 'N/A'})
by ${entry.user_name || 'Unknown'} • ${timestamp}
`; }); html += '
'; content.innerHTML = html; } });