// profile.js - Личный кабинет и настройки
// Личный кабинет
function showProfileSection() {
showSection('profile');
loadUserProfile();
loadNotificationSettings();
}
async function loadUserProfile() {
try {
const response = await fetch('/api/user');
const data = await response.json();
if (data.user) {
const userInfo = document.getElementById('user-profile-info');
userInfo.innerHTML = `
Логин:
${data.user.login}
Email:
${data.user.email || 'Не указан'}
Роль:
${data.user.role === 'admin' ? 'Администратор' : 'Учитель'}
Тип авторизации:
${data.user.auth_type === 'ldap' ? 'LDAP' : 'Локальная'}
${data.user.groups && data.user.groups.length > 0 ? `
Группы:
${Array.isArray(data.user.groups) ? data.user.groups.join(', ') : data.user.groups}
` : ''}
`;
}
} catch (error) {
console.error('Ошибка загрузки профиля:', error);
}
}
// Настройки уведомлений
async function loadNotificationSettings() {
try {
const response = await fetch('/api/user/settings');
const settings = await response.json();
document.getElementById('email-notifications').checked = settings.email_notifications;
document.getElementById('notification-email').value = settings.notification_email || '';
document.getElementById('telegram-notifications').checked = settings.telegram_notifications;
document.getElementById('vk-notifications').checked = settings.vk_notifications;
} catch (error) {
console.error('Ошибка загрузки настроек:', error);
}
}
async function saveNotificationSettings(event) {
event.preventDefault();
const settings = {
email_notifications: document.getElementById('email-notifications').checked,
notification_email: document.getElementById('notification-email').value.trim(),
telegram_notifications: document.getElementById('telegram-notifications').checked,
vk_notifications: document.getElementById('vk-notifications').checked
};
try {
const response = await fetch('/api/user/settings', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(settings)
});
const result = await response.json();
if (result.success) {
alert('Настройки уведомлений сохранены!');
} else {
alert('Ошибка сохранения настроек: ' + (result.error || 'Неизвестная ошибка'));
}
} catch (error) {
console.error('Ошибка сохранения настроек:', error);
alert('Ошибка сохранения настроек');
}
}