const ADBWohneinheitContactManager = { modal: null, homeId: null, header: null, contacts: [], editingIndex: null, init() { document.body.addEventListener('click', this.handleBodyClick.bind(this)); }, handleBodyClick(e) { const trigger = e.target.closest('[data-home-id][data-home-contact]'); if (trigger) { e.preventDefault(); this.homeId = trigger.dataset.homeId; this.openModal(); } }, async openModal() { this.editingIndex = null; this.createModal(); this.showLoading(); try { const response = await axios.post('/ADBWohneinheit/getContacts', { id: this.homeId }); if (response.data.success) { this.contacts = response.data.contacts || []; this.header = response.data.header || {}; this.render(); } else { this.showError(response.data.message); } } catch (error) { this.showError('Fehler beim Laden der Kontaktdaten.'); console.error(error); } }, createModal() { if (this.modal) { $(this.modal).modal('hide'); this.modal.remove(); } const modalHtml = ` `; document.body.insertAdjacentHTML('beforeend', modalHtml); this.modal = document.getElementById('contactManagerModal'); $(this.modal).modal('show'); this.modal.addEventListener('hidden.bs.modal', () => { if (document.body.contains(this.modal)) { this.modal.remove(); } this.modal = null; }); }, render() { const body = this.modal.querySelector('.modal-body'); const contactListHtml = this.contacts.map((contact, index) => this.renderContact(contact, index)).join(''); this.modal.querySelector('.modal-title').innerText = `Kontakt verwalten (WE-ID: ${this.homeId}) [${this.header}]`; body.innerHTML = `
Bestehende Kontakte
${this.contacts.length ? `` : '

Keine Kontakte vorhanden.

'}

${this.editingIndex !== null ? 'Kontakt bearbeiten' : 'Neuen Kontakt hinzufügen'}
${this.renderForm(this.editingIndex !== null ? this.contacts[this.editingIndex] : {})}
`; this.attachFormListeners(); }, renderContact(contact, index) { const isCompany = contact.firma && contact.firma.trim() !== ''; const displayName = isCompany ? contact.firma : `${contact.vorname || ''} ${contact.nachname || ''}`.trim(); const typeLabel = contact.type === 'renter' ? 'Mieter' : (contact.type === 'prospect' ? 'Interessent' : ''); const typeBadge = typeLabel ? `${typeLabel}` : ''; return `
  • ${displayName}${typeBadge}
    ${contact.telefon || 'Kein Telefon'} | ${contact.email || 'Keine E-Mail'}
  • `; }, renderForm(contact = {}) { return `

    ${this.editingIndex !== null ? `` : ''}
    `; }, attachFormListeners() { const form = this.modal.querySelector('#contactForm'); if (!form) return; const firmaInput = form.querySelector('[name="firma"]'); const vornameInput = form.querySelector('[name="vorname"]'); const nachnameInput = form.querySelector('[name="nachname"]'); const togglePersonFields = () => { const isCompany = firmaInput.value.trim() !== ''; vornameInput.disabled = isCompany; nachnameInput.disabled = isCompany; if (isCompany) { vornameInput.value = ''; nachnameInput.value = ''; } }; const toggleCompanyField = () => { const isPerson = vornameInput.value.trim() !== '' || nachnameInput.value.trim() !== ''; firmaInput.disabled = isPerson; if (isPerson) { firmaInput.value = ''; } }; firmaInput.addEventListener('input', togglePersonFields); vornameInput.addEventListener('input', toggleCompanyField); nachnameInput.addEventListener('input', toggleCompanyField); togglePersonFields(); toggleCompanyField(); form.addEventListener('submit', this.handleFormSubmit.bind(this)); this.modal.querySelector('.modal-body').addEventListener('click', (e) => { const button = e.target.closest('button[data-action]'); if(!button) return; e.preventDefault(); const action = button.dataset.action; const index = button.dataset.index; if (action === 'edit') this.handleEdit(index); if (action === 'delete') this.handleDelete(index); if (action === 'cancel-edit') this.handleCancelEdit(); }); }, handleFormSubmit(e) { e.preventDefault(); const formData = new FormData(e.target); const newContact = Object.fromEntries(formData.entries()); if (this.editingIndex !== null) { this.contacts[this.editingIndex] = newContact; } else { this.contacts.push(newContact); } this.saveContacts(); }, handleEdit(index) { this.editingIndex = parseInt(index, 10); this.render(); }, handleCancelEdit() { this.editingIndex = null; this.render(); }, handleDelete(index) { if (confirm('Sollen die Kontaktdaten wirklich gelöscht werden?')) { this.contacts.splice(index, 1); this.saveContacts(); } }, async saveContacts() { this.showLoading('Speichern...'); try { const response = await axios.post('/ADBWohneinheit/saveContacts', { id: this.homeId, data: this.contacts }); if (response.data.success) { this.editingIndex = null; const freshResponse = await axios.post('/ADBWohneinheit/getContacts', { id: this.homeId }); this.contacts = freshResponse.data.contacts || []; this.render(); window.notify('success', response.data.message); } else { this.showError(response.data.message); } } catch(error) { this.showError('Fehler beim Speichern der Kontaktdaten.'); console.error(error); } }, showLoading(message = 'Laden...') { const body = this.modal.querySelector('.modal-body'); body.innerHTML = `
    Loading...

    ${message}

    `; }, showError(message) { const body = this.modal.querySelector('.modal-body'); body.innerHTML = `
    ${message}
    `; setTimeout(() => this.render(), 3000); } }; document.addEventListener('DOMContentLoaded', () => { ADBWohneinheitContactManager.init(); });