// Tab functionality document.querySelectorAll('.tab-button').forEach(button => { button.addEventListener('click', () => { // Remove active class from all tabs and contents document.querySelectorAll('.tab-button').forEach(btn => { btn.classList.remove('active'); }); document.querySelectorAll('.tab-content').forEach(content => { content.classList.remove('active'); }); // Add active class to clicked tab button.classList.add('active'); // Show corresponding content const tabId = button.getAttribute('data-tab'); document.getElementById(`${tabId}-tab`).classList.add('active'); }); }); // Login functionality with auto-redirect document.getElementById('client-login-btn').addEventListener('click', handleClientLogin); document.getElementById('staff-login-btn').addEventListener('click', handleStaffLogin); document.getElementById('admin-login-btn').addEventListener('click', handleAdminLogin); async function handleClientLogin(e) { e.preventDefault(); const email = document.getElementById('client-username').value; const password = document.getElementById('client-password').value; await loginUser(email, password, 'client'); } async function handleStaffLogin(e) { e.preventDefault(); const email = document.getElementById('staff-username').value; const password = document.getElementById('staff-password').value; await loginUser(email, password, 'staff'); } async function handleAdminLogin(e) { e.preventDefault(); const email = document.getElementById('admin-username').value; const password = document.getElementById('admin-password').value; await loginUser(email, password, 'admin'); } async function loginUser(email, password, userType) { if (!email || !password) { alert('Please enter both email and password.'); return; } try { const { auth, db } = await import('/js/firebase-init.js'); const { signInWithEmailAndPassword } = await import('https://www.gstatic.com/firebasejs/11.6.1/firebase-auth.js'); const { doc, getDoc } = await import('https://www.gstatic.com/firebasejs/11.6.1/firebase-firestore.js'); const userCredential = await signInWithEmailAndPassword(auth, email, password); const uid = userCredential.user.uid; // Check if user exists in Firestore and has correct role const userDoc = await getDoc(doc(db, 'users', uid)); if (userDoc.exists()) { const userData = userDoc.data(); // Check if user has the correct role for this login type if (userData.role !== userType && userData.role !== 'admin') { throw new Error(`Access denied. This account doesn't have ${userType} privileges.`); } // Store user info in localStorage localStorage.setItem('currentUser', JSON.stringify({ uid: uid, email: userCredential.user.email, role: userData.role, name: userData.name, company: userData.company })); alert(`Successfully logged in as ${userData.name}`); // AUTO-REDIRECT: Redirect based on role setTimeout(() => { if (userData.role === 'admin') { window.location.href = 'admin-dashboard.html'; } else if (userData.role === 'staff') { window.location.href = 'staff-dashboard.html'; } else { window.location.href = 'service-request.html'; } }, 1000); } else { // User doesn't exist in Firestore - redirect to signup localStorage.setItem('pendingUser', JSON.stringify({ email: email, uid: uid, role: userType })); window.location.href = `signup.html?email=${encodeURIComponent(email)}&role=${userType}`; } } catch (error) { console.error('Login error:', error); if (error.code === 'auth/user-not-found') { // User doesn't exist in Authentication - redirect to signup localStorage.setItem('pendingUser', JSON.stringify({ email: email, role: userType })); window.location.href = `signup.html?email=${encodeURIComponent(email)}&role=${userType}`; } else if (error.code === 'auth/wrong-password') { alert('Invalid password. Please try again.'); } else if (error.code === 'auth/invalid-email') { alert('Invalid email address.'); } else { alert('Login failed: ' + error.message); } } } // AUTO-REDIRECT: Check if user is already logged in on page load auth.onAuthStateChanged((user) => { if (user) { // User is signed in, check Firestore for role db.collection('users').doc(user.uid).get().then((doc) => { if (doc.exists) { const userData = doc.data(); // AUTO-REDIRECT based on role console.log('User already logged in, redirecting to:', userData.role); if (userData.role === 'admin') { window.location.href = 'admin-dashboard.html'; } else if (userData.role === 'staff') { window.location.href = 'staff-dashboard.html'; } else { window.location.href = 'service-request.html'; } } }); } });