import { initThemePicker, initHighContrastToggle, initThemeToggle, applyTheme, getTheme } from "/static/js/wesp-theme.js"; import { DEFAULT_PASSWORD_MIN_LEN, validateCredentialChange, } from "/static/js/wesp-user-credentials.js"; export function createRecipesAuthSettingsController({ notyf }) { let settingsModalClickHandler = null; let settingsEscapeHandler = null; let syncDispenserNames = []; let passwordMinLen = DEFAULT_PASSWORD_MIN_LEN; function escapeHtmlSyncSettings(value) { if (value == null) return ""; const div = document.createElement("div"); div.textContent = String(value); return div.innerHTML; } function closeSettings() { const modal = document.getElementById("settingsModal"); if (!modal) return; modal.style.display = "none"; document.body.style.overflow = ""; const form = document.getElementById("settingsForm"); if (form) form.reset(); const credForm = document.getElementById("credentialsForm"); if (credForm) credForm.style.display = "none"; const userCard = document.querySelector(".settings-user-card--clickable"); if (userCard) userCard.classList.remove("open"); const syncForm = document.getElementById("syncSettingsForm"); if (syncForm) syncForm.style.display = "none"; const syncCard = document.querySelector(".settings-sync-card--clickable"); if (syncCard) syncCard.classList.remove("open"); if (settingsModalClickHandler) { modal.removeEventListener("click", settingsModalClickHandler); settingsModalClickHandler = null; } if (settingsEscapeHandler) { document.removeEventListener("keydown", settingsEscapeHandler); settingsEscapeHandler = null; } } async function openSettings(event) { if (event) event.preventDefault(); try { const response = await fetch("/api/auth/get_current_credentials"); const data = await response.json(); if (data.status === "success") { const oldLogin = document.getElementById("oldLogin"); const oldPassword = document.getElementById("oldPassword"); if (oldLogin) oldLogin.value = data.login || ""; if (oldPassword) oldPassword.value = ""; } } catch (error) { console.error("Ошибка загрузки текущих учетных данных:", error); } const modal = document.getElementById("settingsModal"); if (!modal) return; modal.style.display = "block"; document.body.style.overflow = "hidden"; initThemePicker(); initHighContrastToggle(); if (settingsModalClickHandler) { modal.removeEventListener("click", settingsModalClickHandler); } if (settingsEscapeHandler) { document.removeEventListener("keydown", settingsEscapeHandler); } settingsModalClickHandler = (e) => { if (e.target === modal) closeSettings(); }; settingsEscapeHandler = (e) => { if (e.key === "Escape" && modal.style.display === "block") closeSettings(); }; modal.addEventListener("click", settingsModalClickHandler); document.addEventListener("keydown", settingsEscapeHandler); } async function checkAuth() { try { const response = await fetch("/api/auth/check"); const data = await response.json(); if (data.status === "success" && data.authenticated) { const userInfo = document.getElementById("userInfo"); const userLogin = document.getElementById("userLogin"); const mobileUserInfo = document.getElementById("mobileUserInfo"); const canOpenAdmin = Boolean(data.is_superuser); window.WESP_CAN_LAB = Boolean(data.can_lab); window.WespLabAccess?.apply?.(window.WESP_CAN_LAB); const rules = data.user_rules || {}; passwordMinLen = Number(rules.password_min_len) || DEFAULT_PASSWORD_MIN_LEN; if (userLogin) userLogin.textContent = data.user_login; if (userInfo) { userInfo.style.cursor = canOpenAdmin ? "pointer" : ""; userInfo.title = canOpenAdmin ? "Открыть админ-панель" : ""; userInfo.onclick = canOpenAdmin ? () => { window.location.href = "/admin"; } : null; } if (mobileUserInfo) { mobileUserInfo.innerHTML = ` ${data.user_login}`; mobileUserInfo.style.cursor = canOpenAdmin ? "pointer" : ""; mobileUserInfo.title = canOpenAdmin ? "Открыть админ-панель" : ""; mobileUserInfo.onclick = canOpenAdmin ? () => { window.location.href = "/admin"; } : null; } } else { window.location.href = "/login"; } return data; } catch (error) { console.error("Ошибка проверки авторизации:", error); window.location.href = "/login"; return null; } } async function logout() { try { const response = await fetch("/api/auth/logout", { method: "POST", headers: { "Content-Type": "application/json" }, }); const data = await response.json(); if (data.status === "success") { window.location.href = "/"; } else { notyf.error("Ошибка при выходе из системы"); } } catch (error) { console.error("Ошибка при выходе:", error); notyf.error("Ошибка при выходе из системы"); } } function setupMobileMenu() { const hamburgerMenu = document.getElementById("hamburgerMenu"); const mobileMenuOverlay = document.getElementById("mobileMenuOverlay"); const closeMenu = document.getElementById("closeMenu"); const mobileUserInfo = document.getElementById("mobileUserInfo"); if (!hamburgerMenu || !mobileMenuOverlay || !closeMenu) return; hamburgerMenu.addEventListener("click", () => { mobileMenuOverlay.classList.add("active"); hamburgerMenu.classList.add("active"); document.body.style.overflow = "hidden"; const userInfo = document.getElementById("userInfo"); if (userInfo && mobileUserInfo) { mobileUserInfo.textContent = userInfo.textContent || ""; } }); const closeMobileMenu = () => { mobileMenuOverlay.classList.remove("active"); hamburgerMenu.classList.remove("active"); document.body.style.overflow = ""; }; closeMenu.addEventListener("click", closeMobileMenu); mobileMenuOverlay.addEventListener("click", (e) => { if (e.target === mobileMenuOverlay) closeMobileMenu(); }); document.addEventListener("keydown", (e) => { if (e.key === "Escape" && mobileMenuOverlay.classList.contains("active")) { closeMobileMenu(); } }); const mobileMenuItems = document.querySelectorAll(".mobile-menu-item"); mobileMenuItems.forEach((item) => { item.addEventListener("click", () => { setTimeout(closeMobileMenu, 300); }); }); } async function loadSyncClientsSettings() { const listEl = document.getElementById("syncClientsListSettings"); const emptyEl = document.getElementById("syncClientsEmptySettings"); if (!listEl) return; listEl.innerHTML = ""; if (emptyEl) emptyEl.style.display = "none"; try { const [clientsRes, namesRes] = await Promise.all([ fetch("/api/sync/clients"), fetch("/api/feed_dispensers/names"), ]); const clients = clientsRes.ok ? await clientsRes.json() : []; syncDispenserNames = namesRes.ok ? await namesRes.json() : []; if (!clients || clients.length === 0) { if (emptyEl) { emptyEl.style.display = "block"; emptyEl.textContent = "Нет подключенных клиентов"; } return; } clients.forEach((client) => { const displayLabel = client.display_name || client.client_name || client.node_id; const item = document.createElement("div"); item.className = "settings-sync-client-item"; item.dataset.nodeId = client.node_id; item.dataset.displayName = displayLabel || ""; item.innerHTML = `${escapeHtmlSyncSettings(displayLabel)}` + '