/* Reverse-proxy prefix, so /api/* still resolves on a subpath mount. */ const URL_BASE = document.body.dataset.urlBase || ""; function showToast(msg, isError) { const t = document.getElementById('toast'); t.textContent = msg; t.classList.toggle('error', !!isError); t.classList.add('show'); setTimeout(() => t.classList.remove('show'), 2800); } async function post(url, payload) { return fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(payload) }); } async function saveGeneral() { try { const resp = await post(URL_BASE + '/api/settings', { timezone: document.getElementById('timezone').value, arr_host: document.getElementById('arr_host').value.trim() }); showToast(resp.ok ? 'General settings saved — reload the dashboard to apply.' : 'Save failed.', !resp.ok); } catch (e) { showToast('Network error — not saved.', true); } } async function saveService(name) { try { const key = document.getElementById(name + '_api_key').value.trim(); const resp = await post(URL_BASE + '/api/settings', { [name + '_api_key']: key }); const label = name.charAt(0).toUpperCase() + name.slice(1); showToast(resp.ok ? label + ' key saved — restart the container to apply.' : 'Save failed.', !resp.ok); } catch (e) { showToast('Network error — not saved.', true); } } async function changePassword() { const errEl = document.getElementById('pw-error'); errEl.style.display = 'none'; try { const resp = await post(URL_BASE + '/api/change-password', { current: document.getElementById('pw_current').value, new: document.getElementById('pw_new').value, confirm: document.getElementById('pw_confirm').value }); const data = await resp.json(); if (resp.ok) { ['pw_current', 'pw_new', 'pw_confirm'].forEach(id => document.getElementById(id).value = ''); showToast('Password updated.'); } else { errEl.textContent = data.error || 'Failed to update password.'; errEl.style.display = 'block'; showToast(data.error || 'Failed to update password.', true); } } catch (e) { showToast('Network error — password not changed.', true); } }