180 lines
5.5 KiB
JavaScript
180 lines
5.5 KiB
JavaScript
const POCKETBASE_URL = 'https://pocketbase.ccllc.pro';
|
|
|
|
// Initialize PocketBase client
|
|
const pb = new PocketBase(POCKETBASE_URL);
|
|
|
|
// Check authentication and load user data
|
|
async function initDashboard() {
|
|
// Load auth from cookies first
|
|
try {
|
|
pb.authStore.loadFromCookie(document.cookie);
|
|
} catch (e) {
|
|
console.error('Error loading from cookie:', e);
|
|
}
|
|
|
|
// If not valid from cookie, try loading from localStorage
|
|
if (!pb.authStore.isValid) {
|
|
const storedToken = localStorage.getItem('pb_auth_token');
|
|
const storedModel = localStorage.getItem('pb_auth_model');
|
|
|
|
if (storedToken && storedModel) {
|
|
try {
|
|
pb.authStore.save(storedToken, JSON.parse(storedModel));
|
|
} catch (e) {
|
|
console.error('Error loading from localStorage:', e);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check if user is authenticated
|
|
if (!pb.authStore.isValid) {
|
|
// Try to refresh the token if we have a token but it's expired
|
|
if (pb.authStore.token) {
|
|
try {
|
|
await pb.collection('users').authRefresh();
|
|
// Update localStorage after refresh
|
|
if (pb.authStore.token && pb.authStore.model) {
|
|
localStorage.setItem('pb_auth_token', pb.authStore.token);
|
|
localStorage.setItem('pb_auth_model', JSON.stringify(pb.authStore.model));
|
|
}
|
|
} catch (e) {
|
|
console.error('Token refresh failed:', e);
|
|
// If refresh fails, clear everything and redirect to login
|
|
pb.authStore.clear();
|
|
localStorage.removeItem('pb_auth_token');
|
|
localStorage.removeItem('pb_auth_model');
|
|
window.location.href = '/index.html';
|
|
return;
|
|
}
|
|
} else {
|
|
// No token at all, redirect to login
|
|
localStorage.removeItem('pb_auth_token');
|
|
localStorage.removeItem('pb_auth_model');
|
|
window.location.href = '/index.html';
|
|
return;
|
|
}
|
|
}
|
|
|
|
try {
|
|
// Display user information
|
|
const user = pb.authStore.model;
|
|
if (user) {
|
|
const userEmailElement = document.getElementById('userEmail');
|
|
const userNameElement = document.getElementById('userName');
|
|
|
|
if (userEmailElement) {
|
|
userEmailElement.textContent = user.email || 'User';
|
|
}
|
|
if (userNameElement) {
|
|
userNameElement.textContent = user.username || user.email || 'User';
|
|
}
|
|
} else {
|
|
// No user model, redirect to login
|
|
console.error('No user model found');
|
|
pb.authStore.clear();
|
|
localStorage.removeItem('pb_auth_token');
|
|
localStorage.removeItem('pb_auth_model');
|
|
window.location.href = '/index.html';
|
|
}
|
|
} catch (error) {
|
|
console.error('Dashboard init error:', error);
|
|
pb.authStore.clear();
|
|
localStorage.removeItem('pb_auth_token');
|
|
localStorage.removeItem('pb_auth_model');
|
|
window.location.href = '/index.html';
|
|
}
|
|
}
|
|
|
|
// Get auth token for API requests
|
|
function getAuthToken() {
|
|
return pb.authStore.token || localStorage.getItem('pb_auth_token') || '';
|
|
}
|
|
|
|
// Load dashboard statistics
|
|
async function loadStats() {
|
|
try {
|
|
const token = getAuthToken();
|
|
if (!token) {
|
|
return;
|
|
}
|
|
|
|
// Load employees stats
|
|
try {
|
|
const employeesResponse = await fetch('/api/employees?page=1&perPage=1000', {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
},
|
|
});
|
|
|
|
if (employeesResponse.ok) {
|
|
const employeesData = await employeesResponse.json();
|
|
const totalEmployeesElement = document.getElementById('totalEmployees');
|
|
if (totalEmployeesElement) {
|
|
totalEmployeesElement.innerHTML = employeesData.totalItems || 0;
|
|
}
|
|
|
|
// Count active employees (those with Type = "Employee")
|
|
const activeCount = employeesData.items?.filter(emp => emp.Type === 'Employee').length || 0;
|
|
const activeEmployeesElement = document.getElementById('activeEmployees');
|
|
if (activeEmployeesElement) {
|
|
activeEmployeesElement.innerHTML = activeCount;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error loading employee stats:', error);
|
|
}
|
|
|
|
// Load reports stats
|
|
try {
|
|
const reportsResponse = await fetch('/api/reports?page=1&perPage=1', {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
},
|
|
});
|
|
|
|
if (reportsResponse.ok) {
|
|
const reportsData = await reportsResponse.json();
|
|
const totalReportsElement = document.getElementById('totalReports');
|
|
if (totalReportsElement) {
|
|
totalReportsElement.innerHTML = reportsData.totalItems || 0;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error loading report stats:', error);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error loading stats:', error);
|
|
}
|
|
}
|
|
|
|
// Handle logout
|
|
function handleLogout() {
|
|
const logoutButton = document.getElementById('logoutButton');
|
|
|
|
if (logoutButton) {
|
|
logoutButton.addEventListener('click', async () => {
|
|
try {
|
|
await pb.authStore.clear();
|
|
localStorage.removeItem('pb_auth_token');
|
|
localStorage.removeItem('pb_auth_model');
|
|
window.location.href = '/index.html';
|
|
} catch (error) {
|
|
console.error('Logout error:', error);
|
|
// Clear auth store anyway
|
|
pb.authStore.clear();
|
|
localStorage.removeItem('pb_auth_token');
|
|
localStorage.removeItem('pb_auth_model');
|
|
window.location.href = '/index.html';
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// Initialize dashboard when page loads
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
await initDashboard();
|
|
handleLogout();
|
|
loadStats();
|
|
});
|
|
|