Actualizar configuracion.html
This commit is contained in:
@@ -472,21 +472,27 @@
|
||||
// RESTO DE FUNCIONES (INTACTAS)
|
||||
// ==========================================
|
||||
|
||||
function loadWaSettings() {
|
||||
const getCfg = (key, defaultVal) => {
|
||||
const val = localStorage.getItem(key);
|
||||
return val === null ? defaultVal : val === 'true';
|
||||
};
|
||||
|
||||
document.getElementById('cfg_delay').checked = getCfg('wa_delay_enabled', true);
|
||||
document.getElementById('cfg_evt_welcome').checked = getCfg('wa_evt_welcome', true);
|
||||
document.getElementById('cfg_evt_date').checked = getCfg('wa_evt_date', true);
|
||||
document.getElementById('cfg_evt_onway').checked = getCfg('wa_evt_onway', true);
|
||||
document.getElementById('cfg_evt_finished').checked = getCfg('wa_evt_finished', false);
|
||||
document.getElementById('cfg_evt_survey').checked = getCfg('wa_evt_survey', false);
|
||||
async function loadWaSettings() {
|
||||
try {
|
||||
// 1. Pedimos al servidor cómo están los botones (ON/OFF)
|
||||
const res = await fetch(`${API_URL}/whatsapp/settings`, {
|
||||
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
|
||||
});
|
||||
const data = await res.json();
|
||||
const s = data.settings || {};
|
||||
|
||||
// 2. Colocamos los interruptores según lo que diga la base de datos
|
||||
document.getElementById('cfg_delay').checked = s.wa_delay_enabled !== false; // Por defecto ON
|
||||
document.getElementById('cfg_evt_welcome').checked = s.wa_evt_welcome || false;
|
||||
document.getElementById('cfg_evt_date').checked = s.wa_evt_date || false;
|
||||
document.getElementById('cfg_evt_onway').checked = s.wa_evt_onway || false;
|
||||
document.getElementById('cfg_evt_finished').checked = s.wa_evt_finished || false;
|
||||
document.getElementById('cfg_evt_survey').checked = s.wa_evt_survey || false;
|
||||
} catch(e) { console.error("Error cargando ajustes WA"); }
|
||||
}
|
||||
|
||||
async function saveWaSettings() {
|
||||
// 1. Leemos cómo has dejado los botones en la pantalla
|
||||
const settings = {
|
||||
wa_delay_enabled: document.getElementById('cfg_delay').checked,
|
||||
wa_evt_welcome: document.getElementById('cfg_evt_welcome').checked,
|
||||
@@ -496,28 +502,17 @@
|
||||
wa_evt_survey: document.getElementById('cfg_evt_survey').checked
|
||||
};
|
||||
|
||||
for (const [key, value] of Object.entries(settings)) {
|
||||
localStorage.setItem(key, value);
|
||||
}
|
||||
showToast("Ajustes de WhatsApp guardados");
|
||||
}
|
||||
|
||||
async function loadProviderCredentials() {
|
||||
// 2. Se lo enviamos al servidor para que lo guarde en la base de datos
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/providers/credentials`, {
|
||||
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
|
||||
const res = await fetch(`${API_URL}/whatsapp/settings`, {
|
||||
method: 'POST',
|
||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||
body: JSON.stringify(settings)
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
if (data.ok && data.credentials) {
|
||||
data.credentials.forEach(cred => {
|
||||
const inputUser = document.getElementById(`user_${cred.provider}`);
|
||||
const inputPass = document.getElementById(`pass_${cred.provider}`);
|
||||
if(inputUser) inputUser.value = cred.username;
|
||||
if(inputPass) inputPass.placeholder = "•••••••• (Guardada)";
|
||||
});
|
||||
}
|
||||
} catch(e) { console.error("Error loading credentials", e); }
|
||||
if (res.ok) showToast("✅ Ajustes de WhatsApp guardados");
|
||||
else showToast("❌ Error al guardar", true);
|
||||
} catch(e) { showToast("❌ Error de conexión", true); }
|
||||
}
|
||||
|
||||
async function saveProviderCreds(event, provider) {
|
||||
|
||||
Reference in New Issue
Block a user