// ========================================================
// SISTEMA GLOBAL DE MODALES Y DIÁLOGOS DE SIBOPAY
// Reemplaza window.alert y window.confirm por modales integrados de la página
// ========================================================
(function() {
function ensureDialogDom() {
let backdrop = document.getElementById('appGlobalDialogBackdrop');
if (!backdrop) {
backdrop = document.createElement('div');
backdrop.id = 'appGlobalDialogBackdrop';
backdrop.className = 'app-dialog-backdrop';
backdrop.innerHTML = `
`;
document.body.appendChild(modal);
} else if (modal.parentElement !== document.body) {
document.body.appendChild(modal);
}
return modal;
}
window.abrirModalRechazoSinpe = function({ id, estudiante_nombre, monto_colones, comprobante, onConfirm }) {
currentSinpeRejectData = {
id,
estudiante_nombre: estudiante_nombre || 'Estudiante',
monto_colones: Number(monto_colones || 0),
comprobante: comprobante || 'N/A',
onConfirm
};
const modal = ensureSinpeRejectModalDom();
if (!modal) return;
const estEl = document.getElementById('rechazoSinpeEstudiante');
const montoEl = document.getElementById('rechazoSinpeMonto');
const compEl = document.getElementById('rechazoSinpeComprobante');
const realInput = document.getElementById('sinpeRejectMontoReal');
const notasInput = document.getElementById('sinpeRejectNotasExtra');
if (estEl) estEl.textContent = currentSinpeRejectData.estudiante_nombre;
if (montoEl) montoEl.textContent = `₡${currentSinpeRejectData.monto_colones.toLocaleString('es-CR')}`;
if (compEl) compEl.textContent = currentSinpeRejectData.comprobante;
if (realInput) realInput.value = '';
if (notasInput) notasInput.value = '';
const firstRadio = document.querySelector('input[name="sinpeRejectReason"][value="fondos_no_recibidos"]');
if (firstRadio) firstRadio.checked = true;
window.toggleSinpeRejectInputs();
modal.style.display = 'flex';
};
window.cerrarModalRechazoSinpe = function(event) {
if (event && event.target && event.target !== document.getElementById('modalRechazarSinpe')) {
return;
}
const modal = document.getElementById('modalRechazarSinpe');
if (modal) modal.style.display = 'none';
currentSinpeRejectData = null;
};
window.toggleSinpeRejectInputs = function() {
const selected = document.querySelector('input[name="sinpeRejectReason"]:checked');
const container = document.getElementById('sinpeRejectMontoContainer');
if (!container || !selected) return;
if (selected.value === 'monto_no_coincide') {
container.style.display = 'block';
window.updateSinpeRejectPreview();
const input = document.getElementById('sinpeRejectMontoReal');
if (input) setTimeout(() => input.focus(), 50);
} else {
container.style.display = 'none';
}
};
window.updateSinpeRejectPreview = function() {
const preview = document.getElementById('sinpeRejectMontoPreview');
const inputReal = document.getElementById('sinpeRejectMontoReal');
if (!currentSinpeRejectData || !preview || !inputReal) return;
const solicitado = currentSinpeRejectData.monto_colones || 0;
const real = Number(inputReal.value || 0);
preview.textContent = `Se intentaron recargar ₡${solicitado.toLocaleString('es-CR')} pero el SINPE fue de ₡${real.toLocaleString('es-CR')}`;
};
window.confirmarRechazoSinpeModal = async function() {
if (!currentSinpeRejectData) return;
const selected = document.querySelector('input[name="sinpeRejectReason"]:checked');
const val = selected ? selected.value : 'fondos_no_recibidos';
const notasExtra = (document.getElementById('sinpeRejectNotasExtra')?.value || '').trim();
const solicitado = currentSinpeRejectData.monto_colones || 0;
let motivoFinal = '';
if (val === 'fondos_no_recibidos') {
motivoFinal = 'Fondos no recibidos en cuenta bancaria';
if (notasExtra) motivoFinal += ` (${notasExtra})`;
} else if (val === 'monto_no_coincide') {
const inputReal = document.getElementById('sinpeRejectMontoReal');
const real = Number(inputReal?.value || 0);
if (!inputReal?.value || real <= 0) {
showAppAlert({
title: 'Monto requerido',
message: 'Por favor ingresa de cuánto era el monto que figuró en el SINPE recibido.',
type: 'warning'
});
inputReal?.focus();
return;
}
motivoFinal = `Monto no coincide: Se intentaron recargar ₡${solicitado.toLocaleString('es-CR')} pero el SINPE recibido fue de ₡${real.toLocaleString('es-CR')}`;
if (notasExtra) motivoFinal += ` - ${notasExtra}`;
} else if (val === 'comprobante_invalido') {
motivoFinal = 'Comprobante falso, duplicado o ilegible';
if (notasExtra) motivoFinal += ` (${notasExtra})`;
} else if (val === 'telefono_no_coincide') {
motivoFinal = 'Teléfono o destinatario no coincide con la cuenta de la soda';
if (notasExtra) motivoFinal += ` (${notasExtra})`;
} else if (val === 'otro') {
if (!notasExtra) {
showAppAlert({
title: 'Detalle requerido',
message: 'Por favor escribe el motivo específico del rechazo.',
type: 'warning'
});
document.getElementById('sinpeRejectNotasExtra')?.focus();
return;
}
motivoFinal = notasExtra;
}
const modal = document.getElementById('modalRechazarSinpe');
if (modal) modal.style.display = 'none';
if (typeof currentSinpeRejectData.onConfirm === 'function') {
const fn = currentSinpeRejectData.onConfirm;
currentSinpeRejectData = null;
await fn(motivoFinal);
}
};
})();