// Main Application Handler
class Application {
constructor() {
this.init();
}
async init() {
// Wait for auth check
const isAuth = await auth.checkAuth();
if (isAuth) {
// Redirect to dashboard
if (window.location.hash === '' || window.location.hash === '#login') {
window.location.hash = '#dashboard';
}
} else {
// Redirect to login
window.location.hash = '#login';
}
}
loadDashboardData() {
this.loadStats();
this.loadTablesList();
}
async loadStats() {
try {
const stats = await api.getDatabaseStats();
document.getElementById('statsTableCount').textContent = stats.tableCount || 0;
document.getElementById('statsRecordCount').textContent = stats.recordCount || 0;
document.getElementById('statsUserCount').textContent = stats.userCount || 0;
document.getElementById('statsDbSize').textContent = stats.dbSize || '-';
} catch (error) {
console.error('Failed to load stats:', error);
}
}
async loadTablesList() {
try {
const tables = await api.getTables();
const tablesList = document.getElementById('tablesList');
if (tables.length === 0) {
tablesList.innerHTML = '
Таблицы не найдены
';
return;
}
tablesList.innerHTML = tables.map(table => `
${table.name}
${table.recordCount || 0} записей
`).join('');
if (typeof lucide !== 'undefined') {
lucide.createIcons();
}
} catch (error) {
console.error('Failed to load tables:', error);
}
}
loadAdminPanel() {
this.loadUsers();
}
async loadUsers() {
try {
const users = await api.getUsers();
const usersList = document.getElementById('usersList');
if (!users || users.length === 0) {
usersList.innerHTML = '| Пользователи не найдены |
';
return;
}
usersList.innerHTML = users.map(user => `
| ${user.name} |
${user.email} |
${this.getRoleName(user.role)}
|
${user.active ? 'Активен' : 'Неактивен'}
|
|
`).join('');
if (typeof lucide !== 'undefined') {
lucide.createIcons();
}
this.setupAdminPanelHandlers();
} catch (error) {
console.error('Failed to load users:', error);
}
}
setupAdminPanelHandlers() {
// Add user button
const addUserBtn = document.getElementById('addUserBtn');
if (addUserBtn) {
addUserBtn.onclick = () => this.showUserModal(null);
}
// User modal controls
const userModal = document.getElementById('userModal');
const closeBtn = document.getElementById('closeUserModal');
const cancelBtn = document.getElementById('cancelUserEdit');
const userForm = document.getElementById('userForm');
if (closeBtn) closeBtn.onclick = () => userModal.classList.add('hidden');
if (cancelBtn) cancelBtn.onclick = () => userModal.classList.add('hidden');
if (userForm) {
userForm.onsubmit = (e) => this.handleUserFormSubmit(e);
}
}
showUserModal(userId) {
const userModal = document.getElementById('userModal');
const userForm = document.getElementById('userForm');
const userModalTitle = document.getElementById('userModalTitle');
userForm.reset();
userForm.dataset.userId = userId || '';
if (userId) {
userModalTitle.textContent = 'Редактировать пользователя';
// Load user data
// TODO: implement
} else {
userModalTitle.textContent = 'Добавить пользователя';
}
userModal.classList.remove('hidden');
}
async handleUserFormSubmit(e) {
e.preventDefault();
const formData = {
name: document.getElementById('userNameInput').value,
email: document.getElementById('userEmailInput').value,
role: document.getElementById('userRoleSelect').value,
password: document.getElementById('userPasswordInput').value
};
const userId = e.target.dataset.userId;
try {
if (userId) {
await api.updateUser(userId, formData);
} else {
await api.createUser(formData);
}
document.getElementById('userModal').classList.add('hidden');
this.loadUsers();
} catch (error) {
alert('Ошибка: ' + error.message);
}
}
async editUser(userId) {
// TODO: implement
this.showUserModal(userId);
}
async deleteUser(userId) {
if (confirm('Вы уверены, что хотите удалить этого пользователя?')) {
try {
await api.deleteUser(userId);
this.loadUsers();
} catch (error) {
alert('Ошибка: ' + error.message);
}
}
}
getRoleName(role) {
const names = {
'superadmin': 'Суперадминистратор',
'admin': 'Администратор',
'moderator': 'Модератор',
'viewer': 'Только просмотр'
};
return names[role] || role;
}
}
// Initialize application
const app = new Application();
// Update content when route changes
document.addEventListener('DOMContentLoaded', () => {
window.addEventListener('hashchange', () => {
const currentRoute = window.location.hash.slice(1);
// Load dashboard data
if (currentRoute.includes('dashboard') || currentRoute === '') {
app.loadDashboardData();
}
// Load admin panel
if (currentRoute.includes('admin')) {
app.loadAdminPanel();
}
});
});