diff --git a/src/main/resources/static/js/matchDetailChangeScore.js b/src/main/resources/static/js/matchDetailChangeScore.js new file mode 100644 index 0000000000000000000000000000000000000000..0c1ba94815e6db26dd6f5dd9e9caba2e556d02f2 --- /dev/null +++ b/src/main/resources/static/js/matchDetailChangeScore.js @@ -0,0 +1,67 @@ +loadNavbar('navbar-container'); +loadFooter('footer-container'); +const urlParams = new URLSearchParams(window.location.search); +const matchId = urlParams.get('id'); + +async function fetchMatchDetails(id) { + try { + const response = await fetch(`/match/${id}`); + const matchData = await response.json(); + document.getElementById('sport').value = matchData.sport; + document.getElementById('playerAId').value = matchData.playerAId; + document.getElementById('playerBId').value = matchData.playerBId; + document.getElementById('planTime').value = matchData.planTime; + document.getElementById('status').value = matchData.status; + document.getElementById('scoreA').value = matchData.scoreA; + document.getElementById('scoreB').value = matchData.scoreB; + document.getElementById('confirmByA').checked = matchData.confirmByA; + document.getElementById('confirmByB').checked = matchData.confirmByB; + document.getElementById('winnerId').value = matchData.winnerId; + // document.getElementById('scoreA').value = matchData.scoreA; + // document.getElementById('scoreB').value = matchData.scoreB; + // document.getElementById('winnerId').value = matchData.winnerId ? matchData.winnerId : ''; + } catch (error) { + console.error('Error fetching match details:', error); + } +} + +document.getElementById('updateMatchForm').addEventListener('submit', async function(event) { + // prevent evnts + event.preventDefault(); + + + const updatedMatch = { + sport: document.getElementById('sport').value, + player_AId: document.getElementById('playerAId').value, + player_BId: document.getElementById('playerBId').value, + plan_Time: document.getElementById('planTime').value, + status: document.getElementById('status').value, + scoreA: document.getElementById('scoreA').value, + scoreB: document.getElementById('scoreB').value, + confirm_a: document.getElementById('confirmByA').checked, + confirm_b: document.getElementById('confirmByB').checked, + winnerId: document.getElementById('winnerId').value + }; + + try { + const res = await fetch(`/match/${matchId}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(updatedMatch) + }); + + if (res.ok) { + alert('Match updated successfully'); + window.location.href = `/html/matchDetail.html?id=${matchId}`; + } else { + alert('Failed to update match'); + } + } catch (err) { + console.error('error updating match:', err); + alert('error updating match'); + } +}); + +fetchMatchDetails(matchId); \ No newline at end of file