137 lines
6.1 KiB
JavaScript
137 lines
6.1 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-modern">
|
||
<!-- Шапка профиля с аватаром -->
|
||
<div class="profile-header">
|
||
<div class="profile-avatar">
|
||
<i class="fas fa-user-circle"></i>
|
||
</div>
|
||
<div class="profile-title">
|
||
<h3>${data.user.name}</h3>
|
||
<span class="profile-badge ${data.user.role}">
|
||
${data.user.role === 'admin' ? 'Администратор' : 'Сотрудник'}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
<!-- Сетка с информацией -->
|
||
<div class="profile-grid">
|
||
<div class="profile-info-card">
|
||
<div class="info-icon">
|
||
<i class="fas fa-at"></i>
|
||
</div>
|
||
<div class="info-content">
|
||
<span class="info-label">Логин</span>
|
||
<span class="info-value">${data.user.login}</span>
|
||
</div>
|
||
</div>
|
||
<div class="profile-info-card">
|
||
<div class="info-icon">
|
||
<i class="fas fa-shield-alt"></i>
|
||
</div>
|
||
<div class="info-content">
|
||
<span class="info-label">Тип авторизации</span>
|
||
<span class="info-value">
|
||
${data.user.auth_type === 'ldap' ?
|
||
'<span class="badge-ldap"><i class="fas fa-building"></i> LDAP</span>' :
|
||
'<span class="badge-local"><i class="fas fa-database"></i> Локальная</span>'}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
|
||
${data.user.groups && data.user.groups.length > 0 ? `
|
||
<div class="profile-info-card groups-card">
|
||
<div class="info-icon">
|
||
<i class="fas fa-users"></i>
|
||
</div>
|
||
<div class="info-content">
|
||
<span class="info-label">Группы доступа</span>
|
||
<div class="groups-list">
|
||
${Array.isArray(data.user.groups) ?
|
||
data.user.groups.map(group =>
|
||
`<span class="group-tag">${group}</span>`
|
||
).join('') :
|
||
`<span class="group-tag">${data.user.groups}</span>`
|
||
}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
` : ''}
|
||
</div>
|
||
</div>
|
||
`;
|
||
}
|
||
} catch (error) {
|
||
console.error('Ошибка загрузки профиля:', error);
|
||
const userInfo = document.getElementById('user-profile-info');
|
||
if (userInfo) {
|
||
userInfo.innerHTML = `
|
||
<div class="profile-error">
|
||
<i class="fas fa-exclamation-circle"></i>
|
||
<p>Ошибка загрузки профиля</p>
|
||
</div>
|
||
`;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Настройки уведомлений
|
||
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('Ошибка сохранения настроек');
|
||
}
|
||
} |