Actualiser js/admin.js
This commit is contained in:
+89
-31
@@ -5,14 +5,11 @@ async function apiCall(endpoint, method = 'GET', data = null) {
|
||||
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);
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/${endpoint}`, options);
|
||||
const result = await response.json();
|
||||
if (!response.ok) throw new Error(result.error || 'Erreur serveur');
|
||||
@@ -39,9 +36,13 @@ function showAdminSection(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');
|
||||
|
||||
if (sectionId === 'matches') loadMatchesTable();
|
||||
if (window.event && window.event.target) {
|
||||
window.event.target.closest('li').classList.add('active');
|
||||
}
|
||||
if (sectionId === 'matches') {
|
||||
loadMatchesTable();
|
||||
loadPlayersDropdowns();
|
||||
}
|
||||
if (sectionId === 'users') loadUsersTable();
|
||||
if (sectionId === 'results') loadResultsDropdown();
|
||||
}
|
||||
@@ -50,40 +51,111 @@ async function loadAdminStats() {
|
||||
try {
|
||||
const users = await apiCall('users.php');
|
||||
const matches = await apiCall('matches.php');
|
||||
const players = await apiCall('players.php');
|
||||
|
||||
const players = await apiCall('player.php');
|
||||
document.getElementById('totalUsers').textContent = users.users ? users.users.length : 0;
|
||||
document.getElementById('totalMatches').textContent = matches.matches ? matches.matches.length : 0;
|
||||
document.getElementById('totalPlayers').textContent = players.players ? players.players.length : 0;
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur stats admin', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadPlayersDropdowns() {
|
||||
try {
|
||||
const result = await apiCall('player.php');
|
||||
const p1Select = document.getElementById('matchPlayer1');
|
||||
const p2Select = document.getElementById('matchPlayer2');
|
||||
if (!p1Select || !p2Select) return;
|
||||
p1Select.innerHTML = '';
|
||||
p2Select.innerHTML = '';
|
||||
if (result.players) {
|
||||
result.players.forEach(p => {
|
||||
const opt1 = document.createElement('option');
|
||||
opt1.value = p.id;
|
||||
opt1.textContent = `${p.name} (#${p.ranking})`;
|
||||
p1Select.appendChild(opt1);
|
||||
const opt2 = document.createElement('option');
|
||||
opt2.value = p.id;
|
||||
opt2.textContent = `${p.name} (#${p.ranking})`;
|
||||
p2Select.appendChild(opt2);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Erreur chargement joueurs:', e);
|
||||
}
|
||||
}
|
||||
|
||||
async function addMatch() {
|
||||
const round = document.getElementById('matchRound').value;
|
||||
const player1_id = parseInt(document.getElementById('matchPlayer1').value);
|
||||
const player2_id = parseInt(document.getElementById('matchPlayer2').value);
|
||||
const match_date = document.getElementById('matchDate').value;
|
||||
const court = document.getElementById('matchCourt').value || 'Court Philippe-Chatrier';
|
||||
|
||||
if (!match_date || isNaN(player1_id) || isNaN(player2_id)) {
|
||||
alert('Veuillez remplir tous les champs requis.');
|
||||
return;
|
||||
}
|
||||
if (player1_id === player2_id) {
|
||||
alert('Veuillez sélectionner deux joueurs différents.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await apiCall('matches.php', 'POST', { round, player1_id, player2_id, match_date, court });
|
||||
alert('Match ajouté avec succès !');
|
||||
loadMatchesTable();
|
||||
loadAdminStats();
|
||||
} catch (error) {
|
||||
alert(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function addPlayer() {
|
||||
const name = document.getElementById('playerName').value.trim();
|
||||
const nationality = document.getElementById('playerNationality').value.trim();
|
||||
const age = parseInt(document.getElementById('playerAge').value);
|
||||
const handedness = document.getElementById('playerHandedness').value;
|
||||
const ranking = parseInt(document.getElementById('playerRanking').value);
|
||||
|
||||
if (!name || !nationality || isNaN(age) || isNaN(ranking)) {
|
||||
alert('Veuillez remplir tous les champs requis.');
|
||||
return;
|
||||
}
|
||||
const player_code = name.toLowerCase().replace(/[^a-z0-9]/g, '');
|
||||
try {
|
||||
await apiCall('player.php', 'POST', {
|
||||
player_code, name, nationality, age, handedness, ranking,
|
||||
points: 0, clay_win_rate: 0.75, hard_win_rate: 0.70, grass_win_rate: 0.65
|
||||
});
|
||||
alert('Joueur ajouté avec succès !');
|
||||
document.getElementById('playerName').value = '';
|
||||
document.getElementById('playerNationality').value = '';
|
||||
document.getElementById('playerAge').value = '';
|
||||
document.getElementById('playerRanking').value = '';
|
||||
loadAdminStats();
|
||||
} catch (error) {
|
||||
alert(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMatchesTable() {
|
||||
try {
|
||||
const result = await apiCall('matches.php');
|
||||
const tbody = document.getElementById('matchesTable');
|
||||
if (!tbody) return;
|
||||
tbody.innerHTML = '';
|
||||
|
||||
if (result.matches) {
|
||||
result.matches.forEach(match => {
|
||||
const row = document.createElement('tr');
|
||||
// Gestion de l'affichage selon le format retourné par getAllMatches()
|
||||
const p1Name = match.p1_name || (match.player1 && match.player1.name) || 'Inconnu';
|
||||
const p2Name = match.p2_name || (match.player2 && match.player2.name) || 'Inconnu';
|
||||
|
||||
row.innerHTML = `
|
||||
<td>${match.round}</td>
|
||||
<td>${p1Name}</td>
|
||||
<td>${p2Name}</td>
|
||||
<td>${new Date(match.match_date || match.date).toLocaleDateString('fr-FR')}</td>
|
||||
<td>${match.status === 'completed' ? 'Terminé' : 'À venir'}</td>
|
||||
<td>
|
||||
<button class="btn-danger" onclick="deleteMatch(${match.id})">Supprimer</button>
|
||||
</td>
|
||||
<td><button class="btn-danger" onclick="deleteMatch(${match.id})">Supprimer</button></td>
|
||||
`;
|
||||
tbody.appendChild(row);
|
||||
});
|
||||
@@ -97,7 +169,6 @@ async function loadUsersTable() {
|
||||
const tbody = document.getElementById('usersTable');
|
||||
if (!tbody) return;
|
||||
tbody.innerHTML = '';
|
||||
|
||||
if (result.users) {
|
||||
result.users.forEach(user => {
|
||||
const row = document.createElement('tr');
|
||||
@@ -107,9 +178,7 @@ async function loadUsersTable() {
|
||||
<td>${user.email}</td>
|
||||
<td>${user.role}</td>
|
||||
<td>${user.points || 0}</td>
|
||||
<td>
|
||||
<button class="btn-danger" onclick="deleteUser(${user.id})">Supprimer</button>
|
||||
</td>
|
||||
<td><button class="btn-danger" onclick="deleteUser(${user.id})">Supprimer</button></td>
|
||||
`;
|
||||
tbody.appendChild(row);
|
||||
});
|
||||
@@ -143,9 +212,7 @@ async function loadResultsDropdown() {
|
||||
const select = document.getElementById('resultMatch');
|
||||
if (!select) return;
|
||||
select.innerHTML = '';
|
||||
|
||||
window.currentAdminMatches = result.matches || [];
|
||||
|
||||
window.currentAdminMatches.filter(m => m.status !== 'completed').forEach(match => {
|
||||
const option = document.createElement('option');
|
||||
option.value = match.id;
|
||||
@@ -154,7 +221,6 @@ async function loadResultsDropdown() {
|
||||
option.textContent = `${match.round} : ${p1Name} vs ${p2Name}`;
|
||||
select.appendChild(option);
|
||||
});
|
||||
|
||||
updateWinnerSelect();
|
||||
select.addEventListener('change', updateWinnerSelect);
|
||||
} catch (e) { console.error(e); }
|
||||
@@ -163,18 +229,15 @@ async function loadResultsDropdown() {
|
||||
function updateWinnerSelect() {
|
||||
const select = document.getElementById('resultMatch');
|
||||
const winnerSelect = document.getElementById('resultWinner');
|
||||
|
||||
if (select.selectedIndex === -1 || !window.currentAdminMatches) return;
|
||||
const matchId = parseInt(select.value);
|
||||
const match = window.currentAdminMatches.find(m => m.id === matchId);
|
||||
|
||||
winnerSelect.innerHTML = '';
|
||||
if (match) {
|
||||
const p1Id = match.p1_id || (match.player1 && match.player1.id);
|
||||
const p1Name = match.p1_name || (match.player1 && match.player1.name);
|
||||
const p2Id = match.p2_id || (match.player2 && match.player2.id);
|
||||
const p2Name = match.p2_name || (match.player2 && match.player2.name);
|
||||
|
||||
winnerSelect.innerHTML = `
|
||||
<option value="${p1Id}">${p1Name}</option>
|
||||
<option value="${p2Id}">${p2Name}</option>
|
||||
@@ -186,13 +249,8 @@ async function updateResult() {
|
||||
const matchId = parseInt(document.getElementById('resultMatch').value);
|
||||
const winnerId = parseInt(document.getElementById('resultWinner').value);
|
||||
const score = document.getElementById('resultScore').value;
|
||||
|
||||
try {
|
||||
await apiCall('results.php', 'POST', {
|
||||
match_id: matchId,
|
||||
winner_id: winnerId,
|
||||
score: score
|
||||
});
|
||||
await apiCall('results.php', 'POST', { match_id: matchId, winner_id: winnerId, score: score });
|
||||
alert('Résultat mis à jour avec succès ! Les points ont été distribués.');
|
||||
loadResultsDropdown();
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user