// Shared JavaScript const API_BASE_URL = 'http://localhost:5000/api'; // Check admin access function checkAdminAccess() { const token = localStorage.getItem('authToken'); if (!token) { window.location.href = '/login.html'; return false; } // Demo admin access - in production, verify with backend function isAdminUser() { // For demo purposes, allow access if token exists // In a real app, you would verify the token and user role with the backend return true; } // Handle logout function handleLogout() { localStorage.removeItem('authToken'); window.location.href = '/login.html'; } document.addEventListener('DOMContentLoaded', () => { // Form submission handling const forms = document.querySelectorAll('form'); forms.forEach(form => { form.addEventListener('submit', (e) => { e.preventDefault(); // Form handling logic would go here console.log('Form submitted'); }); }); // Property loading for properties page if (window.location.pathname.includes('properties.html')) { loadProperties(); } }); async function loadProperties() { try { const response = await fetch('http://localhost:5000/api/properties'); const properties = await response.json(); const propertiesContainer = document.querySelector('.grid'); if (propertiesContainer) { propertiesContainer.innerHTML = properties.map(property => `
${property.title}

${property.title}

${property.location}

${property.price} ${property.size}
${property.features ? `
${property.features.map(feat => `${feat}` ).join('')}
` : ''}
`).join(''); } } catch (error) { console.error('Error loading properties:', error); alert('Failed to load properties. Please try again later.'); } } // Handle login form submission document.addEventListener('DOMContentLoaded', () => { const loginForm = document.getElementById('loginForm'); if (loginForm) { loginForm.addEventListener('submit', async (e) => { e.preventDefault(); const email = e.target.email.value; const password = e.target.password.value; try { const response = await fetch('http://localhost:5000/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email, password }), }); const data = await response.json(); if (data.token) { localStorage.setItem('authToken', data.token); // Check if admin and redirect accordingly if (email === 'admin@luxuryestates.com' || data.user?.role === 'admin') { window.location.href = '/admin.html'; } else { window.location.href = '/properties.html'; } } catch (error) { console.error('Login error:', error); // For demo purposes, allow admin access with specific credentials if (email === 'admin@luxuryestates.com' && password === 'admin123') { localStorage.setItem('authToken', 'demo-admin-token'); window.location.href = '/admin.html'; } }); } // Form submission handling for contact form const contactForm = document.querySelector('form'); if (contactForm && !contactForm.id) { contactForm.addEventListener('submit', (e) => { e.preventDefault(); // In a real app, you'd send this to your backend alert('Message sent! We will contact you soon.'); contactForm.reset(); }); } });