116 lines
4.7 KiB
JavaScript
116 lines
4.7 KiB
JavaScript
// 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 = `
|
||
<div class="profile-card">
|
||
<div class="profile-field">
|
||
<i class="fas fa-user"></i>
|
||
<div>
|
||
<strong>Имя:</strong>
|
||
<p>${data.user.name}</p>
|
||
</div>
|
||
</div>
|
||
<div class="profile-field">
|
||
<i class="fas fa-at"></i>
|
||
<div>
|
||
<strong>Логин:</strong>
|
||
<p>${data.user.login}</p>
|
||
</div>
|
||
</div>
|
||
<div class="profile-field">
|
||
<i class="fas fa-envelope"></i>
|
||
<div>
|
||
<strong>Email:</strong>
|
||
<p>${data.user.email || 'Не указан'}</p>
|
||
</div>
|
||
</div>
|
||
<div class="profile-field">
|
||
<i class="fas fa-user-tag"></i>
|
||
<div>
|
||
<strong>Роль:</strong>
|
||
<p>${data.user.role === 'admin' ? 'Администратор' : 'Учитель'}</p>
|
||
</div>
|
||
</div>
|
||
<div class="profile-field">
|
||
<i class="fas fa-key"></i>
|
||
<div>
|
||
<strong>Тип авторизации:</strong>
|
||
<p>${data.user.auth_type === 'ldap' ? 'LDAP' : 'Локальная'}</p>
|
||
</div>
|
||
</div>
|
||
${data.user.groups && data.user.groups.length > 0 ? `
|
||
<div class="profile-field">
|
||
<i class="fas fa-users"></i>
|
||
<div>
|
||
<strong>Группы:</strong>
|
||
<p>${Array.isArray(data.user.groups) ? data.user.groups.join(', ') : data.user.groups}</p>
|
||
</div>
|
||
</div>
|
||
` : ''}
|
||
</div>
|
||
`;
|
||
}
|
||
} 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('Ошибка сохранения настроек');
|
||
}
|
||
} |