File size: 5,893 Bytes
d7c84a4
4703cc6
d7c84a4
4703cc6
 
d7c84a4
 
 
4703cc6
 
d7c84a4
4703cc6
d7c84a4
4703cc6
d7c84a4
 
 
 
 
 
 
 
 
 
 
 
4703cc6
 
d7c84a4
 
4703cc6
 
 
d7c84a4
 
 
 
 
 
 
 
 
 
 
 
 
4703cc6
 
 
 
 
 
 
d7c84a4
4703cc6
 
 
 
d7c84a4
 
 
 
 
 
 
 
 
 
 
 
 
 
4703cc6
 
d7c84a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

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 = `
            <td class="px-6 py-4 whitespace-nowrap">
                <span class="record-type px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${getTypeBadgeClass(data.type)}">${data.type}</span>
            </td>
            <td class="record-name px-6 py-4 whitespace-nowrap text-sm text-gray-900">${data.name}</td>
            <td class="record-content px-6 py-4 whitespace-nowrap text-sm text-gray-900">${data.content}</td>
            <td class="record-ttl px-6 py-4 whitespace-nowrap text-sm text-gray-900">${data.ttl}</td>
            <td class="record-priority px-6 py-4 whitespace-nowrap text-sm text-gray-900">${data.priority || '-'}</td>
            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium flex gap-2">
                <button class="edit-btn text-primary-600 hover:text-primary-900">
                    <i data-feather="edit-2"></i>
                </button>
                <button class="delete-btn text-red-600 hover:text-red-900">
                    <i data-feather="trash-2"></i>
                </button>
            </td>
        `;
        tableBody.appendChild(newRow);
        feather.replace();
    }
    
    function getTypeBadgeClass(type) {
        const classes = {
            'A': 'bg-green-100 text-green-800',
            'AAAA': 'bg-emerald-100 text-emerald-800',
            'CNAME': 'bg-purple-100 text-purple-800',
            'MX': 'bg-blue-100 text-blue-800',
            'TXT': 'bg-yellow-100 text-yellow-800',
            'NS': 'bg-red-100 text-red-800',
            'SRV': 'bg-lime-100 text-lime-800'
        };
        return classes[type] || 'bg-gray-100 text-gray-800';
    }
    
    function showToast(message, type = 'success') {
        const toast = document.createElement('div');
        toast.className = `fixed top-4 right-4 z-50 px-4 py-2 rounded-md shadow-md text-white ${
            type === 'success' ? 'bg-green-500' : 'bg-red-500'
        }`;
        toast.textContent = message;
        document.body.appendChild(toast);
        
        setTimeout(() => {
            toast.classList.add('opacity-0', 'transition-opacity', 'duration-300');
            setTimeout(() => toast.remove(), 300);
        }, 3000);
    }
});