- Auth movida a NGINX (Basic Auth en htpasswd) para que fetch() del browser incluya credenciales automáticamente — resuelve categorías sin cargar y POST fallido - Tailwind CDN descargado al build del Docker y servido localmente — elimina dependencia externa en cada carga de página - Soporte de teclado físico en add.html (números, punto/coma, Backspace, Enter) - Layout centrado en desktop con max-w-md mx-auto en las tres páginas - Eliminada imagen decorativa externa y links de fonts duplicados Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
122 lines
4.5 KiB
JavaScript
122 lines
4.5 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
let currentMonto = "0";
|
|
let selectedTipo = "gasto"; // default
|
|
|
|
const montoEl = document.getElementById('ui-monto');
|
|
const btnGasto = document.getElementById('btn-gasto');
|
|
const btnIngreso = document.getElementById('btn-ingreso');
|
|
const numpad = document.getElementById('ui-numpad');
|
|
const btnSave = document.getElementById('btn-save');
|
|
const iptFecha = document.getElementById('ui-fecha');
|
|
|
|
// Default a hoy
|
|
iptFecha.valueAsDate = new Date();
|
|
|
|
// Cargar categorías desde la API
|
|
fetch('api/movimientos.php')
|
|
.then(r => r.json())
|
|
.then(data => {
|
|
if (data.categorias) {
|
|
const sel = document.getElementById('ui-categoria');
|
|
sel.innerHTML = data.categorias.map(c =>
|
|
`<option value="${c.id}">${c.nombre}</option>`
|
|
).join('');
|
|
}
|
|
})
|
|
.catch(() => {});
|
|
|
|
// Tipo selector
|
|
const updateTipoUI = () => {
|
|
if (selectedTipo === 'gasto') {
|
|
btnGasto.classList.replace('text-on-surface-variant', 'text-primary');
|
|
btnGasto.classList.add('bg-surface-container-lowest');
|
|
btnIngreso.classList.replace('bg-surface-container-lowest', 'bg-transparent');
|
|
btnIngreso.classList.replace('text-primary', 'text-on-surface-variant');
|
|
} else {
|
|
btnIngreso.classList.replace('text-on-surface-variant', 'text-primary');
|
|
btnIngreso.classList.add('bg-surface-container-lowest');
|
|
btnGasto.classList.replace('bg-surface-container-lowest', 'bg-transparent');
|
|
btnGasto.classList.replace('text-primary', 'text-on-surface-variant');
|
|
}
|
|
};
|
|
|
|
btnGasto.addEventListener('click', () => { selectedTipo = 'gasto'; updateTipoUI(); });
|
|
btnIngreso.addEventListener('click', () => { selectedTipo = 'ingreso'; updateTipoUI(); });
|
|
|
|
function applyInput(val) {
|
|
if (val === 'backspace') {
|
|
currentMonto = currentMonto.slice(0, -1);
|
|
if (currentMonto === '') currentMonto = "0";
|
|
} else if (/^[0-9]$/.test(val)) {
|
|
if (currentMonto === "0") {
|
|
currentMonto = val;
|
|
} else {
|
|
currentMonto += val;
|
|
}
|
|
} else if (val === '.') {
|
|
if (!currentMonto.includes('.')) currentMonto += '.';
|
|
}
|
|
montoEl.textContent = currentMonto;
|
|
}
|
|
|
|
// Numpad táctil
|
|
numpad.addEventListener('click', (e) => {
|
|
const btn = e.target.closest('button');
|
|
if (!btn) return;
|
|
if (btn.querySelector('[data-icon="backspace"]')) {
|
|
applyInput('backspace');
|
|
} else {
|
|
applyInput(btn.textContent.trim());
|
|
}
|
|
});
|
|
|
|
// Teclado físico (desktop)
|
|
document.addEventListener('keydown', (e) => {
|
|
if (e.key >= '0' && e.key <= '9') applyInput(e.key);
|
|
else if (e.key === '.' || e.key === ',') applyInput('.');
|
|
else if (e.key === 'Backspace') applyInput('backspace');
|
|
else if (e.key === 'Enter') btnSave.click();
|
|
});
|
|
|
|
// Guardar Movimiento
|
|
btnSave.addEventListener('click', async () => {
|
|
const payload = {
|
|
tipo: selectedTipo,
|
|
monto: parseFloat(currentMonto),
|
|
fecha: document.getElementById('ui-fecha').value,
|
|
notas: document.getElementById('ui-notas').value,
|
|
id_categoria: document.getElementById('ui-categoria').value
|
|
};
|
|
|
|
if (isNaN(payload.monto) || payload.monto <= 0) {
|
|
rmModal.alert('Ingresá un monto mayor a 0.', 'Monto inválido');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const btnOriginal = btnSave.innerHTML;
|
|
btnSave.innerHTML = 'Guardando...';
|
|
btnSave.disabled = true;
|
|
|
|
const r = await fetch('api/movimientos.php', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload)
|
|
});
|
|
const data = await r.json();
|
|
|
|
if (data.status === 'success') {
|
|
window.location.href = 'index.html';
|
|
} else {
|
|
await rmModal.alert(data.message, 'Error al guardar');
|
|
btnSave.innerHTML = btnOriginal;
|
|
btnSave.disabled = false;
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
await rmModal.alert('No se pudo conectar con el servidor.', 'Error de conexión');
|
|
btnSave.disabled = false;
|
|
}
|
|
});
|
|
});
|