const API_BASE_URL = window.location.origin + '/api'; let currentUser = null; let currentMatch = null; // Fonction API async function apiCall(endpoint, method = 'GET', data = null) { const options = { method: method, headers: { 'Content-Type': 'application/json' } }; const token = localStorage.getItem('authToken'); if (token) { options.headers['Authorization'] = `Bearer ${token}`; } if (data && (method === 'POST' || method === 'PUT')) { options.body = JSON.stringify(data); } try { const response = await fetch(`${API_BASE_URL}/${endpoint}`, options); const result = await response.json(); if (!response.ok) { throw new Error(result.error || 'Erreur serveur'); } return result; } catch (error) { console.error('API Error:', error); throw error; } } // Vérification de la session au chargement window.onload = async function() { const token = localStorage.getItem('authToken'); if (!token) { window.location.href = 'index.html'; return; } try { const result = await apiCall('auth.php?action=me'); currentUser = result.user; document.getElementById('userName').textContent = currentUser.username; document.getElementById('userPoints').textContent = `${currentUser.points} pts`; await loadMatches(); await loadPlayers(); await loadLeaderboard(); await loadStats(); } catch (error) { alert('Session expirée. Veuillez vous reconnecter.'); localStorage.removeItem('authToken'); localStorage.removeItem('currentUser'); window.location.href = 'index.html'; } }; function logout() { localStorage.removeItem('authToken'); localStorage.removeItem('currentUser'); window.location.href = 'index.html'; } function showSection(sectionId) { document.querySelectorAll('.section').forEach(s => s.classList.remove('active')); document.querySelectorAll('.sidebar li').forEach(l => l.classList.remove('active')); document.getElementById(sectionId).classList.add('active'); event.target.classList.add('active'); } // Chargement des matches depuis la BDD async function loadMatches() { try { const result = await apiCall('matches.php'); const container = document.getElementById('matchesList'); container.innerHTML = ''; result.matches.forEach(match => { const matchCard = document.createElement('div'); matchCard.className = 'match-card'; matchCard.onclick = () => openPredictionModal(match); matchCard.innerHTML = `
${match.player1.nationality} - ${match.player1.handedness}
Ranking: #${match.player1.ranking}
${match.player2.nationality} - ${match.player2.handedness}
Ranking: #${match.player2.ranking}
${new Date(match.date).toLocaleDateString('fr-FR')}
${match.court}
${match.status === 'completed' ? `${match.score}
` : ''}#${player.ranking} Mondial
${match.player1.nationality}
${match.player2.nationality}
${match.round} - ${match.court}
`; // Afficher les probabilités document.getElementById('prob1').style.width = `${result.probabilities.player1}%`; document.getElementById('prob2').style.width = `${result.probabilities.player2}%`; document.getElementById('probText1').textContent = `${result.probabilities.player1}%`; document.getElementById('probText2').textContent = `${result.probabilities.player2}%`; // Afficher l'analyse const analysisDiv = document.getElementById('matchupAnalysis'); analysisDiv.innerHTML = `