Поправил модалку с расписанием препода

This commit is contained in:
ProstoDenya01
2026-03-15 15:51:29 +03:00
parent 8df736ae36
commit 05fcf86e32
5 changed files with 675 additions and 516 deletions

View File

@@ -7,11 +7,13 @@ const ROLE_BADGE = { ADMIN: 'badge-admin', TEACHER: 'badge-teacher', STUDENT: 'b
export async function initUsers() {
const usersTbody = document.getElementById('users-tbody');
const createForm = document.getElementById('create-form');
const modalBackdrop = document.getElementById('modal-backdrop');
// Элементы модального окна добавления занятия
// ===== 1-е модальное окно: Добавить занятие =====
const modalAddLesson = document.getElementById('modal-add-lesson');
const modalAddLessonClose = document.getElementById('modal-add-lesson-close');
const addLessonForm = document.getElementById('add-lesson-form');
const lessonGroupSelect = document.getElementById('lesson-group');
const lessonDisciplineSelect = document.getElementById('lesson-discipline');
const lessonClassroomSelect = document.getElementById('lesson-classroom');
@@ -22,15 +24,21 @@ export async function initUsers() {
const lessonDaySelect = document.getElementById('lesson-day');
const weekUpper = document.getElementById('week-upper');
const weekLower = document.getElementById('week-lower');
// NEW: получаем элемент выбора времени
const lessonTimeSelect = document.getElementById('lesson-time');
// Переменные для хранения загруженных данных
// ===== 2-е модальное окно: Просмотр занятий =====
const modalViewLessons = document.getElementById('modal-view-lessons');
const modalViewLessonsClose = document.getElementById('modal-view-lessons-close');
const lessonsContainer = document.getElementById('lessons-container');
const modalTeacherName = document.getElementById('modal-teacher-name');
let currentLessonsTeacherId = null;
let currentLessonsTeacherName = '';
// ===== Данные =====
let groups = [];
let subjects = [];
let classrooms = [];
// NEW: массивы с временными слотами
const weekdaysTimes = [
"8:00-9:30",
"9:40-11:10",
@@ -48,7 +56,39 @@ export async function initUsers() {
"13:20-14:50"
];
// Загрузка групп с сервера
// =========================================================
// СИНХРОНИЗАЦИЯ ВЫСОТЫ 1-й МОДАЛКИ -> CSS переменная
// =========================================================
const addLessonContent = document.querySelector('#modal-add-lesson .modal-content');
function setAddLessonHeightVar(px) {
const h = Math.max(0, Math.ceil(px || 0));
document.documentElement.style.setProperty('--add-lesson-height', `${h}px`);
}
function syncAddLessonHeight() {
if (!addLessonContent) return;
if (!modalAddLesson?.classList.contains('open')) {
// если первая модалка закрыта — "шапки" нет
setAddLessonHeightVar(0);
return;
}
setAddLessonHeightVar(addLessonContent.getBoundingClientRect().height);
}
// Авто-обновление при любом изменении размеров первой модалки
if (addLessonContent && 'ResizeObserver' in window) {
const ro = new ResizeObserver(() => syncAddLessonHeight());
ro.observe(addLessonContent);
}
window.addEventListener('resize', () => syncAddLessonHeight());
// =========================================================
// Загрузка справочников
// =========================================================
async function loadGroups() {
try {
groups = await api.get('/api/groups');
@@ -58,7 +98,6 @@ export async function initUsers() {
}
}
// Загрузка дисциплин
async function loadSubjects() {
try {
subjects = await api.get('/api/subjects');
@@ -69,39 +108,37 @@ export async function initUsers() {
}
async function loadClassrooms() {
try {
classrooms = await api.get('/api/classrooms');
renderClassroomsOptions();
} catch (e) {
console.error('Ошибка загрузки аудиторий:', e);
}
try {
classrooms = await api.get('/api/classrooms');
renderClassroomsOptions();
} catch (e) {
console.error('Ошибка загрузки аудиторий:', e);
}
}
// Заполнение select группами
function renderGroupOptions() {
if (!groups || groups.length === 0) {
lessonClassroomSelect.innerHTML = '<option value="">Нет доступных групп</option>';
lessonGroupSelect.innerHTML = '<option value="">Нет доступных групп</option>';
return;
}
lessonGroupSelect.innerHTML = '<option value="">Выберите группу</option>' +
lessonGroupSelect.innerHTML =
'<option value="">Выберите группу</option>' +
groups.map(g => {
let optionText = escapeHtml(g.name);
if(g.groupSize) {
optionText += ` (численность: ${g.groupSize} чел.)`;
}
return `<option value="${g.id}">${optionText}</option>`;
let optionText = escapeHtml(g.name);
if (g.groupSize) optionText += ` (численность: ${g.groupSize} чел.)`;
return `<option value="${g.id}">${optionText}</option>`;
}).join('');
}
// Заполнение select дисциплинами
function renderSubjectOptions() {
lessonDisciplineSelect.innerHTML = '<option value="">Выберите дисциплину</option>' +
lessonDisciplineSelect.innerHTML =
'<option value="">Выберите дисциплину</option>' +
subjects.map(s => `<option value="${s.id}">${escapeHtml(s.name)}</option>`).join('');
}
function renderClassroomsOptions() {
if (!classrooms || classrooms.length ===0) {
if (!classrooms || classrooms.length === 0) {
lessonClassroomSelect.innerHTML = '<option value="">Нет доступных аудиторий</option>';
return;
}
@@ -110,32 +147,28 @@ export async function initUsers() {
const selectedGroup = groups?.find(g => g.id == selectedGroupId);
const groupSize = selectedGroup?.groupSize || 0;
lessonClassroomSelect.innerHTML = '<option value="">Выберите аудиторию</option>' +
lessonClassroomSelect.innerHTML =
'<option value="">Выберите аудиторию</option>' +
classrooms.map(c => {
let optionText = escapeHtml(c.name);
// Добавление текста с инфой о вместимости чел.
if(c.capacity) {
optionText += ` (вместимость: ${c.capacity} чел.)`;
}
let optionText = escapeHtml(c.name);
// Если аудитория занята, то рисуем крестик допом
if (c.isAvailable === false) {
optionText += ` ❌ Занята`
// Если свободна, но меньше численности группы, отображаем воскл. знак
} else if (selectedGroupId && groupSize > 0 && c.capacity && groupSize > c.capacity) {
optionText += ` ⚠️ Недостаточно места`;
}
if (c.capacity) optionText += ` (вместимость: ${c.capacity} чел.)`;
if (c.isAvailable === false) {
optionText += ` ❌ Занята`;
} else if (selectedGroupId && groupSize > 0 && c.capacity && groupSize > c.capacity) {
optionText += ` ⚠️ Недостаточно места`;
}
return `<option value="${c.id}">${optionText}</option>`;
return `<option value="${c.id}">${optionText}</option>`;
}).join('');
}
lessonGroupSelect.addEventListener('change', function() {
lessonGroupSelect.addEventListener('change', function () {
renderClassroomsOptions();
requestAnimationFrame(() => syncAddLessonHeight());
});
// NEW: функция обновления списка времени в зависимости от дня
function updateTimeOptions(dayValue) {
let times = [];
if (dayValue === "Суббота") {
@@ -148,17 +181,23 @@ export async function initUsers() {
return;
}
lessonTimeSelect.innerHTML = '<option value="">Выберите время</option>' +
lessonTimeSelect.innerHTML =
'<option value="">Выберите время</option>' +
times.map(t => `<option value="${t}">${t}</option>`).join('');
lessonTimeSelect.disabled = false;
}
// =========================================================
// Пользователи
// =========================================================
async function loadUsers() {
try {
const users = await api.get('/api/users');
renderUsers(users);
} catch (e) {
usersTbody.innerHTML = '<tr><td colspan="4" class="loading-row">Ошибка загрузки: ' + escapeHtml(e.message) + '</td></tr>';
usersTbody.innerHTML =
'<tr><td colspan="4" class="loading-row">Ошибка загрузки: ' +
escapeHtml(e.message) + '</td></tr>';
}
}
@@ -167,45 +206,72 @@ export async function initUsers() {
usersTbody.innerHTML = '<tr><td colspan="4" class="loading-row">Нет пользователей</td></tr>';
return;
}
usersTbody.innerHTML = users.map(u => `
<tr>
<td>${u.id}</td>
<td>${escapeHtml(u.username)}</td>
<td><span class="badge ${ROLE_BADGE[u.role] || ''}">${ROLE_LABELS[u.role] || escapeHtml(u.role)}</span></td>
<td>
<button class="btn-delete" data-id="${u.id}">Удалить</button>
</td>
<td>
<button class="btn-add-lesson" data-id="${u.id}">Добавить занятие</button>
<button class="btn-view-lessons" data-id="${u.id}" data-name="${escapeHtml(u.username)}" style="margin-left: 0.5rem;">👁️ Занятия</button>
</td>
</tr>`).join('');
<tr>
<td>${u.id}</td>
<td>${escapeHtml(u.username)}</td>
<td><span class="badge ${ROLE_BADGE[u.role] || ''}">${ROLE_LABELS[u.role] || escapeHtml(u.role)}</span></td>
<td>
<button class="btn-delete" data-id="${u.id}">Удалить</button>
</td>
<td>
<button class="btn-add-lesson" data-id="${u.id}" data-name="${escapeHtml(u.username)}">Добавить занятие</button>
</td>
</tr>
`).join('');
}
// Сброс формы модального окна
function updateBackdrop() {
if(!modalBackdrop) return;
const anyOpen =
modalAddLesson?.classList.contains('open') ||
modalViewLessons?.classList.contains('open');
modalBackdrop.classList.toggle('open', anyOpen);
}
// Клик мимо модалок закроет их, если не надо, то закомментить этот код
modalBackdrop?.addEventListener('click', () => {
if (modalAddLesson?.classList.contains('open')) {
modalAddLesson.classList.remove('open');
resetLessonForm();
syncAddLessonHeight();
}
if (modalViewLessons?.classList.contains('open')) {
closeViewLessonsModal();
}
});
// =========================================================
// 1-я модалка: добавление занятия
// =========================================================
function resetLessonForm() {
addLessonForm.reset();
lessonUserId.value = '';
if (weekUpper) weekUpper.checked = false;
if (weekLower) weekLower.checked = false;
// NEW: сбрасываем селект времени
if (lessonOfflineFormat) lessonOfflineFormat.checked = true;
if (lessonOnlineFormat) lessonOnlineFormat.checked = false;
lessonTimeSelect.innerHTML = '<option value="">Сначала выберите день</option>';
lessonTimeSelect.disabled = true;
hideAlert('add-lesson-alert');
}
// Открытие модалки с установкой userId
function openAddLessonModal(userId) {
lessonUserId.value = userId;
// NEW: сбрасываем выбранный день и время
lessonDaySelect.value = '';
updateTimeOptions('');
modalAddLesson.classList.add('open');
updateBackdrop();
requestAnimationFrame(() => syncAddLessonHeight());
}
// Обработчик отправки формы добавления занятия
addLessonForm.addEventListener('submit', async (e) => {
e.preventDefault();
hideAlert('add-lesson-alert');
@@ -216,48 +282,26 @@ export async function initUsers() {
const classroomId = lessonClassroomSelect.value;
const lessonType = lessonTypeSelect.value;
const dayOfWeek = lessonDaySelect.value;
const timeSlot = lessonTimeSelect.value; // NEW: получаем выбранное время
const timeSlot = lessonTimeSelect.value;
const lessonFormat = document.querySelector('input[name="lessonFormat"]:checked')?.value;
// Проверка обязательных полей
if (!groupId) {
showAlert('add-lesson-alert', 'Выберите группу', 'error');
return;
}
if (!subjectId) {
showAlert('add-lesson-alert', 'Выберите дисциплину', 'error');
return;
}
if (!classroomId) {
showAlert('add-lesson-alert', 'Выберите аудиторию', 'error')
return;
}
if (!dayOfWeek) {
showAlert('add-lesson-alert', 'Выберите день недели', 'error');
return;
}
// NEW: проверка времени
if (!timeSlot) {
showAlert('add-lesson-alert', 'Выберите время', 'error');
return;
}
if (!groupId) { showAlert('add-lesson-alert', 'Выберите группу', 'error'); requestAnimationFrame(() => syncAddLessonHeight()); return; }
if (!subjectId) { showAlert('add-lesson-alert', 'Выберите дисциплину', 'error'); requestAnimationFrame(() => syncAddLessonHeight()); return; }
if (!classroomId) { showAlert('add-lesson-alert', 'Выберите аудиторию', 'error'); requestAnimationFrame(() => syncAddLessonHeight()); return; }
if (!dayOfWeek) { showAlert('add-lesson-alert', 'Выберите день недели', 'error'); requestAnimationFrame(() => syncAddLessonHeight()); return; }
if (!timeSlot) { showAlert('add-lesson-alert', 'Выберите время', 'error'); requestAnimationFrame(() => syncAddLessonHeight()); return; }
// Определяем выбранный тип недели
const weekUpperChecked = weekUpper?.checked || false;
const weekLowerChecked = weekLower?.checked || false;
let weekType = null;
if (weekUpperChecked && weekLowerChecked) {
weekType = 'Обе';
} else if (weekUpperChecked) {
weekType = 'Верхняя';
} else if (weekLowerChecked) {
weekType = 'Нижняя';
}
if (weekUpperChecked && weekLowerChecked) weekType = 'Обе';
else if (weekUpperChecked) weekType = 'Верхняя';
else if (weekLowerChecked) weekType = 'Нижняя';
try {
// Отправляем данные на сервер
const response = await api.post('/api/users/lessons/create', {
await api.post('/api/users/lessons/create', {
teacherId: parseInt(userId),
groupId: parseInt(groupId),
subjectId: parseInt(subjectId),
@@ -266,10 +310,15 @@ export async function initUsers() {
lessonFormat: lessonFormat,
day: dayOfWeek,
week: weekType,
time: timeSlot // передаём время
time: timeSlot
});
if (modalViewLessons?.classList.contains('open') && currentLessonsTeacherId == userId) {
await loadTeacherLessons(currentLessonsTeacherId, currentLessonsTeacherName);
}
showAlert('add-lesson-alert', 'Занятие добавлено', 'success');
// Очищаем только поля, но оставляем userId
lessonGroupSelect.value = '';
lessonDisciplineSelect.value = '';
lessonClassroomSelect.value = '';
@@ -277,106 +326,90 @@ export async function initUsers() {
lessonDaySelect.value = '';
lessonTimeSelect.value = '';
lessonTimeSelect.disabled = true;
weekUpper.checked = false;
weekLower.checked = false;
document.querySelector('input[name="lessonFormat"][value="Очно"]').checked = true;
// Убираем сообщение через 3 секунды, чтобы можно было добавлять дальше
requestAnimationFrame(() => syncAddLessonHeight());
setTimeout(() => {
hideAlert('add-lesson-alert');
syncAddLessonHeight();
}, 3000);
} catch (e) {
showAlert('add-lesson-alert', e.message || 'Ошибка добавления занятия', 'error');
} catch (err) {
showAlert('add-lesson-alert', err.message || 'Ошибка добавления занятия', 'error');
requestAnimationFrame(() => syncAddLessonHeight());
}
});
lessonDaySelect.addEventListener('change', function () {
updateTimeOptions(this.value);
requestAnimationFrame(() => syncAddLessonHeight());
});
if (modalAddLessonClose) {
modalAddLessonClose.addEventListener('click', () => {
modalAddLesson.classList.remove('open');
resetLessonForm();
syncAddLessonHeight();
updateBackdrop();
});
}
if (modalAddLesson) {
modalAddLesson.addEventListener('click', (e) => {
if (e.target === modalAddLesson) {
modalAddLesson.classList.remove('open');
resetLessonForm();
syncAddLessonHeight();
updateBackdrop();
}
});
}
// =========================================================
// Создание пользователя
// =========================================================
createForm.addEventListener('submit', async (e) => {
e.preventDefault();
hideAlert('create-alert');
const username = document.getElementById('new-username').value.trim();
const password = document.getElementById('new-password').value;
const role = document.getElementById('new-role').value;
if (!username || !password) { showAlert('create-alert', 'Заполните все поля', 'error'); return; }
if (!username || !password) {
showAlert('create-alert', 'Заполните все поля', 'error');
return;
}
try {
const data = await api.post('/api/users', { username, password, role });
showAlert('create-alert', `Пользователь "${escapeHtml(data.username)}" создан`, 'success');
createForm.reset();
loadUsers();
} catch (e) {
showAlert('create-alert', e.message || 'Ошибка соединения', 'error');
} catch (err) {
showAlert('create-alert', err.message || 'Ошибка соединения', 'error');
}
});
// Обработчик кликов по таблице
usersTbody.addEventListener('click', async (e) => {
const deleteBtn = e.target.closest('.btn-delete');
if (deleteBtn) {
if (!confirm('Удалить пользователя?')) return;
try {
await api.delete('/api/users/' + deleteBtn.dataset.id);
loadUsers();
} catch (e) {
alert(e.message || 'Ошибка удаления');
}
return;
}
const addLessonBtn = e.target.closest('.btn-add-lesson');
if (addLessonBtn) {
e.preventDefault();
if (modalAddLesson) {
openAddLessonModal(addLessonBtn.dataset.id);
}
}
});
// NEW: обработчик изменения дня недели для обновления списка времени
lessonDaySelect.addEventListener('change', function() {
updateTimeOptions(this.value);
});
// Закрытие модалки по крестику
if (modalAddLessonClose) {
modalAddLessonClose.addEventListener('click', () => {
modalAddLesson.classList.remove('open');
resetLessonForm();
});
}
// Закрытие по клику на overlay
if (modalAddLesson) {
modalAddLesson.addEventListener('click', (e) => {
if (e.target === modalAddLesson) {
modalAddLesson.classList.remove('open');
resetLessonForm();
}
});
}
// Загружаем все данные при инициализации
// =========================================================
// Инициализация
// =========================================================
await Promise.all([loadUsers(), loadGroups(), loadSubjects(), loadClassrooms()]);
// Элементы второго модального окна
const modalViewLessons = document.getElementById('modal-view-lessons');
const modalViewLessonsClose = document.getElementById('modal-view-lessons-close');
const lessonsContainer = document.getElementById('lessons-container');
const modalTeacherName = document.getElementById('modal-teacher-name');
// Функция для загрузки и отображения занятий преподавателя
// =========================================================
// 2-я модалка: просмотр занятий
// =========================================================
async function loadTeacherLessons(teacherId, teacherName) {
try {
lessonsContainer.innerHTML = '<div class="loading-lessons">Загрузка занятий...</div>';
// Устанавливаем имя преподавателя в заголовок
if (teacherName) {
modalTeacherName.textContent = `Занятия преподавателя: ${teacherName}`;
} else {
modalTeacherName.textContent = 'Занятия преподавателя';
}
modalTeacherName.textContent = teacherName
? `Занятия преподавателя: ${teacherName}`
: 'Занятия преподавателя';
// Загружаем занятия
const lessons = await api.get(`/api/users/lessons/${teacherId}`);
if (!lessons || lessons.length === 0) {
@@ -384,34 +417,27 @@ export async function initUsers() {
return;
}
// Группируем занятия по дням недели
const daysOrder = ['Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота'];
const lessonsByDay = {};
lessons.forEach(lesson => {
if (!lessonsByDay[lesson.day]) {
lessonsByDay[lesson.day] = [];
}
if (!lessonsByDay[lesson.day]) lessonsByDay[lesson.day] = [];
lessonsByDay[lesson.day].push(lesson);
});
// Сортируем занятия в каждом дне по времени
Object.keys(lessonsByDay).forEach(day => {
lessonsByDay[day].sort((a, b) => {
// Простая сортировка по времени (можно улучшить)
return a.time.localeCompare(b.time);
});
lessonsByDay[day].sort((a, b) => a.time.localeCompare(b.time));
});
// Формируем HTML
let html = '';
daysOrder.forEach(day => {
if (lessonsByDay[day]) {
html += `<div class="lesson-day-divider">${day}</div>`;
if (!lessonsByDay[day]) return;
lessonsByDay[day].forEach(lesson => {
html += `
html += `<div class="lesson-day-divider">${day}</div>`;
lessonsByDay[day].forEach(lesson => {
html += `
<div class="lesson-card">
<div class="lesson-card-header">
<span class="lesson-group">${escapeHtml(lesson.groupName)}</span>
@@ -428,36 +454,68 @@ export async function initUsers() {
</div>
</div>
`;
});
}
});
});
lessonsContainer.innerHTML = html;
} catch (e) {
lessonsContainer.innerHTML = `<div class="no-lessons">Ошибка загрузки: ${escapeHtml(e.message)}</div>`;
console.error('Ошибка загрузки занятий:', e);
}
}
// Функция открытия модального окна
function openViewLessonsModal(teacherId, teacherName) {
currentLessonsTeacherId = teacherId;
currentLessonsTeacherName = teacherName || '';
loadTeacherLessons(teacherId, teacherName);
requestAnimationFrame(() => syncAddLessonHeight());
modalViewLessons.classList.add('open');
// Блокируем скролл страницы
document.body.style.overflow = 'hidden';
updateBackdrop();
// document.body.style.overflow = 'hidden';
}
// Функция закрытия модального окна
function closeViewLessonsModal() {
modalViewLessons.classList.remove('open');
document.body.style.overflow = '';
updateBackdrop();
// document.body.style.overflow = '';
currentLessonsTeacherId = null;
currentLessonsTeacherName = '';
}
// Обработчик кликов по таблице (добавляем в существующий)
// Найдите в коде usersTbody.addEventListener('click', ...) и добавьте в него:
if (modalViewLessonsClose) {
modalViewLessonsClose.addEventListener('click', closeViewLessonsModal);
}
// ДОЛЖНО ПОЛУЧИТЬСЯ ТАК:
if (modalViewLessons) {
modalViewLessons.addEventListener('click', (e) => {
if (e.target === modalViewLessons) closeViewLessonsModal();
});
}
document.addEventListener('keydown', (e) => {
if (e.key !== 'Escape') return;
if (modalAddLesson?.classList.contains('open')) {
modalAddLesson.classList.remove('open');
resetLessonForm();
syncAddLessonHeight();
updateBackdrop();
return;
}
if (modalViewLessons?.classList.contains('open')) {
closeViewLessonsModal();
return;
}
});
// =========================================================
// ЕДИНЫЙ обработчик кликов по таблице (ВАЖНО: без дубля)
// =========================================================
usersTbody.addEventListener('click', async (e) => {
const deleteBtn = e.target.closest('.btn-delete');
if (deleteBtn) {
@@ -465,8 +523,8 @@ export async function initUsers() {
try {
await api.delete('/api/users/' + deleteBtn.dataset.id);
loadUsers();
} catch (e) {
alert(e.message || 'Ошибка удаления');
} catch (err) {
alert(err.message || 'Ошибка удаления');
}
return;
}
@@ -474,40 +532,23 @@ export async function initUsers() {
const addLessonBtn = e.target.closest('.btn-add-lesson');
if (addLessonBtn) {
e.preventDefault();
if (modalAddLesson) {
openAddLessonModal(addLessonBtn.dataset.id);
}
const teacherId = addLessonBtn.dataset.id;
const teacherName = addLessonBtn.dataset.name;
openAddLessonModal(teacherId);
openViewLessonsModal(teacherId, teacherName);
return;
}
// НОВЫЙ ОБРАБОТЧИК для кнопки просмотра занятий
const viewLessonsBtn = e.target.closest('.btn-view-lessons');
if (viewLessonsBtn) {
e.preventDefault();
const teacherId = viewLessonsBtn.dataset.id;
const teacherName = viewLessonsBtn.dataset.name;
openViewLessonsModal(teacherId, teacherName);
}
});
// Обработчики закрытия модального окна
if (modalViewLessonsClose) {
modalViewLessonsClose.addEventListener('click', closeViewLessonsModal);
}
// Закрытие по клику на overlay
if (modalViewLessons) {
modalViewLessons.addEventListener('click', (e) => {
if (e.target === modalViewLessons) {
closeViewLessonsModal();
}
});
}
// Закрытие по Escape
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && modalViewLessons?.classList.contains('open')) {
closeViewLessonsModal();
return;
}
});
}