diff --git a/configuracion.html b/configuracion.html index 4dc27bc..2c93b36 100644 --- a/configuracion.html +++ b/configuracion.html @@ -1268,25 +1268,51 @@ } catch(e) { console.error("Error creds"); } } - // ========================================== + // ========================================== // LÓGICA PORTAL CLIENTE // ========================================== async function loadPortalConfig() { try { + // 1. Cargamos TODOS tus gremios de la base de datos + const resGuilds = await fetch(`${API_URL}/guilds`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); + const dataGuilds = await resGuilds.json(); + let allGuilds = dataGuilds.ok ? dataGuilds.guilds : []; + + // 2. Cargamos la configuración del portal const res = await fetch(`${API_URL}/config/company`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); const data = await res.json(); + if (data.ok && data.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 = ``; } - 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; + + 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; + + // 3. Pintamos los checkboxes de los gremios + const visibleGuilds = s.visible_guilds || []; + const container = document.getElementById('portalGuildsList'); + container.innerHTML = ''; // Limpiar el "Cargando..." + + if(allGuilds.length === 0) { + container.innerHTML = '

No tienes gremios creados aún.

'; + } else { + allGuilds.forEach(g => { + // Si el ID de este gremio está guardado, lo marcamos + const isChecked = visibleGuilds.includes(g.id) ? 'checked' : ''; + container.innerHTML += ` + + `; + }); } } } catch (e) { console.error("Error portal"); } @@ -1307,12 +1333,19 @@ const btn = document.getElementById('btnSavePortal'), original = btn.innerHTML; btn.innerHTML = ' Guardando...'; lucide.createIcons(); + + // 🛠️ Recogemos todos los checkboxes que estén marcados + const selectedGuilds = Array.from(document.querySelectorAll('.portal-guild-cb:checked')).map(cb => parseInt(cb.value)); + const payload = { company_name: document.getElementById('confCompanyName').value, company_logo: document.getElementById('confLogoBase64').value, portal_settings: { - m_start: document.getElementById('mStart').value, m_end: document.getElementById('mEnd').value, - a_start: document.getElementById('aStart').value, a_end: document.getElementById('aEnd').value + m_start: document.getElementById('mStart').value, + m_end: document.getElementById('mEnd').value, + a_start: document.getElementById('aStart').value, + a_end: document.getElementById('aEnd').value, + visible_guilds: selectedGuilds // <--- Guardamos los IDs } }; try {