Actualizar configuracion.html
This commit is contained in:
@@ -346,7 +346,7 @@
|
|||||||
<script>
|
<script>
|
||||||
let cachedTemplates = {};
|
let cachedTemplates = {};
|
||||||
let currentTemplateType = null;
|
let currentTemplateType = null;
|
||||||
let localGuilds = []; // Guardará los gremios cargados para la IA
|
let localGuilds = [];
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
if (!localStorage.getItem("token")) window.location.href = "index.html";
|
if (!localStorage.getItem("token")) window.location.href = "index.html";
|
||||||
@@ -364,7 +364,6 @@
|
|||||||
lucide.createIcons();
|
lucide.createIcons();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. Cargamos todos los gremios del usuario
|
|
||||||
const resGuilds = await fetch(`${API_URL}/guilds`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
const resGuilds = await fetch(`${API_URL}/guilds`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||||
const dataGuilds = await resGuilds.json();
|
const dataGuilds = await resGuilds.json();
|
||||||
|
|
||||||
@@ -376,9 +375,7 @@
|
|||||||
localGuilds = dataGuilds.guilds;
|
localGuilds = dataGuilds.guilds;
|
||||||
list.innerHTML = '';
|
list.innerHTML = '';
|
||||||
|
|
||||||
// 2. Para cada gremio, mostramos su fila
|
|
||||||
localGuilds.forEach(g => {
|
localGuilds.forEach(g => {
|
||||||
// El array de keywords lo leemos del JSONB de la BD (si existe), si no, un array vacío.
|
|
||||||
const keywords = Array.isArray(g.ia_keywords) ? g.ia_keywords : [];
|
const keywords = Array.isArray(g.ia_keywords) ? g.ia_keywords : [];
|
||||||
|
|
||||||
const tagsHtml = keywords.length > 0
|
const tagsHtml = keywords.length > 0
|
||||||
@@ -428,13 +425,11 @@
|
|||||||
const guildId = document.getElementById('iaModalGuildId').value;
|
const guildId = document.getElementById('iaModalGuildId').value;
|
||||||
const rawKeywords = document.getElementById('iaModalKeywords').value;
|
const rawKeywords = document.getElementById('iaModalKeywords').value;
|
||||||
|
|
||||||
// Limpiamos y convertimos el texto en un array
|
|
||||||
const keywordsArray = rawKeywords.split(',')
|
const keywordsArray = rawKeywords.split(',')
|
||||||
.map(k => k.trim().toLowerCase())
|
.map(k => k.trim().toLowerCase())
|
||||||
.filter(k => k.length > 0);
|
.filter(k => k.length > 0);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Hacemos el PUT a la nueva ruta que vamos a crear en el servidor
|
|
||||||
const res = await fetch(`${API_URL}/guilds/${guildId}/ia-rules`, {
|
const res = await fetch(`${API_URL}/guilds/${guildId}/ia-rules`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||||
@@ -444,7 +439,7 @@
|
|||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
showToast("✅ Reglas de IA guardadas correctamente.");
|
showToast("✅ Reglas de IA guardadas correctamente.");
|
||||||
closeIaModal();
|
closeIaModal();
|
||||||
loadIaRules(); // Recargamos la lista
|
loadIaRules();
|
||||||
} else {
|
} else {
|
||||||
showToast("❌ Error al guardar las reglas.", true);
|
showToast("❌ Error al guardar las reglas.", true);
|
||||||
}
|
}
|
||||||
@@ -453,7 +448,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modificamos showTab para cargar la IA al entrar a Proveedores
|
|
||||||
function showTab(tabId) {
|
function showTab(tabId) {
|
||||||
document.querySelectorAll('.tab-content').forEach(el => el.classList.add('hidden'));
|
document.querySelectorAll('.tab-content').forEach(el => el.classList.add('hidden'));
|
||||||
document.getElementById(`view-${tabId}`).classList.remove('hidden');
|
document.getElementById(`view-${tabId}`).classList.remove('hidden');
|
||||||
@@ -470,7 +464,7 @@
|
|||||||
if(tabId === 'whatsapp') { checkWhatsappStatus(); }
|
if(tabId === 'whatsapp') { checkWhatsappStatus(); }
|
||||||
if(tabId === 'providers') {
|
if(tabId === 'providers') {
|
||||||
loadProviderCredentials();
|
loadProviderCredentials();
|
||||||
loadIaRules(); // <--- AÑADIDO AQUI
|
loadIaRules();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -651,6 +645,10 @@
|
|||||||
await fetch(`${API_URL}/companies/${id}`, { method: 'DELETE', headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
await fetch(`${API_URL}/companies/${id}`, { method: 'DELETE', headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||||
loadCompanies();
|
loadCompanies();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// CARGA DE ESTADOS Y GESTIÓN DE SEGURIDAD
|
||||||
|
// ==========================================
|
||||||
async function loadStatusesConfig() {
|
async function loadStatusesConfig() {
|
||||||
const list = document.getElementById('listStatuses');
|
const list = document.getElementById('listStatuses');
|
||||||
list.innerHTML = '<p class="text-center text-xs text-gray-400 mt-10">Cargando...</p>';
|
list.innerHTML = '<p class="text-center text-xs text-gray-400 mt-10">Cargando...</p>';
|
||||||
@@ -661,12 +659,25 @@
|
|||||||
data.statuses.forEach(s => {
|
data.statuses.forEach(s => {
|
||||||
const div = document.createElement('div');
|
const div = document.createElement('div');
|
||||||
div.className = "p-3 border border-gray-100 rounded-lg bg-gray-50 flex justify-between items-center group hover:border-blue-200 transition-colors";
|
div.className = "p-3 border border-gray-100 rounded-lg bg-gray-50 flex justify-between items-center group hover:border-blue-200 transition-colors";
|
||||||
div.innerHTML = `<div class="flex items-center gap-3"><div class="w-3 h-3 rounded-full bg-${s.color}-500"></div><span class="font-bold text-gray-700 text-sm">${s.name}</span></div><button onclick="deleteStatus(${s.id})" class="text-gray-300 hover:text-red-500"><i data-lucide="trash-2" class="w-4 h-4"></i></button>`;
|
|
||||||
|
// SI ES DEL SISTEMA, PONEMOS UN CANDADO. SI NO, LA PAPELERA.
|
||||||
|
const actionBtn = s.is_system
|
||||||
|
? `<i data-lucide="lock" class="w-4 h-4 text-slate-300" title="Estado del sistema (Imborrable)"></i>`
|
||||||
|
: `<button onclick="deleteStatus(${s.id})" class="text-gray-300 hover:text-red-500"><i data-lucide="trash-2" class="w-4 h-4"></i></button>`;
|
||||||
|
|
||||||
|
div.innerHTML = `
|
||||||
|
<div class="flex items-center gap-3">
|
||||||
|
<div class="w-3 h-3 rounded-full bg-${s.color}-500"></div>
|
||||||
|
<span class="font-bold text-gray-700 text-sm">${s.name}</span>
|
||||||
|
</div>
|
||||||
|
${actionBtn}
|
||||||
|
`;
|
||||||
list.appendChild(div);
|
list.appendChild(div);
|
||||||
});
|
});
|
||||||
lucide.createIcons();
|
lucide.createIcons();
|
||||||
} catch(e) { list.innerHTML = '<p class="text-red-500 text-xs text-center">Error</p>'; }
|
} catch(e) { list.innerHTML = '<p class="text-red-500 text-xs text-center">Error</p>'; }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addStatus() {
|
async function addStatus() {
|
||||||
const input = document.getElementById('newStatusInput');
|
const input = document.getElementById('newStatusInput');
|
||||||
const color = document.getElementById('newStatusColor').value;
|
const color = document.getElementById('newStatusColor').value;
|
||||||
@@ -677,13 +688,19 @@
|
|||||||
if(res.ok) { showToast("Añadido"); input.value = ""; loadStatusesConfig(); }
|
if(res.ok) { showToast("Añadido"); input.value = ""; loadStatusesConfig(); }
|
||||||
} catch(e) { showToast("Error", true); }
|
} catch(e) { showToast("Error", true); }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteStatus(id) {
|
async function deleteStatus(id) {
|
||||||
if(!confirm("¿Borrar?")) return;
|
if(!confirm("¿Borrar este estado? Asegúrate de que ningún servicio lo esté utilizando.")) return;
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_URL}/statuses/${id}`, { method: 'DELETE', headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
const res = await fetch(`${API_URL}/statuses/${id}`, { method: 'DELETE', headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
if(res.ok) { showToast("Eliminado"); loadStatusesConfig(); } else { showToast(json.error || "Error", true); }
|
if(res.ok) {
|
||||||
} catch(e) { showToast("Error", true); }
|
showToast("Estado eliminado");
|
||||||
|
loadStatusesConfig();
|
||||||
|
} else {
|
||||||
|
showToast(json.error || "Error al borrar", true);
|
||||||
|
}
|
||||||
|
} catch(e) { showToast("Error de conexión", true); }
|
||||||
}
|
}
|
||||||
|
|
||||||
function showToast(msg, isError = false) {
|
function showToast(msg, isError = false) {
|
||||||
|
|||||||
Reference in New Issue
Block a user