400 lines
18 KiB
JavaScript
400 lines
18 KiB
JavaScript
import { api } from '../api.js';
|
|
import { escapeHtml, showAlert, hideAlert } from '../utils.js';
|
|
|
|
export async function initUniversityStructure() {
|
|
const deptTbody = document.getElementById('departments-tbody');
|
|
const specTbody = document.getElementById('specialties-tbody');
|
|
|
|
const createDeptForm = document.getElementById('create-department-form');
|
|
const createSpecForm = document.getElementById('create-specialty-form');
|
|
const editDeptForm = document.getElementById('edit-department-form');
|
|
const editSpecForm = document.getElementById('edit-specialty-form');
|
|
const editDeptModal = document.getElementById('modal-edit-department');
|
|
const editSpecModal = document.getElementById('modal-edit-specialty');
|
|
const editDeptClose = document.getElementById('modal-edit-department-close');
|
|
const editSpecClose = document.getElementById('modal-edit-specialty-close');
|
|
|
|
// Элементы вкладок
|
|
const tabDepartments = document.getElementById('btn-tab-departments');
|
|
const tabSpecialties = document.getElementById('btn-tab-specialties');
|
|
const contentDepartments = document.getElementById('tab-content-departments');
|
|
const contentSpecialties = document.getElementById('tab-content-specialties');
|
|
|
|
// Элементы модалки профилей
|
|
const profilesModal = document.getElementById('modal-manage-profiles');
|
|
const profilesModalClose = document.getElementById('modal-manage-profiles-close');
|
|
const profilesSpecContext = document.getElementById('profiles-spec-context');
|
|
const profileFormTitle = document.getElementById('profile-form-title');
|
|
const manageProfileForm = document.getElementById('manage-profile-form');
|
|
const profileIdInput = document.getElementById('manage-profile-id');
|
|
const profileSpecIdInput = document.getElementById('manage-profile-spec-id');
|
|
const profileNameInput = document.getElementById('manage-profile-name');
|
|
const profileDescInput = document.getElementById('manage-profile-description');
|
|
const btnSaveProfile = document.getElementById('btn-save-profile');
|
|
const btnCancelProfileEdit = document.getElementById('btn-cancel-profile-edit');
|
|
const profilesTbody = document.getElementById('manage-profiles-tbody');
|
|
|
|
let departments = [];
|
|
let specialties = [];
|
|
let currentProfiles = [];
|
|
|
|
// --- Логика переключения табов ---
|
|
tabDepartments.addEventListener('click', () => {
|
|
tabDepartments.classList.add('active');
|
|
tabDepartments.style.borderBottomColor = 'var(--primary)';
|
|
tabDepartments.style.color = 'var(--text-main)';
|
|
tabSpecialties.classList.remove('active');
|
|
tabSpecialties.style.borderBottomColor = 'transparent';
|
|
tabSpecialties.style.color = 'var(--text-secondary)';
|
|
contentDepartments.style.display = 'block';
|
|
contentSpecialties.style.display = 'none';
|
|
});
|
|
|
|
tabSpecialties.addEventListener('click', () => {
|
|
tabSpecialties.classList.add('active');
|
|
tabSpecialties.style.borderBottomColor = 'var(--primary)';
|
|
tabSpecialties.style.color = 'var(--text-main)';
|
|
tabDepartments.classList.remove('active');
|
|
tabDepartments.style.borderBottomColor = 'transparent';
|
|
tabDepartments.style.color = 'var(--text-secondary)';
|
|
contentSpecialties.style.display = 'block';
|
|
contentDepartments.style.display = 'none';
|
|
});
|
|
|
|
// --- Загрузка данных ---
|
|
async function loadData() {
|
|
// Загрузка кафедр
|
|
try {
|
|
departments = await api.get('/api/departments');
|
|
renderDepartments();
|
|
} catch (e) {
|
|
deptTbody.innerHTML = '<tr><td colspan="4" class="loading-row">Ошибка загрузки</td></tr>';
|
|
}
|
|
|
|
// Загрузка специальностей
|
|
try {
|
|
specialties = await api.get('/api/specialties');
|
|
renderSpecialties();
|
|
} catch (e) {
|
|
specTbody.innerHTML = '<tr><td colspan="4" class="loading-row">Ошибка загрузки</td></tr>';
|
|
}
|
|
}
|
|
|
|
function renderDepartments() {
|
|
if (!departments || !departments.length) {
|
|
deptTbody.innerHTML = '<tr><td colspan="4" class="loading-row">Кафедры не созданы</td></tr>';
|
|
return;
|
|
}
|
|
deptTbody.innerHTML = departments.map(d => `
|
|
<tr>
|
|
<td>${d.id}</td>
|
|
<td>${escapeHtml(d.departmentName || d.name)}</td>
|
|
<td>${escapeHtml(String(d.departmentCode || d.code))}</td>
|
|
<td>
|
|
<button class="btn-edit-classroom btn-edit-department" data-id="${d.id}">Изменить</button>
|
|
<button class="btn-delete btn-delete-department" data-id="${d.id}">Удалить</button>
|
|
</td>
|
|
</tr>
|
|
`).join('');
|
|
}
|
|
|
|
function renderSpecialties() {
|
|
if (!specialties || !specialties.length) {
|
|
specTbody.innerHTML = '<tr><td colspan="4" class="loading-row">Специальности не созданы</td></tr>';
|
|
return;
|
|
}
|
|
specTbody.innerHTML = specialties.map(s => `
|
|
<tr>
|
|
<td>${s.id}</td>
|
|
<td>${escapeHtml(s.specialityName || s.name)}</td>
|
|
<td>${escapeHtml(s.specialityCode || s.specialtyCode || s.specialty_code)}</td>
|
|
<td>
|
|
<button class="btn-edit-classroom btn-edit-specialty" data-id="${s.id}">Изменить</button>
|
|
<button class="btn-primary btn-manage-profiles" data-id="${s.id}" style="padding: 0.25rem 0.5rem; font-size: 0.85rem;">Профили</button>
|
|
<button class="btn-delete btn-delete-specialty" data-id="${s.id}">Удалить</button>
|
|
</td>
|
|
</tr>
|
|
`).join('');
|
|
}
|
|
|
|
// --- Кафедры: CRUD ---
|
|
createDeptForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
hideAlert('create-dept-alert');
|
|
const name = document.getElementById('dept-name').value.trim();
|
|
const code = document.getElementById('dept-code').value.trim();
|
|
|
|
if (!name || !code) {
|
|
showAlert('create-dept-alert', 'Заполните все поля', 'error');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await api.post('/api/departments', { departmentName: name, departmentCode: Number(code) });
|
|
showAlert('create-dept-alert', `Кафедра "${name}" создана`, 'success');
|
|
createDeptForm.reset();
|
|
loadData();
|
|
} catch (error) {
|
|
showAlert('create-dept-alert', error.message || 'Ошибка создания кафедры', 'error');
|
|
}
|
|
});
|
|
|
|
deptTbody.addEventListener('click', async (e) => {
|
|
const editBtn = e.target.closest('.btn-edit-department');
|
|
const deleteBtn = e.target.closest('.btn-delete-department');
|
|
|
|
if (editBtn) {
|
|
const department = departments.find(item => item.id == editBtn.dataset.id);
|
|
if (!department) return;
|
|
|
|
document.getElementById('edit-dept-id').value = department.id;
|
|
document.getElementById('edit-dept-name').value = department.departmentName || department.name || '';
|
|
document.getElementById('edit-dept-code').value = department.departmentCode || department.code || '';
|
|
hideAlert('edit-dept-alert');
|
|
editDeptModal.classList.add('open');
|
|
return;
|
|
}
|
|
|
|
if (deleteBtn) {
|
|
if (!confirm('Удалить кафедру?')) return;
|
|
try {
|
|
await api.delete('/api/departments/' + deleteBtn.dataset.id);
|
|
showAlert('create-dept-alert', 'Кафедра удалена', 'success');
|
|
await loadData();
|
|
} catch (error) {
|
|
showAlert('create-dept-alert', error.message || 'Ошибка удаления кафедры', 'error');
|
|
}
|
|
}
|
|
});
|
|
|
|
editDeptForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
hideAlert('edit-dept-alert');
|
|
const id = document.getElementById('edit-dept-id').value;
|
|
const name = document.getElementById('edit-dept-name').value.trim();
|
|
const code = document.getElementById('edit-dept-code').value.trim();
|
|
|
|
if (!name || !code) {
|
|
showAlert('edit-dept-alert', 'Заполните все поля', 'error');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await api.put('/api/departments/' + id, { departmentName: name, departmentCode: Number(code) });
|
|
editDeptModal.classList.remove('open');
|
|
showAlert('create-dept-alert', `Кафедра "${name}" обновлена`, 'success');
|
|
await loadData();
|
|
} catch (error) {
|
|
showAlert('edit-dept-alert', error.message || 'Ошибка обновления кафедры', 'error');
|
|
}
|
|
});
|
|
|
|
// --- Специальности: CRUD ---
|
|
createSpecForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
hideAlert('create-spec-alert');
|
|
const name = document.getElementById('spec-name').value.trim();
|
|
const code = document.getElementById('spec-code').value.trim();
|
|
|
|
if (!name || !code) {
|
|
showAlert('create-spec-alert', 'Заполните все поля', 'error');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await api.post('/api/specialties', { specialityName: name, specialityCode: code });
|
|
showAlert('create-spec-alert', `Специальность "${name}" создана`, 'success');
|
|
createSpecForm.reset();
|
|
loadData();
|
|
} catch (error) {
|
|
showAlert('create-spec-alert', error.message || 'Ошибка создания специальности', 'error');
|
|
}
|
|
});
|
|
|
|
specTbody.addEventListener('click', async (e) => {
|
|
const editBtn = e.target.closest('.btn-edit-specialty');
|
|
const deleteBtn = e.target.closest('.btn-delete-specialty');
|
|
const profilesBtn = e.target.closest('.btn-manage-profiles');
|
|
|
|
if (editBtn) {
|
|
const speciality = specialties.find(item => item.id == editBtn.dataset.id);
|
|
if (!speciality) return;
|
|
|
|
document.getElementById('edit-spec-id').value = speciality.id;
|
|
document.getElementById('edit-spec-name').value = speciality.specialityName || speciality.name || '';
|
|
document.getElementById('edit-spec-code').value = speciality.specialityCode || speciality.specialtyCode || speciality.specialty_code || '';
|
|
hideAlert('edit-spec-alert');
|
|
editSpecModal.classList.add('open');
|
|
return;
|
|
}
|
|
|
|
if (deleteBtn) {
|
|
if (!confirm('Удалить специальность?')) return;
|
|
try {
|
|
await api.delete('/api/specialties/' + deleteBtn.dataset.id);
|
|
showAlert('create-spec-alert', 'Специальность удалена', 'success');
|
|
await loadData();
|
|
} catch (error) {
|
|
showAlert('create-spec-alert', error.message || 'Ошибка удаления специальности', 'error');
|
|
}
|
|
}
|
|
|
|
if (profilesBtn) {
|
|
const specId = profilesBtn.dataset.id;
|
|
const speciality = specialties.find(item => item.id == specId);
|
|
if (!speciality) return;
|
|
|
|
openProfilesModal(speciality);
|
|
}
|
|
});
|
|
|
|
editSpecForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
hideAlert('edit-spec-alert');
|
|
const id = document.getElementById('edit-spec-id').value;
|
|
const name = document.getElementById('edit-spec-name').value.trim();
|
|
const code = document.getElementById('edit-spec-code').value.trim();
|
|
|
|
if (!name || !code) {
|
|
showAlert('edit-spec-alert', 'Заполните все поля', 'error');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await api.put('/api/specialties/' + id, { specialityName: name, specialityCode: code });
|
|
editSpecModal.classList.remove('open');
|
|
showAlert('create-spec-alert', `Специальность "${name}" обновлена`, 'success');
|
|
await loadData();
|
|
} catch (error) {
|
|
showAlert('edit-spec-alert', error.message || 'Ошибка обновления специальности', 'error');
|
|
}
|
|
});
|
|
|
|
// --- Профили обучения: Логика ---
|
|
async function openProfilesModal(speciality) {
|
|
profilesSpecContext.textContent = `${speciality.specialityCode || speciality.specialtyCode || speciality.specialty_code} · ${speciality.specialityName || speciality.name}`;
|
|
profileSpecIdInput.value = speciality.id;
|
|
resetProfileForm();
|
|
profilesModal.classList.add('open');
|
|
await loadProfiles(speciality.id);
|
|
}
|
|
|
|
async function loadProfiles(specId) {
|
|
profilesTbody.innerHTML = '<tr><td colspan="4" class="loading-row">Загрузка...</td></tr>';
|
|
try {
|
|
currentProfiles = await api.get(`/api/specialties/${specId}/profiles`);
|
|
renderProfilesTable();
|
|
} catch (e) {
|
|
profilesTbody.innerHTML = `<tr><td colspan="4" class="loading-row">Ошибка загрузки: ${escapeHtml(e.message)}</td></tr>`;
|
|
}
|
|
}
|
|
|
|
function renderProfilesTable() {
|
|
if (!currentProfiles || !currentProfiles.length) {
|
|
profilesTbody.innerHTML = '<tr><td colspan="4" class="loading-row">Профили не созданы</td></tr>';
|
|
return;
|
|
}
|
|
profilesTbody.innerHTML = currentProfiles.map(p => `
|
|
<tr>
|
|
<td>${p.id}</td>
|
|
<td>${escapeHtml(p.name)}</td>
|
|
<td>${escapeHtml(p.description || '-')}</td>
|
|
<td>
|
|
<button class="btn-edit-classroom btn-edit-profile" data-id="${p.id}">Изменить</button>
|
|
<button class="btn-delete btn-delete-profile" data-id="${p.id}">Удалить</button>
|
|
</td>
|
|
</tr>
|
|
`).join('');
|
|
}
|
|
|
|
function resetProfileForm() {
|
|
manageProfileForm.reset();
|
|
profileIdInput.value = '';
|
|
profileFormTitle.textContent = 'Создание профиля';
|
|
btnSaveProfile.textContent = 'Создать';
|
|
btnCancelProfileEdit.style.display = 'none';
|
|
hideAlert('manage-profile-alert');
|
|
}
|
|
|
|
manageProfileForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
hideAlert('manage-profile-alert');
|
|
|
|
const specId = profileSpecIdInput.value;
|
|
const profileId = profileIdInput.value;
|
|
const name = profileNameInput.value.trim();
|
|
const description = profileDescInput.value.trim();
|
|
|
|
if (!name) {
|
|
showAlert('manage-profile-alert', 'Введите название профиля', 'error');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (profileId) {
|
|
// Обновление
|
|
await api.put(`/api/specialties/${specId}/profiles/${profileId}`, { id: Number(profileId), name, description });
|
|
showAlert('manage-profile-alert', `Профиль "${name}" обновлен`, 'success');
|
|
} else {
|
|
// Создание
|
|
await api.post(`/api/specialties/${specId}/profiles`, { name, description });
|
|
showAlert('manage-profile-alert', `Профиль "${name}" создан`, 'success');
|
|
}
|
|
resetProfileForm();
|
|
await loadProfiles(specId);
|
|
} catch (error) {
|
|
showAlert('manage-profile-alert', error.message || 'Ошибка сохранения профиля', 'error');
|
|
}
|
|
});
|
|
|
|
profilesTbody.addEventListener('click', async (e) => {
|
|
const editBtn = e.target.closest('.btn-edit-profile');
|
|
const deleteBtn = e.target.closest('.btn-delete-profile');
|
|
const specId = profileSpecIdInput.value;
|
|
|
|
if (editBtn) {
|
|
const profile = currentProfiles.find(item => item.id == editBtn.dataset.id);
|
|
if (!profile) return;
|
|
|
|
profileIdInput.value = profile.id;
|
|
profileNameInput.value = profile.name || '';
|
|
profileDescInput.value = profile.description || '';
|
|
profileFormTitle.textContent = 'Редактирование профиля';
|
|
btnSaveProfile.textContent = 'Сохранить';
|
|
btnCancelProfileEdit.style.display = 'inline-block';
|
|
hideAlert('manage-profile-alert');
|
|
return;
|
|
}
|
|
|
|
if (deleteBtn) {
|
|
if (!confirm('Удалить профиль обучения?')) return;
|
|
try {
|
|
await api.delete(`/api/specialties/${specId}/profiles/${deleteBtn.dataset.id}`);
|
|
showAlert('manage-profile-alert', 'Профиль удален', 'success');
|
|
await loadProfiles(specId);
|
|
} catch (error) {
|
|
showAlert('manage-profile-alert', error.message || 'Ошибка удаления профиля', 'error');
|
|
}
|
|
}
|
|
});
|
|
|
|
btnCancelProfileEdit.addEventListener('click', resetProfileForm);
|
|
|
|
// Закрытие модалок
|
|
editDeptClose.addEventListener('click', () => editDeptModal.classList.remove('open'));
|
|
editSpecClose.addEventListener('click', () => editSpecModal.classList.remove('open'));
|
|
profilesModalClose.addEventListener('click', () => profilesModal.classList.remove('open'));
|
|
|
|
editDeptModal.addEventListener('click', (e) => {
|
|
if (e.target === editDeptModal) editDeptModal.classList.remove('open');
|
|
});
|
|
editSpecModal.addEventListener('click', (e) => {
|
|
if (e.target === editSpecModal) editSpecModal.classList.remove('open');
|
|
});
|
|
profilesModal.addEventListener('click', (e) => {
|
|
if (e.target === profilesModal) profilesModal.classList.remove('open');
|
|
});
|
|
|
|
await loadData();
|
|
}
|