This commit is contained in:
2026-02-26 10:09:24 +05:00
parent 908533929b
commit 9d28e67388
4 changed files with 305 additions and 0 deletions

View File

@@ -63,6 +63,7 @@ function showAdminInterface() {
function setupEventListeners() {
const loginForm = document.getElementById('login-form');
const editUserForm = document.getElementById('edit-user-form');
const createUserForm = document.getElementById('create-user-form');
if (loginForm) {
loginForm.addEventListener('submit', login);
@@ -71,6 +72,10 @@ function setupEventListeners() {
if (editUserForm) {
editUserForm.addEventListener('submit', updateUser);
}
if (createUserForm) {
createUserForm.addEventListener('submit', createUser);
}
}
async function login(event) {
@@ -270,6 +275,101 @@ function renderUsersTable() {
`).join('');
}
// Функция для открытия модального окна создания пользователя
function openCreateUserModal() {
const modal = document.getElementById('create-user-modal');
if (modal) {
// Сбрасываем форму
const form = document.getElementById('create-user-form');
if (form) {
form.reset();
}
modal.style.display = 'block';
}
}
// Функция для закрытия модального окна создания пользователя
function closeCreateUserModal() {
const modal = document.getElementById('create-user-modal');
if (modal) {
modal.style.display = 'none';
}
}
// Функция для создания нового пользователя
async function createUser(event) {
event.preventDefault();
const login = document.getElementById('create-login').value;
const password = document.getElementById('create-password').value;
const name = document.getElementById('create-name').value;
const email = document.getElementById('create-email').value;
const role = document.getElementById('create-role').value;
const auth_type = document.getElementById('create-auth-type').value;
const groups = document.getElementById('create-groups').value;
const description = document.getElementById('create-description').value;
// Валидация
if (!login || !password || !name || !email) {
alert('Заполните все обязательные поля');
return;
}
if (password.length < 6) {
alert('Пароль должен содержать не менее 6 символов');
return;
}
const userData = {
login,
password,
name,
email,
role,
auth_type,
groups: groups || '[]',
description
};
try {
const response = await fetch('/admin/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userData)
});
if (response.ok) {
const result = await response.json();
alert(`Пользователь успешно создан! ID: ${result.id}`);
closeCreateUserModal();
loadUsers(); // Перезагружаем список пользователей
// Обновляем статистику если она видна
if (document.getElementById('admin-dashboard')?.classList.contains('active')) {
if (typeof loadDashboardStats === 'function') {
loadDashboardStats();
}
}
if (document.getElementById('admin-stats-section')?.classList.contains('active')) {
if (typeof loadUsersStats === 'function') {
loadUsersStats();
}
if (typeof loadOverallStats === 'function') {
loadOverallStats();
}
}
} else {
const error = await response.json();
alert(error.error || 'Ошибка создания пользователя');
}
} catch (error) {
console.error('Ошибка:', error);
alert('Ошибка создания пользователя');
}
}
async function openEditUserModal(userId) {
try {
const response = await fetch(`/admin/users/${userId}`);
@@ -447,5 +547,8 @@ window.searchUsers = searchUsers;
window.loadUsers = loadUsers;
window.openEditUserModal = openEditUserModal;
window.closeEditUserModal = closeEditUserModal;
window.openCreateUserModal = openCreateUserModal;
window.closeCreateUserModal = closeCreateUserModal;
window.createUser = createUser;
window.updateUser = updateUser;
window.deleteUser = deleteUser;