`;
if (window.lucide) lucide.createIcons();
const res = await api.get('/contacts');
state.contacts = res.data;
renderList();
// Suche
let searchTimer;
document.getElementById('contacts-search').addEventListener('input', (e) => {
clearTimeout(searchTimer);
searchTimer = setTimeout(() => {
state.searchQuery = e.target.value.trim();
renderList();
}, 200);
});
// Kategorie-Filter
document.getElementById('contacts-filters').addEventListener('click', (e) => {
const chip = e.target.closest('[data-cat]');
if (!chip) return;
document.querySelectorAll('.contact-filter-chip').forEach((c) =>
c.classList.toggle('contact-filter-chip--active', c === chip)
);
state.activeCategory = chip.dataset.cat || null;
renderList();
});
// Neu
document.getElementById('contacts-add-btn').addEventListener('click', () =>
openModal({ mode: 'create' })
);
}
// --------------------------------------------------------
// Liste rendern
// --------------------------------------------------------
function filterContacts() {
let list = state.contacts;
if (state.activeCategory) {
list = list.filter((c) => c.category === state.activeCategory);
}
if (state.searchQuery) {
const q = state.searchQuery.toLowerCase();
list = list.filter((c) =>
c.name.toLowerCase().includes(q) ||
(c.phone && c.phone.toLowerCase().includes(q)) ||
(c.email && c.email.toLowerCase().includes(q))
);
}
return list;
}
function renderList() {
const container = document.getElementById('contacts-list');
if (!container) return;
const contacts = filterContacts();
if (!contacts.length) {
container.innerHTML = `
Keine Kontakte gefunden
`;
if (window.lucide) lucide.createIcons();
return;
}
// Nach Kategorie gruppieren
const groups = {};
for (const c of contacts) {
if (!groups[c.category]) groups[c.category] = [];
groups[c.category].push(c);
}
container.innerHTML = Object.entries(groups)
.sort(([a], [b]) => CATEGORIES.indexOf(a) - CATEGORIES.indexOf(b))
.map(([cat, items]) => `