diff --git a/configuracion.html b/configuracion.html index f835a7e..a9ff6a0 100644 --- a/configuracion.html +++ b/configuracion.html @@ -318,6 +318,149 @@ +
+
+

Robot HomeServe: Reglas Automáticas

+

Configura qué debe escribir el robot en la web de HomeServe cuando cambias un estado en IntegraRepara.

+
+ +
+
+
+
+
+
+

1. Al Asignar a un Operario

+

Se activa cuando le pasas un siniestro nuevo a un técnico.

+
+
+
+ + +
+
+ +
+ +
+
+
+
+
+

2. Al Confirmar Cita

+

Se activa cuando se fija fecha y hora con el cliente.

+
+
+
+ + +
+
+ +
+ +
+
+
+
+
+

3. Cliente No Localizado

+

Se activa cuando pulsas el botón de "No contesta" en el buzón.

+
+
+
+ + +
+
+ +
+ +
+ +
+
+
+
@@ -1012,6 +1155,107 @@ m.innerText = msg; t.classList.remove('translate-y-20', 'opacity-0'); setTimeout(() => t.classList.add('translate-y-20', 'opacity-0'), 3000); } + + + // ========================================== + // LÓGICA REGLAS ROBOT HOMESERVE + // ========================================== + function toggleHsConfig(type) { + const isChecked = document.getElementById(`hs_cfg_${type}_enabled`).checked; + const body = document.getElementById(`hs_cfg_${type}_body`); + if (isChecked) body.classList.remove('hidden'); + else body.classList.add('hidden'); + } + + // Esta función la llamaremos dentro de loadProviderCredentials() o en el DOMContentLoaded + async function loadRobotConfig() { + try { + // Aprovechamos la ruta de settings que ya tienes + const res = await fetch(`${API_URL}/whatsapp/settings`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); + const data = await res.json(); + const s = data.settings || {}; + + const hsSettings = s.robot_homeserve || {}; + + // Cargar Asignación + if (hsSettings.assign) { + document.getElementById('hs_cfg_assign_enabled').checked = hsSettings.assign.enabled; + document.getElementById('hs_cfg_assign_status').value = hsSettings.assign.status || "ESPERA"; + document.getElementById('hs_cfg_assign_days').value = hsSettings.assign.days_next ?? 1; + document.getElementById('hs_cfg_assign_inform').checked = hsSettings.assign.check_inform || false; + document.getElementById('hs_cfg_assign_obs').value = hsSettings.assign.obs || ""; + toggleHsConfig('assign'); + } + + // Cargar Cita + if (hsSettings.date) { + document.getElementById('hs_cfg_date_enabled').checked = hsSettings.date.enabled; + document.getElementById('hs_cfg_date_status').value = hsSettings.date.status || "CITADO"; + document.getElementById('hs_cfg_date_days').value = hsSettings.date.days_next ?? 0; + document.getElementById('hs_cfg_date_inform').checked = hsSettings.date.check_inform || false; + document.getElementById('hs_cfg_date_obs').value = hsSettings.date.obs || ""; + toggleHsConfig('date'); + } + + // Cargar No Localizado + if (hsSettings.notfound) { + document.getElementById('hs_cfg_notfound_enabled').checked = hsSettings.notfound.enabled; + document.getElementById('hs_cfg_notfound_status').value = hsSettings.notfound.status || "PTE_CLIENTE"; + document.getElementById('hs_cfg_notfound_days').value = hsSettings.notfound.days_next ?? 1; + document.getElementById('hs_cfg_notfound_inform').checked = hsSettings.notfound.check_inform || false; + document.getElementById('hs_cfg_notfound_obs').value = hsSettings.notfound.obs || ""; + toggleHsConfig('notfound'); + } + } catch(e) { console.error("Error cargando ajustes del robot", e); } + } + + async function saveRobotConfig() { + // Obtenemos todos los settings actuales para no sobreescribir lo de WhatsApp + const resGet = await fetch(`${API_URL}/whatsapp/settings`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); + const dataGet = await resGet.json(); + const currentSettings = dataGet.settings || {}; + + // Construimos el nuevo objeto del robot + currentSettings.robot_homeserve = { + assign: { + enabled: document.getElementById('hs_cfg_assign_enabled').checked, + status: document.getElementById('hs_cfg_assign_status').value, + days_next: parseInt(document.getElementById('hs_cfg_assign_days').value) || 0, + check_inform: document.getElementById('hs_cfg_assign_inform').checked, + obs: document.getElementById('hs_cfg_assign_obs').value + }, + date: { + enabled: document.getElementById('hs_cfg_date_enabled').checked, + status: document.getElementById('hs_cfg_date_status').value, + days_next: parseInt(document.getElementById('hs_cfg_date_days').value) || 0, + check_inform: document.getElementById('hs_cfg_date_inform').checked, + obs: document.getElementById('hs_cfg_date_obs').value + }, + notfound: { + enabled: document.getElementById('hs_cfg_notfound_enabled').checked, + status: document.getElementById('hs_cfg_notfound_status').value, + days_next: parseInt(document.getElementById('hs_cfg_notfound_days').value) || 0, + check_inform: document.getElementById('hs_cfg_notfound_inform').checked, + obs: document.getElementById('hs_cfg_notfound_obs').value + } + }; + + try { + const res = await fetch(`${API_URL}/whatsapp/settings`, { + method: 'POST', + headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, + body: JSON.stringify(currentSettings) + }); + if (res.ok) { + showToast("✅ Reglas del Robot guardadas"); + } else { + showToast("❌ Error al guardar", true); + } + } catch(e) { + showToast("❌ Error de conexión", true); + } + } + \ No newline at end of file