File size: 5,022 Bytes
3aeaee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67efaec
3aeaee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67efaec
 
3aeaee2
 
 
 
67efaec
3aeaee2
 
 
 
67efaec
3aeaee2
 
67efaec
3aeaee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67efaec
3aeaee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67efaec
3aeaee2
 
 
 
 
 
 
 
 
 
 
 
 
67efaec
 
 
3aeaee2
 
 
 
 
 
 
 
 
 
 
67efaec
 
3aeaee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

// Global state
let currentUser = null;
const API_BASE_URL = 'https://api.godsrods.online';

// Initialize app
document.addEventListener('DOMContentLoaded', async function() {
    await checkAuthStatus();
    loadMarketplaceData();
    init3DViewers();
});

async function checkAuthStatus() {
    try {
        const response = await fetch(`${API_BASE_URL}/auth/status`, {
            credentials: 'include'
        });
        
        if (response.ok) {
            const data = await response.json();
            currentUser = data.user;
            updateUIAuthState(true);
            updateUserBalance(data.balance, data.tokens);
        } else {
            updateUIAuthState(false);
        }
    } catch (error) {
        console.error('Auth check failed:', error);
        updateUIAuthState(false);
    }
}

function updateUIAuthState(isLoggedIn) {
    const authElements = document.querySelectorAll('.auth-only');
    const guestElements = document.querySelectorAll('.guest-only');
    
    authElements.forEach(el => {
        el.classList.toggle('hidden', !isLoggedIn);
    });
    
    guestElements.forEach(el => {
        el.classList.toggle('hidden', isLoggedIn);
    });
}

function updateUserBalance(cash, tokens) {
    if (currentUser) {
        document.querySelectorAll('.user-cash').forEach(el => {
            el.textContent = `${cash.toLocaleString()}`;
        });
        document.querySelectorAll('.user-tokens').forEach(el => {
            el.textContent = `${tokens.toLocaleString()} GTR`;
        });
    }
}

async function loadMarketplaceData() {
    try {
        const response = await fetch(`${API_BASE_URL}/marketplace`);
        if (response.ok) {
            const data = await response.json();
            renderCarListings(data.listings);
        }
    } catch (error) {
        console.error('Failed to load marketplace data:', error);
    }
}

function renderCarListings(listings) {
    const container = document.querySelector('#listings-container') || 
                     document.querySelector('.grid.grid-cols-1.md\\:grid-cols-2.lg\\:grid-cols-3.gap-6');
    
    if (container) {
        container.innerHTML = '';
        listings.forEach(listing => {
            const card = document.createElement('custom-car-card');
            card.setAttribute('title', listing.title);
            card.setAttribute('price', listing.price);
            card.setAttribute('tokens', listing.tokenCost);
            card.setAttribute('image', listing.imageUrl);
            card.setAttribute('model', listing.modelUrl);
            card.setAttribute('id', `car-${listing.id}`);
            container.appendChild(card);
        });
    }
}

function init3DViewers() {
    // Initialize 3D model viewers
    document.addEventListener('click', function(e) {
        if (e.target.closest('custom-car-card')) {
            const card = e.target.closest('custom-car-card');
            const modelUrl = card.getAttribute('model');
            openModelViewer(modelUrl);
        }
    });
}

function openModelViewer(modelUrl) {
    // This would show a modal with the 3D viewer
    console.log('Opening 3D viewer for:', modelUrl);
    // Implementation would use model-viewer or three.js
}

// Auth functions
async function handleLogin(email, password) {
    try {
        const response = await fetch(`${API_BASE_URL}/auth/login`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ email, password }),
            credentials: 'include'
        });
        
        if (response.ok) {
            const data = await response.json();
            currentUser = data.user;
            updateUIAuthState(true);
            updateUserBalance(data.balance, data.tokens);
            return { success: true };
        } else {
            return { success: false, error: 'Invalid credentials' };
        }
    } catch (error) {
        console.error('Login failed:', error);
        return { success: false, error: 'Network error' };
    }
}

async function handleLogout() {
    try {
        await fetch(`${API_BASE_URL}/auth/logout`, {
            method: 'POST',
            credentials: 'include'
        });
        currentUser = null;
        updateUIAuthState(false);
    } catch (error) {
        console.error('Logout failed:', error);
    }
}

// Admin functions
async function adminAddListing(listingData) {
    if (!currentUser?.isAdmin) return;
    
    try {
        const response = await fetch(`${API_BASE_URL}/admin/listings`, {
            method: 'POST',
            headers: { 
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${currentUser.token}`
            },
            body: JSON.stringify(listingData)
        });
        
        if (response.ok) {
            loadMarketplaceData();
            return true;
        }
        return false;
    } catch (error) {
        console.error('Failed to add listing:', error);
        return false;
    }
}