Files
asuinventory/frontend/app.js

77 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const api = {
auds: "/auditories/",
oboruds: (audId) => `/oboruds/?aud_id=${encodeURIComponent(audId)}`,
};
async function fetchJSON(url) {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
}
function setStatus(msg, type = "info") {
const el = document.getElementById("status");
el.textContent = msg || "";
el.className = `status ${type}`;
}
async function loadAuditories() {
setStatus("Загрузка аудиторий…");
try {
const data = await fetchJSON(api.auds);
const select = document.getElementById("aud-select");
select.innerHTML = '<option value="">— выберите аудиторию —</option>';
data.forEach((a) => {
const opt = document.createElement("option");
opt.value = a.id;
opt.textContent = `${a.id}${a.audnazvanie}`;
select.appendChild(opt);
});
setStatus("");
} catch (e) {
console.error(e);
setStatus("Не удалось загрузить аудитории", "error");
}
}
function renderOboruds(items) {
const tbody = document.querySelector("#ob-table tbody");
tbody.innerHTML = "";
items.forEach((it) => {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${it.id}</td>
<td>${it.invNumber ?? ""}</td>
<td>${it.nazvanie ?? ""}</td>
<td>${it.raspologenie ?? ""}</td>
<td>${it.kolichestvo ?? ""}</td>
<td>${it.type?.name ?? ""}</td>
`;
tbody.appendChild(tr);
});
}
async function loadOborudsForSelected() {
const select = document.getElementById("aud-select");
const audId = select.value;
if (!audId) {
setStatus("Выберите аудиторию", "warn");
return;
}
setStatus("Загрузка оборудования…");
try {
const data = await fetchJSON(api.oboruds(audId));
renderOboruds(data);
setStatus("");
} catch (e) {
console.error(e);
setStatus("Не удалось загрузить оборудование", "error");
}
}
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("load-btn").addEventListener("click", loadOborudsForSelected);
loadAuditories();
});