Changed around line 1
+ let entries = JSON.parse(localStorage.getItem('phoneEntries')) || [];
+ const tableBody = document.querySelector('#directoryTable tbody');
+ const entryDialog = document.getElementById('entryDialog');
+ const entryForm = document.getElementById('entryForm');
+ const addEntryBtn = document.getElementById('addEntry');
+ const cancelBtn = document.getElementById('cancelBtn');
+ const searchInput = document.getElementById('search');
+
+ let currentEditIndex = null;
+
+ function renderTable() {
+ tableBody.innerHTML = '';
+ entries.forEach((entry, index) => {
+ const row = document.createElement('tr');
+ row.innerHTML = `
+
${entry.name} | +
${entry.office} | +
${entry.phone} | +
${new Date(entry.lastUpdated).toLocaleString()} | + `;
+ tableBody.appendChild(row);
+ });
+ }
+
+ function addEntry(entry) {
+ entry.lastUpdated = new Date();
+ entries.push(entry);
+ localStorage.setItem('phoneEntries', JSON.stringify(entries));
+ renderTable();
+ }
+
+ function editEntry(index) {
+ currentEditIndex = index;
+ const entry = entries[index];
+ document.getElementById('name').value = entry.name;
+ document.getElementById('office').value = entry.office;
+ document.getElementById('phone').value = entry.phone;
+ entryDialog.showModal();
+ }
+
+ function updateEntry(index, updatedEntry) {
+ updatedEntry.lastUpdated = new Date();
+ entries[index] = updatedEntry;
+ localStorage.setItem('phoneEntries', JSON.stringify(entries));
+ renderTable();
+ }
+
+ function deleteEntry(index) {
+ if (confirm('Are you sure you want to delete this entry?')) {
+ entries.splice(index, 1);
+ localStorage.setItem('phoneEntries', JSON.stringify(entries));
+ renderTable();
+ }
+ }
+
+ // Event Listeners
+ addEntryBtn.addEventListener('click', () => {
+ currentEditIndex = null;
+ entryForm.reset();
+ entryDialog.showModal();
+ });
+
+ cancelBtn.addEventListener('click', () => {
+ entryDialog.close();
+ });
+
+ entryForm.addEventListener('submit', (e) => {
+ e.preventDefault();
+ const entry = {
+ name: document.getElementById('name').value,
+ office: document.getElementById('office').value,
+ phone: document.getElementById('phone').value
+ };
+
+ if (currentEditIndex !== null) {
+ updateEntry(currentEditIndex, entry);
+ } else {
+ addEntry(entry);
+ }
+ entryDialog.close();
+ });
+
+ searchInput.addEventListener('input', () => {
+ const searchTerm = searchInput.value.toLowerCase();
+ const filteredEntries = entries.filter(entry =>
+ entry.name.toLowerCase().includes(searchTerm) ||
+ entry.office.toLowerCase().includes(searchTerm)
+ );
+ entries = filteredEntries.length > 0 ? filteredEntries : JSON.parse(localStorage.getItem('phoneEntries')) || [];
+ renderTable();
+ });
+
+ // Initial render
+ renderTable();