passesFrontend/static/scripts/users-table.js

260 lines
7.8 KiB
JavaScript
Raw Permalink Normal View History

2024-05-15 13:27:22 +00:00
const createTable = () => {
const table = document.getElementById("usersTable");
const legalsTable = document.getElementById("legalsTable");
const tbody = table.querySelector("tbody");
const legalsTbody = legalsTable.querySelector("tbody");
// Очищаем таблицу
tbody.innerHTML = "";
legalsTbody.innerHTML = "";
users.forEach((user) => {
const row = document.createElement("tr");
if (user.isblocked === "true" || user.isblocked === true) {
row.setAttribute("style", `background: rgba(255, 69, 58, 0.3)`);
}
row.setAttribute("onclick", `openUser(${user.id});`);
// Добавляем ячейки с данными
const id = document.createElement("td");
id.textContent = user.id;
row.appendChild(id);
const surname = document.createElement("td");
surname.innerHTML = user.surname;
2024-05-15 13:27:22 +00:00
row.appendChild(surname);
const name = document.createElement("td");
name.innerHTML = user.name;
2024-05-15 13:27:22 +00:00
row.appendChild(name);
const secondname = document.createElement("td");
secondname.innerHTML = user.secondname;
2024-05-15 13:27:22 +00:00
row.appendChild(secondname);
const role = document.createElement("td");
if (user.role) {
role.textContent = user.role;
} else {
role.textContent = "Гость";
}
row.appendChild(role);
const phone = document.createElement("td");
phone.innerHTML = user.phone;
2024-05-15 13:27:22 +00:00
row.appendChild(phone);
const email = document.createElement("td");
email.innerHTML = user.email;
2024-05-15 13:27:22 +00:00
row.appendChild(email);
const sub = document.createElement("td");
sub.textContent = user.sub;
row.appendChild(sub);
const department = document.createElement("td");
department.textContent = user.department;
row.appendChild(department);
tbody.appendChild(row);
});
legals.forEach((legal) => {
const legalsRow = document.createElement("tr");
legalsRow.setAttribute("onclick", `openLegal(${legal.id});`);
// Добавляем ячейки с данными
const id = document.createElement("td");
id.textContent = legal.id;
legalsRow.appendChild(id);
const name = document.createElement("td");
name.innerHTML = legal.name;
legalsRow.appendChild(name);
const inn = document.createElement("td");
inn.textContent = legal.inn;
legalsRow.appendChild(inn);
const ogrn = document.createElement("td");
ogrn.textContent = legal.ogrn;
legalsRow.appendChild(ogrn);
const address = document.createElement("td");
address.textContent = legal.address;
legalsRow.appendChild(address);
const realaddress = document.createElement("td");
realaddress.textContent = legal.realaddress;
legalsRow.appendChild(realaddress);
const email = document.createElement("td");
email.textContent = legal.email;
legalsRow.appendChild(email);
legalsTbody.appendChild(legalsRow);
});
};
document.addEventListener("DOMContentLoaded", function () {
createTable();
});
function getCookie(name) {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.startsWith(name + "=")) {
return cookie.substring(name.length + 1);
}
}
return null;
}
function requestUpdate() {
document.getElementById("usersTable").style.filter = "brightness(0.85)";
document.getElementById("legalsTable").style.filter = "brightness(0.85)";
const formData = new FormData();
formData.append("searchText", document.getElementById("table-search").value);
formData.append(
"page",
parseInt(document.getElementById("page-number").textContent)
);
const requestOptions = {
method: "POST",
headers: {
Authorization: getCookie("token"),
},
body: formData,
};
fetch(API_SERVER + "/users/getusers", requestOptions)
.then((response) => {
document.getElementById("usersTable").style.filter = "brightness(1)";
document.getElementById("legalsTable").style.filter = "brightness(1)";
if (response.ok) {
return response.json();
} else {
console.error("Ошибка при отправке POST запроса.");
return "Ошибка при отправке запроса.";
}
})
.then((data) => {
users.splice(0, users.length);
legals.splice(0, legals.length);
users.push(
...data.people.map((item) => ({
id: item.id,
name: item.Имя,
surname: item.Фамилия,
secondname: item.Отчество,
role: item.Должность,
sub: item.Субподряд,
department: item.Цех,
email: item.Email,
phone: item.Телефон,
isblocked: item.Черный_список,
}))
);
legals.push(
...data.legals.map((item) => ({
id: item.id,
name: item.Наименование,
inn: item.ИНН,
ogrn: item.ОГРН,
address: item.Юридический_адрес,
realaddress: item.Фактический_адрес,
email: item.Email,
}))
);
createTable();
peopleMax = Math.ceil(data.totalCountPeople / 15);
legalsMax = Math.ceil(data.totalCountLegal / 15);
if (peopleMax === 0) {
peopleMax = 1;
}
if (legalsMax === 0) {
legalsMax = 1;
}
var currentPage = parseInt(pageNumberInput.textContent, 10);
if (tableType === "People") {
if (currentPage > peopleMax) {
pageNumberInput.textContent = peopleMax;
requestUpdate();
}
} else {
if (currentPage > legalsMax) {
pageNumberInput.textContent = legalsMax;
requestUpdate();
}
}
})
.catch((error) => {
console.error("Ошибка при отправке запроса:", error);
});
}
// function closeForm() {
// var formContainer = $("#form-bg");
// var fform = $("#form");
// formContainer.removeClass("active");
// fform.removeClass("form-animation");
// $("body").css("overflow", "auto");
// }
// document.addEventListener("DOMContentLoaded", function () {
// const form = document.getElementById("add-form");
// form.addEventListener("submit", function (event) {
// event.preventDefault();
// const deleteConfirmation = document.getElementById("addInformation");
// const closeButton = document.getElementById("closeButton");
// const mark = document.getElementById("success-mark");
// closeForm();
// closeButton.style.display = "none";
// deleteConfirmation.style.display = "flex";
// mark.style.display = "none";
// const formData = new FormData(form);
// form.reset();
// fetch(API_SERVER + "/user", {
// method: "POST",
// headers: {
// Authorization: getCookie("token"),
// },
// body: formData,
// })
// .then((response) => response.json())
// .then((data) => {
// showMessage("Пользователь успешно добавлен", true);
// })
// .catch((error) => {
// showMessage("Не удалось добавить пользователя", false);
// console.error("Ошибка:", error);
// });
// });
// });
// function addUser() {
// var formContainer = $("#form-bg");
// var form = $("#form");
// formContainer.addClass("active");
// form.addClass("form-animation");
// $("body").css("overflow", "hidden");
// }
// $(document).ready(function () {
// // Закрывает popup форму
// $("#close-form-btn").click(function () {
// closeForm();
// });
// // Функция для закрытия формы
// function closeForm() {
// var formContainer = $("#form-bg");
// var form = $("#form");
// formContainer.removeClass("active");
// form.removeClass("form-animation");
// $("body").css("overflow", "auto");
// }
// });