// Theme toggle functionality
document.addEventListener('DOMContentLoaded', () => {
// Check for saved theme preference or use preferred color scheme
const savedTheme = localStorage.getItem('theme') ||
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
document.documentElement.classList.add(savedTheme);
// Initialize all tooltips
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
});
function toggleTheme() {
const html = document.documentElement;
const isDark = html.classList.contains('dark');
if (isDark) {
html.classList.remove('dark');
localStorage.setItem('theme', 'light');
} else {
html.classList.add('dark');
localStorage.setItem('theme', 'dark');
}
}
// Form validation example
function validateLoginForm() {
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
if (!username || !password) {
showAlert('Please fill in all fields', 'error');
return false;
}
// Hardcoded admin credentials
if (username === 'admin' && password === 'admin123') {
showAlert('Login successful!', 'success');
// Redirect to dashboard after successful login
setTimeout(() => {
window.location.href = 'dashboard.html';
}, 1000);
return true;
} else {
showAlert('Invalid username or password', 'error');
return false;
}
}
function showAlert(message, type) {
const alertDiv = document.createElement('div');
alertDiv.className = `alert alert-${type} fixed top-4 right-4 z-50`;
alertDiv.innerHTML = `
${message}
`;
document.body.appendChild(alertDiv);
feather.replace();
setTimeout(() => {
alertDiv.remove();
}, 5000);
}
// Simulate API calls
async function fetchPatients() {
try {
// In a real app, this would be a fetch to your backend API
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const data = await response.json();
return data.slice(0, 5); // Return first 5 as sample patient data
} catch (error) {
console.error('Error fetching patients:', error);
return [];
}
}
// Check if user is logged in when accessing dashboard
function checkAuth() {
if (window.location.pathname.includes('dashboard.html') && !localStorage.getItem('loggedIn')) {
window.location.href = 'login.html';
}
}
// Run auth check when page loads
document.addEventListener('DOMContentLoaded', checkAuth);