export class FAQModal { constructor(app) { this.app = app this.id = 'faqModal' this.element = null this.bsModal = null this._editId = null } open(faq = null) { if (!this.element) this._build() this._editId = faq ? faq.id : null const title = this.element.querySelector('.modal-title') const inputs = this.element.querySelectorAll('input[name], textarea[name], select[name]') if (faq) { title.innerHTML = 'Editar pregunta' inputs.forEach(i => { const key = i.getAttribute('name') if (faq[key] !== undefined) i.value = faq[key] if (i.type === 'checkbox') i.checked = faq[key] !== false }) } else { title.innerHTML = 'Añadir pregunta' inputs.forEach(i => { i.value = ''; if (i.type === 'checkbox') i.checked = true }) this.element.querySelector('select[name="category"]').value = 'general' this.element.querySelector('input[name="order_num"]').value = '' } this.bsModal = new bootstrap.Modal(this.element) this.bsModal.show() } close() { if (this.bsModal) this.bsModal.hide() } async save() { const data = this._collect() if (!data.question_es || !data.question_es.trim()) { this.app.showNotification('La pregunta es obligatoria', 'error') return } if (!data.answer_es || !data.answer_es.trim()) { this.app.showNotification('La respuesta es obligatoria', 'error') return } const res = this._editId ? await API.updateFAQ(this._editId, data) : await API.createFAQ(data) if (res.success) { this.close() this.app.showNotification(this._editId ? 'FAQ actualizado' : 'FAQ creado', 'success') this.app.refreshSection('faq') } else { this.app.showNotification(res.error || 'Error al guardar', 'error') } } _collect() { const d = {} this.element.querySelectorAll('input[name], textarea[name], select[name]').forEach(i => { const k = i.getAttribute('name') d[k] = i.type === 'checkbox' ? i.checked : i.value }) d.order_num = parseInt(d.order_num) || 0 d.is_active = d.is_active !== undefined ? d.is_active : true return d } _build() { const el = document.createElement('div') el.id = this.id el.className = 'modal fade' el.innerHTML = ` ` document.body.appendChild(el) el.querySelector('#faqSaveBtn').addEventListener('click', () => this.save()) this.element = el } }