{% extends "layout.html" %} {% block title %}{{ title }}{% endblock %} {% block content %}
{% endblock %} {% block script %} const formState = { brands: {{ brands | tojson }}, types: {{ types | tojson }}, sections: {{ sections | tojson }}, functions: {{ functions | tojson }}, rooms: {{ rooms | tojson }}, }; function buildFormState() { function extractOptions(id) { const select = document.getElementById(`${id}-list`); return Array.from(select.options).map(opt => ({ name: opt.textContent.trim(), id: opt.value || undefined })); } function sanitizeFk(val) { return val && val !== "null" && val !== "" ? val : null; } const roomOptions = Array.from(document.getElementById('room-list').options); const rooms = roomOptions.map(opt => { const data = opt.dataset; return { id: opt.value || undefined, name: opt.textContent.trim(), section_id: sanitizeFk(data.sectionId), function_id: sanitizeFk(data.functionId), }; }); return { brands: extractOptions("brand"), types: extractOptions("type"), sections: extractOptions("section"), functions: extractOptions("function"), rooms }; } document.addEventListener('DOMContentLoaded', () => { const modal = document.getElementById('roomEditor'); const saveButton = document.getElementById('roomEditorSaveButton'); const cancelButton = document.getElementById('roomEditorCancelButton'); const form = document.getElementById('settingsForm'); modal.addEventListener('roomEditor:prepare', (event) => { const { id, name, sectionId, functionId } = event.detail; document.getElementById('roomId').value = id; document.getElementById('roomName').value = name; // Populate dropdowns before assigning selection const modalSections = document.getElementById('roomSection'); const modalFunctions = document.getElementById('roomFunction'); const pageSections = document.getElementById('section-list'); const pageFunctions = document.getElementById('function-list'); modalSections.innerHTML = ''; modalFunctions.innerHTML = ''; Array.from(pageSections.options).forEach(opt => { const option = new Option(opt.textContent, opt.value); if (opt.value === sectionId) { option.selected = true; } modalSections.appendChild(option); }); Array.from(pageFunctions.options).forEach(opt => { const option = new Option(opt.textContent, opt.value); if (opt.value === functionId) { option.selected = true; } modalFunctions.appendChild(option); }); }); form.addEventListener('submit', (event) => { event.preventDefault(); try { const state = buildFormState(); fetch('/api/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(state) }) .then(response => { if (!response.ok) { return response.json().then(data => { throw new Error(data.errors?.join("\n") || "Unknown error"); }); } return response.json(); }) .then(data => { console.log("Sync result:", data); }) .catch(err => { console.error("Submission error:", err); }); } catch (err) { console.error("Failed to build form state:", err); } }); saveButton.addEventListener('click', () => { const name = document.getElementById('roomName').value.trim(); const sectionVal = document.getElementById('roomSection').value; const funcVal = document.getElementById('roomFunction').value; let idRaw = document.getElementById('roomId').value; if (!name) { alert('Please enter a room name.'); return; } const roomList = document.getElementById('room-list'); let existingOption = Array.from(roomList.options).find(opt => opt.value === idRaw); // If it's a brand new ID, generate one (string-based!) if (!idRaw) { idRaw = ComboBoxWidget.createTempId("room"); } if (!existingOption) { existingOption = ComboBoxWidget.createOption(name, idRaw); roomList.appendChild(existingOption); } existingOption.textContent = name; existingOption.value = idRaw; existingOption.dataset.sectionId = sectionVal || ""; existingOption.dataset.functionId = funcVal || ""; ComboBoxWidget.sortOptions(roomList); // Update formState.rooms const index = formState.rooms.findIndex(r => r.id === idRaw); const payload = { id: idRaw, name, section_id: sectionVal !== "" ? sectionVal : null, function_id: funcVal !== "" ? funcVal : null }; if (index >= 0) { formState.rooms[index] = payload; } else { formState.rooms.push(payload); } bootstrap.Modal.getInstance(modal).hide(); }); cancelButton.addEventListener('click', () => { bootstrap.Modal.getInstance(modal).hide(); }); }); {% endblock %}