`) printWin.document.close() } async function pagamentoSalvar() { const responsavel = document.getElementById('pag-responsavel')?.value?.trim() const nome = document.getElementById('pag-nome')?.value?.trim() const valor = document.getElementById('pag-valor')?.value?.trim() const valorExtenso = document.getElementById('pag-valor-extenso')?.value?.trim() || '' const data = document.getElementById('pag-data')?.value if (!responsavel) { notify('Informe o Responsável.', 'warning'); return } if (!nome) { notify('Informe o Nome do Paciente.', 'warning'); return } if (!valor) { notify('Informe o Valor.', 'warning'); return } if (!data) { notify('Informe a Data.', 'warning'); return } const body = { responsavel, nome, valor, valor_extenso: valorExtenso, especialidade: document.getElementById('pag-especialidade')?.value || '', data_pagamento: data, cidade: document.getElementById('pag-cidade')?.value || '', obs: document.getElementById('pag-obs')?.value || '' } try { if (_pagEditId) await api.put('/api/pagamentos/' + _pagEditId, body) else await api.post('/api/pagamentos', body) notify(_pagEditId ? 'Recibo atualizado!' : 'Recibo salvo!', 'success') pagamentoCancelar() await loadPagamentos() } catch(e) { notify(e.response?.data?.error || 'Erro ao salvar', 'error') } } async function pagamentoExcluir(id) { if (!confirm('Deseja excluir este recibo?')) return try { await api.delete('/api/pagamentos/' + id) notify('Recibo excluído.', 'success') _pagEditId = null document.getElementById('pag-form-wrap').style.display = 'none' await loadPagamentos() } catch(e) { notify('Erro ao excluir.', 'error') } } function gerarReciboPDFDireto(p) { // Preenche formulário com os dados do recibo e chama gerarReciboPDF const formWrap = document.getElementById('pag-form-wrap') if (!formWrap) return _pagEditId = p.id document.getElementById('pag-responsavel').value = p.responsavel || '' document.getElementById('pag-nome').value = p.nome || '' document.getElementById('pag-valor').value = p.valor || '' const extEl = document.getElementById('pag-valor-extenso') if (extEl) extEl.value = p.valor_extenso || '' const espEl = document.getElementById('pag-especialidade') if (espEl) espEl.value = p.especialidade || '' document.getElementById('pag-data').value = p.data_pagamento || p.data || '' document.getElementById('pag-cidade').value = p.cidade || '' document.getElementById('pag-obs').value = p.obs || '' formWrap.style.display = 'block' gerarReciboPDF() } // ============================================================ // ATIVIDADES // ============================================================ let _ativState = { lista: [], editId: null } async function renderAtividadesAdmin() { const content = document.getElementById('section-content') content.innerHTML = `

Atividades

` await loadAtividades() } function atividadeFotoPreview(input) { const preview = document.getElementById('atv-foto-preview') if (!preview) return if (!input.files || !input.files[0]) { preview.innerHTML = ''; return } const file = input.files[0] if (file.size > 2 * 1024 * 1024) { notify('Foto muito grande. Máximo 2MB.', 'warning'); input.value = ''; preview.innerHTML = ''; return } const reader = new FileReader() reader.onload = (e) => { preview.innerHTML = `Preview` preview.dataset.base64 = e.target.result } reader.readAsDataURL(file) } async function loadAtividades() { const lista = document.getElementById('atv-lista') if (!lista) return try { const { data } = await api.get('/api/atividades') _ativState.lista = data || [] if (!_ativState.lista.length) { lista.innerHTML = `

Nenhuma atividade cadastrada

Clique em "+ Nova Atividade" para adicionar

` return } lista.innerHTML = _ativState.lista.map(a => `
${a.foto ? `
Foto
` : `
`}

${a.descricao || '—'}

${a.valor ? `

R$ ${a.valor}

` : ''}
`).join('') } catch(e) { lista.innerHTML = `
Erro ao carregar atividades.
` } } function atividadeNova() { _ativState.editId = null document.getElementById('atv-form-title').innerHTML = 'Nova Atividade' document.getElementById('atv-descricao').value = '' document.getElementById('atv-valor').value = '' document.getElementById('atv-foto-input').value = '' document.getElementById('atv-foto-preview').innerHTML = '' document.getElementById('atv-foto-preview').dataset.base64 = '' document.getElementById('atv-form-wrap').style.display = 'block' document.getElementById('atv-form-wrap').scrollIntoView({ behavior: 'smooth' }) } function atividadeEditar(a) { _ativState.editId = a.id document.getElementById('atv-form-title').innerHTML = 'Editar Atividade' document.getElementById('atv-descricao').value = a.descricao || '' document.getElementById('atv-valor').value = a.valor || '' document.getElementById('atv-foto-input').value = '' const prev = document.getElementById('atv-foto-preview') prev.innerHTML = a.foto ? `Foto atual` : '' prev.dataset.base64 = a.foto || '' document.getElementById('atv-form-wrap').style.display = 'block' document.getElementById('atv-form-wrap').scrollIntoView({ behavior: 'smooth' }) } function atividadeCancelar() { _ativState.editId = null document.getElementById('atv-form-wrap').style.display = 'none' } async function atividadeSalvar() { const descricao = document.getElementById('atv-descricao')?.value?.trim() if (!descricao) { notify('Informe a Descrição da Atividade.', 'warning'); return } const foto = document.getElementById('atv-foto-preview')?.dataset?.base64 || '' const body = { descricao, valor: document.getElementById('atv-valor')?.value || '', foto } try { if (_ativState.editId) await api.put('/api/atividades/' + _ativState.editId, body) else await api.post('/api/atividades', body) notify(_ativState.editId ? 'Atividade atualizada!' : 'Atividade salva!', 'success') atividadeCancelar() await loadAtividades() } catch(e) { notify(e.response?.data?.error || 'Erro ao salvar', 'error') } } async function atividadeExcluir(id) { if (!confirm('Deseja excluir esta atividade?')) return try { await api.delete('/api/atividades/' + id) notify('Atividade excluída.', 'success') await loadAtividades() } catch(e) { notify('Erro ao excluir.', 'error') } } // ============================================================ // AUTO LOGIN CHECK // ============================================================ async function checkAuth() { if (state.sessionId) { try { const { data } = await api.get('/api/auth/me') state.user = data.user } catch { state.sessionId = null localStorage.removeItem('session_id') } } renderApp() } // Start checkAuth()