document.addEventListener('DOMContentLoaded', function() { // Initialize modals const recordEditor = document.querySelector('custom-record-editor'); // Add record button document.querySelectorAll('.add-record-btn').forEach(btn => { btn.addEventListener('click', () => { recordEditor.setAttribute('visible', 'true'); recordEditor.setAttribute('mode', 'add'); feather.replace(); }); }); // Edit buttons document.querySelectorAll('.edit-btn').forEach(btn => { btn.addEventListener('click', (e) => { const row = e.target.closest('tr'); const recordData = { type: row.querySelector('.record-type').textContent, name: row.querySelector('.record-name').textContent, content: row.querySelector('.record-content').textContent, ttl: row.querySelector('.record-ttl').textContent, priority: row.querySelector('.record-priority')?.textContent || '' }; recordEditor.setAttribute('visible', 'true'); recordEditor.setAttribute('mode', 'edit'); populateEditForm(recordData); feather.replace(); }); }); // Delete buttons with confirmation document.querySelectorAll('.delete-btn').forEach(btn => { btn.addEventListener('click', async (e) => { if (!confirm('Are you sure you want to delete this record?')) return; const row = e.target.closest('tr'); try { // Simulate API call await new Promise(resolve => setTimeout(resolve, 500)); row.remove(); showToast('Record deleted successfully'); } catch (error) { showToast('Error deleting record', 'error'); } }); }); // Handle form submission const recordForm = document.getElementById('record-form'); if (recordForm) { recordForm.addEventListener('submit', async function(e) { e.preventDefault(); const formData = new FormData(recordForm); const recordData = Object.fromEntries(formData.entries()); try { // Simulate API call await new Promise(resolve => setTimeout(resolve, 800)); recordEditor.setAttribute('visible', 'false'); showToast('Record saved successfully'); // Refresh the table or update the specific row if (recordEditor.getAttribute('mode') === 'add') { addRecordToTable(recordData); } } catch (error) { showToast('Error saving record', 'error'); } }); } // Helper functions function populateEditForm(data) { const shadow = recordEditor.shadowRoot; shadow.getElementById('record-type').value = data.type; shadow.getElementById('record-name').value = data.name; shadow.getElementById('record-content').value = data.content; shadow.getElementById('record-ttl').value = data.ttl; if (data.priority && ['MX', 'SRV'].includes(data.type)) { shadow.getElementById('record-priority').value = data.priority; shadow.getElementById('priority-field').classList.remove('hidden'); } } function addRecordToTable(data) { const tableBody = document.querySelector('tbody'); const newRow = document.createElement('tr'); newRow.innerHTML = `