File size: 4,764 Bytes
d6d41bd
d59e335
 
 
 
 
 
 
 
 
 
49dafe6
 
 
 
d59e335
 
 
 
 
 
 
6b579f8
d6d41bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d912ad0
 
 
d6d41bd
 
d912ad0
 
 
d6d41bd
d912ad0
 
d6d41bd
d912ad0
 
d6d41bd
d912ad0
 
 
 
 
d6d41bd
 
d912ad0
d6d41bd
 
 
d912ad0
d6d41bd
 
d912ad0
 
 
 
 
 
 
 
 
 
49dafe6
 
 
 
 
 
 
 
 
 
 
 
 
 
d912ad0
49dafe6
d912ad0
 
 
49dafe6
 
 
 
d912ad0
 
 
49dafe6
d912ad0
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

// 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 => `
                <div class="property-card bg-white rounded-lg overflow-hidden shadow-lg transition-all duration-300 hover:shadow-xl">
                    <img src="${property.image}" alt="${property.title}" class="w-full h-64 object-cover">
                    <div class="p-6">
                        <h3 class="text-xl font-bold text-gray-800">${property.title}</h3>
                        <p class="mt-2 text-gray-600">${property.location}</p>
                        <div class="mt-4 flex justify-between items-center">
                            <span class="text-lg font-semibold text-blue-600">${property.price}</span>
                            <span class="text-sm text-gray-500">${property.size}</span>
                        </div>
                        ${property.features ? `<div class="mt-4 flex space-x-2">
                            ${property.features.map(feat => 
                                `<span class="bg-gray-100 px-2 py-1 rounded text-sm">${feat}</span>`
                            ).join('')}
                        </div>` : ''}
                    </div>
                </div>
            `).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();
        });
    }
});