// EstateSphere Pro - Main JavaScript class EstateSphereApp { constructor() { this.currentUser = null; this.darkMode = false; this.apiBaseUrl = 'http://localhost:3000/api'; this.init(); } init() { this.setupTheme(); this.setupEventListeners(); this.loadUserPreferences(); this.checkAuthStatus(); } setupTheme() { const savedTheme = localStorage.getItem('estatesphere-theme'); const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; this.darkMode = savedTheme === 'dark' || (!savedTheme && prefersDark); this.applyTheme(); } applyTheme() { if (this.darkMode) { document.documentElement.classList.add('dark'); } else { document.documentElement.classList.remove('dark'); } localStorage.setItem('estatesphere-theme', this.darkMode ? 'dark' : 'light'); } toggleTheme() { this.darkMode = !this.darkMode; this.applyTheme(); } async apiCall(endpoint, options = {}) { try { const token = localStorage.getItem('estatesphere-token'); const headers = { 'Content-Type': 'application/json', ...options.headers }; if (token) { headers['Authorization'] = `Bearer ${token}`; } const response = await fetch(`${this.apiBaseUrl}${endpoint}`, { ...options, headers }); if (!response.ok) { throw new Error(`API error: ${response.statusText}`); } const data = await response.json(); return { success: true, data }; } catch (error) { console.error('API call failed:', error); return { success: false, error: error.message }; } } setupEventListeners() { // Theme toggle const themeToggle = document.querySelector('.theme-toggle'); if (themeToggle) { themeToggle.addEventListener('click', () => this.toggleTheme()); } // Mobile menu toggle const mobileMenuBtn = document.querySelector('.mobile-menu-btn'); const mobileMenu = document.querySelector('.mobile-menu'); if (mobileMenuBtn && mobileMenu) { mobileMenuBtn.addEventListener('click', () => { mobileMenu.classList.toggle('hidden'); }); } // Property search functionality this.setupPropertySearch(); // Contact form handling this.setupContactForm(); // Login form handling this.setupLoginForm(); // Registration form handling this.setupRegistrationForm(); } setupPropertySearch() { const searchForm = document.querySelector('#property-search'); if (searchForm) { searchForm.addEventListener('submit', (e) => { e.preventDefault(); this.performPropertySearch(new FormData(searchForm)); }); } async performPropertySearch(formData) { const params = new URLSearchParams(); for (const [key, value] of formData.entries()) { if (value) params.append(key, value); } // Show loading state this.showLoading(); try { const result = await this.apiCall(`/properties/search?${params.toString()}`); if (result.success) { this.displaySearchResults(result.data); } else { alert('Search failed: ' + result.error); } } catch (error) { alert('Search failed: ' + error.message); } finally { this.hideLoading(); } } showLoading() { const searchButton = document.querySelector('#property-search button[type="submit"]'); if (searchButton) { searchButton.innerHTML = '
'; } } hideLoading() { const searchButton = document.querySelector('#property-search button[type="submit"]'); if (searchButton) { searchButton.textContent = 'Search Properties'; } } displaySearchResults(properties) { const propertiesGrid = document.getElementById('properties-grid'); if (!propertiesGrid) return; if (properties.length === 0) { propertiesGrid.innerHTML = 'No properties found matching your criteria.
${property.bedrooms} Beds • ${property.bathrooms} Baths • ${property.squareFeet} sqft