Actualizar configuracion.html

This commit is contained in:
2026-03-08 19:54:11 +00:00
parent 8c9862ad8c
commit 098f1baec2

View File

@@ -318,6 +318,149 @@
</div> </div>
</div> </div>
<div class="border-t border-gray-200 pt-10 mt-10">
<div class="mb-6">
<h2 class="text-2xl font-bold text-gray-800 flex items-center gap-2"><i data-lucide="bot" class="text-red-600"></i> Robot HomeServe: Reglas Automáticas</h2>
<p class="text-gray-500 text-sm mt-1">Configura qué debe escribir el robot en la web de HomeServe cuando cambias un estado en IntegraRepara.</p>
</div>
<div class="space-y-6">
<div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
<div class="p-4 bg-slate-50 border-b border-slate-200 flex justify-between items-center">
<div class="flex items-center gap-3">
<div class="bg-orange-100 text-orange-600 p-2 rounded-lg"><i data-lucide="user-check" class="w-5 h-5"></i></div>
<div>
<h3 class="font-bold text-slate-800">1. Al Asignar a un Operario</h3>
<p class="text-xs text-slate-500">Se activa cuando le pasas un siniestro nuevo a un técnico.</p>
</div>
</div>
<div class="relative inline-block w-12 align-middle select-none">
<input type="checkbox" id="hs_cfg_assign_enabled" onchange="toggleHsConfig('assign')" class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
<label for="hs_cfg_assign_enabled" class="toggle-label block overflow-hidden h-6 rounded-full bg-slate-200 cursor-pointer"></label>
</div>
</div>
<div id="hs_cfg_assign_body" class="p-5 space-y-4 hidden bg-white">
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Estado en HomeServe</label>
<select id="hs_cfg_assign_status" class="w-full border border-slate-300 rounded-lg p-2.5 text-sm outline-none focus:border-red-500">
<option value="ESPERA">Pendiente asignar / Espera (303)</option>
<option value="PTE_CONCERTAR">Pte. Concertar</option>
<option value="CITADO">Concertado / Citado (307)</option>
</select>
</div>
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Días Siguiente Acción</label>
<input type="number" id="hs_cfg_assign_days" value="1" min="0" class="w-full border border-slate-300 rounded-lg p-2.5 text-sm outline-none focus:border-red-500" placeholder="Ej: 1">
<p class="text-[10px] text-slate-400 mt-1">Se saltará fines de semana.</p>
</div>
<div class="flex items-center pt-5">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="hs_cfg_assign_inform" class="w-4 h-4 text-red-600 border-slate-300 rounded focus:ring-red-500">
<span class="text-sm font-bold text-slate-700">Marcar "Informado al Cliente"</span>
</label>
</div>
</div>
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Texto para "Observaciones" (Soporta variables)</label>
<textarea id="hs_cfg_assign_obs" rows="2" class="w-full border border-slate-300 rounded-lg p-3 text-sm outline-none focus:border-red-500" placeholder="Ej: Aviso asignado al operario {{NOMBRE_OPERARIO}} el {{FECHA}}."></textarea>
<p class="text-[10px] text-slate-400 mt-1">Variables: {{NOMBRE}}, {{FECHA}}, {{HORA}}, {{TELEFONO}}, {{REFERENCIA}}, {{NOMBRE_OPERARIO}}</p>
</div>
</div>
</div>
<div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
<div class="p-4 bg-slate-50 border-b border-slate-200 flex justify-between items-center">
<div class="flex items-center gap-3">
<div class="bg-emerald-100 text-emerald-600 p-2 rounded-lg"><i data-lucide="calendar-check" class="w-5 h-5"></i></div>
<div>
<h3 class="font-bold text-slate-800">2. Al Confirmar Cita</h3>
<p class="text-xs text-slate-500">Se activa cuando se fija fecha y hora con el cliente.</p>
</div>
</div>
<div class="relative inline-block w-12 align-middle select-none">
<input type="checkbox" id="hs_cfg_date_enabled" onchange="toggleHsConfig('date')" class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
<label for="hs_cfg_date_enabled" class="toggle-label block overflow-hidden h-6 rounded-full bg-slate-200 cursor-pointer"></label>
</div>
</div>
<div id="hs_cfg_date_body" class="p-5 space-y-4 hidden bg-white">
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Estado en HomeServe</label>
<select id="hs_cfg_date_status" class="w-full border border-slate-300 rounded-lg p-2.5 text-sm outline-none focus:border-red-500">
<option value="CITADO">Concertado / Citado (307)</option>
<option value="EN_CURSO">En curso / Trabajando</option>
</select>
</div>
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Días Siguiente Acción</label>
<input type="number" id="hs_cfg_date_days" value="0" min="0" class="w-full border border-slate-300 rounded-lg p-2.5 text-sm outline-none focus:border-red-500" title="0 significa que usará la misma fecha de la cita como Siguiente Acción.">
<p class="text-[10px] text-slate-400 mt-1">Si pones 0, usará el día de la cita.</p>
</div>
<div class="flex items-center pt-5">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="hs_cfg_date_inform" checked class="w-4 h-4 text-red-600 border-slate-300 rounded focus:ring-red-500">
<span class="text-sm font-bold text-slate-700">Marcar "Informado al Cliente"</span>
</label>
</div>
</div>
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Texto para "Observaciones"</label>
<textarea id="hs_cfg_date_obs" rows="2" class="w-full border border-slate-300 rounded-lg p-3 text-sm outline-none focus:border-red-500" placeholder="Ej: Cita confirmada con el cliente para el {{FECHA}} a las {{HORA}}."></textarea>
</div>
</div>
</div>
<div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
<div class="p-4 bg-slate-50 border-b border-slate-200 flex justify-between items-center">
<div class="flex items-center gap-3">
<div class="bg-red-100 text-red-600 p-2 rounded-lg"><i data-lucide="phone-off" class="w-5 h-5"></i></div>
<div>
<h3 class="font-bold text-slate-800">3. Cliente No Localizado</h3>
<p class="text-xs text-slate-500">Se activa cuando pulsas el botón de "No contesta" en el buzón.</p>
</div>
</div>
<div class="relative inline-block w-12 align-middle select-none">
<input type="checkbox" id="hs_cfg_notfound_enabled" onchange="toggleHsConfig('notfound')" class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
<label for="hs_cfg_notfound_enabled" class="toggle-label block overflow-hidden h-6 rounded-full bg-slate-200 cursor-pointer"></label>
</div>
</div>
<div id="hs_cfg_notfound_body" class="p-5 space-y-4 hidden bg-white">
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Estado en HomeServe</label>
<select id="hs_cfg_notfound_status" class="w-full border border-slate-300 rounded-lg p-2.5 text-sm outline-none focus:border-red-500">
<option value="PTE_CLIENTE">Pte. Cliente (No contesta)</option>
<option value="ESPERA">Espera (303)</option>
</select>
</div>
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Días Siguiente Acción</label>
<input type="number" id="hs_cfg_notfound_days" value="1" min="0" class="w-full border border-slate-300 rounded-lg p-2.5 text-sm outline-none focus:border-red-500">
<p class="text-[10px] text-slate-400 mt-1">Sumará estos días a la fecha actual.</p>
</div>
<div class="flex items-center pt-5">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="hs_cfg_notfound_inform" class="w-4 h-4 text-red-600 border-slate-300 rounded focus:ring-red-500">
<span class="text-sm font-bold text-slate-700">Marcar "Informado al Cliente"</span>
</label>
</div>
</div>
<div>
<label class="block text-xs font-bold text-slate-600 mb-1">Texto para "Observaciones"</label>
<textarea id="hs_cfg_notfound_obs" rows="2" class="w-full border border-slate-300 rounded-lg p-3 text-sm outline-none focus:border-red-500" placeholder="Ej: Intento de llamada al {{TELEFONO}} el {{FECHA}} a las {{HORA}}. No contesta. Se le envía WhatsApp con enlace web."></textarea>
</div>
</div>
</div>
<div class="flex justify-end mt-4">
<button onclick="saveRobotConfig()" class="bg-red-600 hover:bg-red-700 text-white px-6 py-3 rounded-xl text-sm font-bold shadow-md transition-all flex items-center gap-2">
<i data-lucide="save" class="w-4 h-4"></i> Guardar Reglas del Robot
</button>
</div>
</div>
</div>
<div class="border-t border-gray-200 pt-10"> <div class="border-t border-gray-200 pt-10">
<div class="mb-6 flex justify-between items-end"> <div class="mb-6 flex justify-between items-end">
<div> <div>
@@ -1012,6 +1155,107 @@
m.innerText = msg; t.classList.remove('translate-y-20', 'opacity-0'); m.innerText = msg; t.classList.remove('translate-y-20', 'opacity-0');
setTimeout(() => t.classList.add('translate-y-20', 'opacity-0'), 3000); 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);
}
}
</script> </script>
</body> </body>
</html> </html>