Actualizar configuracion.html
This commit is contained in:
@@ -497,6 +497,7 @@
|
||||
<div class="p-6">
|
||||
<input type="hidden" id="iaModalGuildId">
|
||||
<label class="block text-sm font-bold text-gray-700 mb-2">Palabras Clave</label>
|
||||
<p class="text-xs text-gray-500 mb-3">Escribe palabras clave separadas por comas. Si el robot detecta alguna de ellas en la descripción del servicio, lo asignará a este gremio automáticamente.</p>
|
||||
<textarea id="iaModalKeywords" rows="5" class="w-full border border-gray-300 rounded-xl p-3 text-sm focus:ring-2 focus:ring-purple-500 outline-none" placeholder="Ej: fuga de agua, tubería rota, grifo, desatasco..."></textarea>
|
||||
</div>
|
||||
<div class="p-4 border-t bg-gray-50 flex justify-end gap-3">
|
||||
@@ -513,15 +514,73 @@
|
||||
let cachedTemplates = {};
|
||||
let currentTemplateType = null;
|
||||
let localGuilds = [];
|
||||
let currentCompanyConfig = {}; // <--- VARIABLE GLOBAL PARA ALMACENAR LA CONFIGURACIÓN
|
||||
let currentCompanyConfig = {}; // <--- ALMACENA TODA LA CONFIG GLOBAL
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
if (!localStorage.getItem("token")) window.location.href = "index.html";
|
||||
showTab('templates');
|
||||
loadTemplates();
|
||||
loadWaSettings();
|
||||
loadGlobalCompanyConfig(); // <--- CARGA GLOBAL PARA EVITAR ERRORES
|
||||
});
|
||||
|
||||
// ==========================================
|
||||
// CARGA GLOBAL DE DATOS (ANTI-ERROR 500)
|
||||
// ==========================================
|
||||
async function loadGlobalCompanyConfig() {
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/config/company`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||
const data = await res.json();
|
||||
if (data.ok && data.config) {
|
||||
currentCompanyConfig = data.config;
|
||||
|
||||
// Llenar datos Pestaña Portal
|
||||
document.getElementById('confCompanyName').value = data.config.full_name || "";
|
||||
if (data.config.company_logo) {
|
||||
document.getElementById('confLogoBase64').value = data.config.company_logo;
|
||||
document.getElementById('logoPreview').innerHTML = `<img src="${data.config.company_logo}" class="w-full h-full object-contain">`;
|
||||
}
|
||||
const ps = data.config.portal_settings || {};
|
||||
if(ps.m_start) document.getElementById('mStart').value = ps.m_start;
|
||||
if(ps.m_end) document.getElementById('mEnd').value = ps.m_end;
|
||||
if(ps.a_start) document.getElementById('aStart').value = ps.a_start;
|
||||
if(ps.a_end) document.getElementById('aEnd').value = ps.a_end;
|
||||
|
||||
// Llenar datos Pestaña App
|
||||
const apps = ps.app_settings || {};
|
||||
document.getElementById('color-primary').value = apps.primary || "#2563EB";
|
||||
document.getElementById('color-primary-hex').value = apps.primary || "#2563EB";
|
||||
document.getElementById('color-secondary').value = apps.secondary || "#F59E0B";
|
||||
document.getElementById('color-secondary-hex').value = apps.secondary || "#F59E0B";
|
||||
document.getElementById('color-bg').value = apps.bg || "#F4F7F9";
|
||||
document.getElementById('color-bg-hex').value = apps.bg || "#F4F7F9";
|
||||
updateAppPreview();
|
||||
}
|
||||
} catch(e) { console.error("Error config global"); }
|
||||
}
|
||||
|
||||
// UNIFICADOR DE PAYLOAD (Evita enviar datos vacíos a la BD y crashear PostgreSQL)
|
||||
function getCompanyPayload() {
|
||||
const portalSettings = currentCompanyConfig.portal_settings || {};
|
||||
|
||||
portalSettings.m_start = document.getElementById('mStart').value || "09:00";
|
||||
portalSettings.m_end = document.getElementById('mEnd').value || "14:00";
|
||||
portalSettings.a_start = document.getElementById('aStart').value || "16:00";
|
||||
portalSettings.a_end = document.getElementById('aEnd').value || "19:00";
|
||||
|
||||
portalSettings.app_settings = {
|
||||
primary: document.getElementById('color-primary').value || "#2563EB",
|
||||
secondary: document.getElementById('color-secondary').value || "#F59E0B",
|
||||
bg: document.getElementById('color-bg').value || "#F4F7F9"
|
||||
};
|
||||
|
||||
return {
|
||||
company_name: document.getElementById('confCompanyName').value || "",
|
||||
company_logo: document.getElementById('confLogoBase64').value || "",
|
||||
portal_settings: portalSettings
|
||||
};
|
||||
}
|
||||
|
||||
function showTab(tabId) {
|
||||
document.querySelectorAll('.tab-content').forEach(el => el.classList.add('hidden'));
|
||||
document.getElementById(`view-${tabId}`).classList.remove('hidden');
|
||||
@@ -538,8 +597,6 @@
|
||||
|
||||
if(tabId === 'others') { loadCompanies(); loadStatusesConfig(); }
|
||||
if(tabId === 'whatsapp') { checkWhatsappStatus(); }
|
||||
if(tabId === 'portal') { loadPortalConfig(); }
|
||||
if(tabId === 'app-operario') { loadAppConfig(); }
|
||||
if(tabId === 'providers') { loadProviderCredentials(); loadIaRules(); }
|
||||
}
|
||||
|
||||
@@ -571,47 +628,13 @@
|
||||
document.getElementById('app-preview').style.backgroundColor = b;
|
||||
}
|
||||
|
||||
async function loadAppConfig() {
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/config/company`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||
const data = await res.json();
|
||||
if (data.ok && data.config) {
|
||||
currentCompanyConfig = data.config; // Guardar toda la config
|
||||
|
||||
// Los colores los vamos a guardar dentro de portal_settings para evitar el error 500
|
||||
const s = (data.config.portal_settings && data.config.portal_settings.app_settings) ? data.config.portal_settings.app_settings : {};
|
||||
document.getElementById('color-primary').value = s.primary || "#2563EB";
|
||||
document.getElementById('color-primary-hex').value = s.primary || "#2563EB";
|
||||
document.getElementById('color-secondary').value = s.secondary || "#F59E0B";
|
||||
document.getElementById('color-secondary-hex').value = s.secondary || "#F59E0B";
|
||||
document.getElementById('color-bg').value = s.bg || "#F4F7F9";
|
||||
document.getElementById('color-bg-hex').value = s.bg || "#F4F7F9";
|
||||
updateAppPreview();
|
||||
}
|
||||
} catch (e) { console.error("Error cargando colores"); }
|
||||
}
|
||||
|
||||
async function saveAppConfig() {
|
||||
const btn = document.getElementById('btnSaveApp');
|
||||
const original = btn.innerHTML;
|
||||
btn.innerHTML = '<i data-lucide="loader-2" class="w-4 h-4 animate-spin"></i> Aplicando...';
|
||||
lucide.createIcons();
|
||||
|
||||
const appSettings = {
|
||||
primary: document.getElementById('color-primary').value,
|
||||
secondary: document.getElementById('color-secondary').value,
|
||||
bg: document.getElementById('color-bg').value
|
||||
};
|
||||
|
||||
// TRAMPA ANTI-ERROR 500: Metemos los colores DENTRO de portal_settings
|
||||
const portalSettings = currentCompanyConfig.portal_settings || {};
|
||||
portalSettings.app_settings = appSettings;
|
||||
|
||||
const payload = {
|
||||
company_name: currentCompanyConfig.full_name || null,
|
||||
company_logo: currentCompanyConfig.company_logo || null,
|
||||
portal_settings: portalSettings
|
||||
};
|
||||
const payload = getCompanyPayload();
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/config/company`, {
|
||||
@@ -619,9 +642,10 @@
|
||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
if (data.ok) {
|
||||
showToast("✅ Colores de la App actualizados");
|
||||
localStorage.setItem('app_theme', JSON.stringify(appSettings));
|
||||
localStorage.setItem('app_theme', JSON.stringify(payload.portal_settings.app_settings));
|
||||
} else {
|
||||
showToast("❌ Error al guardar", true);
|
||||
}
|
||||
@@ -632,36 +656,15 @@
|
||||
// ==========================================
|
||||
// LÓGICA PORTAL CLIENTE
|
||||
// ==========================================
|
||||
async function loadPortalConfig() {
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/config/company`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||
const data = await res.json();
|
||||
if (data.ok && data.config) {
|
||||
currentCompanyConfig = data.config; // Guardar toda la config
|
||||
|
||||
document.getElementById('confCompanyName').value = data.config.full_name || "";
|
||||
if (data.config.company_logo) {
|
||||
document.getElementById('confLogoBase64').value = data.config.company_logo;
|
||||
document.getElementById('logoPreview').innerHTML = `<img src="${data.config.company_logo}" class="w-full h-full object-contain">`;
|
||||
}
|
||||
if (data.config.portal_settings) {
|
||||
const s = data.config.portal_settings;
|
||||
if(s.m_start) document.getElementById('mStart').value = s.m_start;
|
||||
if(s.m_end) document.getElementById('mEnd').value = s.m_end;
|
||||
if(s.a_start) document.getElementById('aStart').value = s.a_start;
|
||||
if(s.a_end) document.getElementById('aEnd').value = s.a_end;
|
||||
}
|
||||
}
|
||||
} catch (e) { console.error("Error portal"); }
|
||||
}
|
||||
|
||||
function encodeLogo(input) {
|
||||
const file = input.files[0];
|
||||
if (!file || file.size > 1024 * 1024) { alert("Máximo 1MB"); return; }
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
document.getElementById('confLogoBase64').value = e.target.result;
|
||||
document.getElementById('logoPreview').innerHTML = `<img src="${e.target.result}" class="w-full h-full object-contain">`;
|
||||
};
|
||||
reader.readAsDataURL(input.files[0]);
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
|
||||
async function savePortalConfig() {
|
||||
@@ -670,18 +673,7 @@
|
||||
btn.innerHTML = '<i data-lucide="loader-2" class="w-4 h-4 animate-spin"></i> Guardando...';
|
||||
lucide.createIcons();
|
||||
|
||||
// Usamos la variable global para no borrar los colores
|
||||
const portalSettings = currentCompanyConfig.portal_settings || {};
|
||||
portalSettings.m_start = document.getElementById('mStart').value;
|
||||
portalSettings.m_end = document.getElementById('mEnd').value;
|
||||
portalSettings.a_start = document.getElementById('aStart').value;
|
||||
portalSettings.a_end = document.getElementById('aEnd').value;
|
||||
|
||||
const payload = {
|
||||
company_name: document.getElementById('confCompanyName').value,
|
||||
company_logo: document.getElementById('confLogoBase64').value,
|
||||
portal_settings: portalSettings
|
||||
};
|
||||
const payload = getCompanyPayload();
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/config/company`, {
|
||||
@@ -689,21 +681,18 @@
|
||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
if (data.ok) {
|
||||
showToast("✅ Configuración del Portal guardada");
|
||||
const nameEl = document.getElementById("headerUserName");
|
||||
if (nameEl && payload.company_name) nameEl.innerText = payload.company_name;
|
||||
} else {
|
||||
showToast("Error: " + data.error, true);
|
||||
showToast("❌ Error al guardar", true);
|
||||
}
|
||||
} catch (e) { showToast("Error de conexión", true); }
|
||||
finally { btn.innerHTML = original; lucide.createIcons(); }
|
||||
}
|
||||
|
||||
|
||||
// ==========================================
|
||||
// LÓGICA DE INTELIGENCIA ARTIFICIAL (GREMIOS)
|
||||
// ==========================================
|
||||
@@ -887,7 +876,6 @@
|
||||
|
||||
async function deleteStatus(id) { if(confirm("Borrar?")) { await fetch(`${API_URL}/statuses/${id}`, { method: 'DELETE', headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); loadStatusesConfig(); } }
|
||||
|
||||
|
||||
async function loadProviderCredentials() {
|
||||
const res = await fetch(`${API_URL}/providers/credentials`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||
const data = await res.json();
|
||||
|
||||
Reference in New Issue
Block a user