Mise à jour

This commit is contained in:
2026-06-03 14:47:35 +02:00
parent 085cf33114
commit ad0d86e734
16 changed files with 669 additions and 889 deletions

View File

@@ -1,13 +1,3 @@
/**
* ============================================
* RESERVATION.JS - Système de réservation
* Smart Parking v3.0
* CORRIGÉ : ne bloque plus une place pour
* toujours — vérifie uniquement si
* une voiture est physiquement là
* ============================================
*/
const PRICING = {
30: 2,
60: 3,
@@ -27,14 +17,46 @@ const TIME_SLOTS = [
let currentReservation = null;
document.addEventListener('DOMContentLoaded', () => {
console.log('📅 Initialisation du système de réservation...');
cleanupExpiredReservations();
initReservationForm();
initDatePicker();
initTimeSlots();
initPricePreview();
initConfirmationModal();
setInterval(cleanupExpiredReservations, 60000);
});
function cleanupExpiredReservations() {
let reservations = JSON.parse(localStorage.getItem('smart_parking_reservations') || '[]');
const now = new Date();
let cleaned = 0;
reservations.forEach(r => {
if (r.status !== 'active' && r.status !== 'pending') return;
const endDateTime = new Date(r.date + 'T' + r.endTime);
if (endDateTime < now) {
r.status = 'completed';
cleaned++;
}
});
if (cleaned > 0) {
localStorage.setItem('smart_parking_reservations', JSON.stringify(reservations));
}
}
function checkLocalConflict(spotId, date, startTime, endTime) {
cleanupExpiredReservations();
const reservations = JSON.parse(localStorage.getItem('smart_parking_reservations') || '[]');
return reservations.some(r => {
if (r.spotId !== spotId) return false;
if (r.date !== date) return false;
if (r.status !== 'active' && r.status !== 'pending') return false;
return r.startTime < endTime && r.endTime > startTime;
});
}
function initReservationForm() {
const form = document.getElementById('reservationForm');
if (!form) return;
@@ -52,18 +74,16 @@ function initDatePicker() {
function initTimeSlots() {
const select = document.getElementById('resStartTime');
if (!select) return;
TIME_SLOTS.forEach(time => {
const option = document.createElement('option');
option.value = time;
option.textContent = time;
select.appendChild(option);
});
const now = new Date();
const currentHour = now.getHours();
const currentMins = now.getMinutes();
const nextSlot = TIME_SLOTS.find(t => {
const now = new Date();
const currentHour = now.getHours();
const currentMins = now.getMinutes();
const nextSlot = TIME_SLOTS.find(t => {
const [h, m] = t.split(':').map(Number);
return h > currentHour || (h === currentHour && m > currentMins);
});
@@ -83,14 +103,6 @@ function updatePricePreview() {
document.getElementById('previewPrice').textContent = price + '€';
}
/**
* Gère la soumission du formulaire
*
* CORRIGÉ : on n'empêche plus la réservation si la place
* est "reserved" dans le localStorage. On laisse le serveur
* vérifier les conflits d'horaire. On bloque uniquement si
* une voiture est physiquement détectée (status "occupied").
*/
async function handleReservationSubmit(e) {
e.preventDefault();
@@ -111,25 +123,29 @@ async function handleReservationSubmit(e) {
return;
}
// CORRIGÉ : on bloque uniquement si une voiture est physiquement là
// Une place "reserved" peut quand même être réservée à un autre horaire
const spots = JSON.parse(localStorage.getItem('smart_parking_spots') || '[]');
const spot = spots.find(s => s.id === spotId);
if (spot && spot.status === 'occupied') {
Dashboard.showToast('Une voiture est déjà sur cette place', 'error');
Dashboard.showToast('Une voiture est deja sur cette place', 'error');
if (window.ParkingMap) window.ParkingMap.refresh();
return;
}
// Calculer l'heure de fin
const endDate = new Date(date + 'T' + startTime);
endDate.setMinutes(endDate.getMinutes() + duration);
const endTime = endDate.toTimeString().slice(0, 5);
// Essayer de créer la réservation via l'API
// Le serveur vérifiera les conflits d'horaire
if (checkLocalConflict(spotId, date, startTime, endTime)) {
Dashboard.showToast(
'Cette place est deja reservee a cet horaire. Essayez un autre creneau ou une autre date.',
'error'
);
return;
}
const token = localStorage.getItem('smart_parking_token');
let apiSuccess = false;
if (token) {
try {
@@ -149,12 +165,10 @@ async function handleReservationSubmit(e) {
const data = await response.json();
if (!data.success) {
// Le serveur a détecté un conflit ou une erreur
Dashboard.showToast(data.message, 'error');
return;
}
// Succès via API
currentReservation = {
id: data.data.id,
userId: user.id,
@@ -167,28 +181,34 @@ async function handleReservationSubmit(e) {
status: 'active',
createdAt: new Date().toISOString()
};
apiSuccess = true;
} catch (_err) {
// Mode offline : enregistrement local
currentReservation = creerReservationLocale(
user, spotId, spot, date, startTime, endTime, duration, vehicle
);
apiSuccess = false;
}
} else {
// Pas de token : mode offline
currentReservation = creerReservationLocale(
user, spotId, spot, date, startTime, endTime, duration, vehicle
);
}
// Sauvegarder dans le localStorage
if (!apiSuccess) {
currentReservation = {
id: Date.now(),
userId: user.id,
userName: user.name,
spotId: spotId,
spotNumber: spot ? spot.number : spotId,
date, startTime, endTime, duration,
vehicle: vehicle.toUpperCase(),
price: PRICING[duration],
status: 'active',
createdAt: new Date().toISOString()
};
}
let reservations = JSON.parse(localStorage.getItem('smart_parking_reservations') || '[]');
reservations.push(currentReservation);
localStorage.setItem('smart_parking_reservations', JSON.stringify(reservations));
// Mettre à jour la carte seulement si la réservation est pour aujourd'hui
const today = new Date().toISOString().split('T')[0];
const now = new Date();
const today = new Date().toISOString().split('T')[0];
const now = new Date();
const resStart = new Date(date + 'T' + startTime);
const diffMin = (resStart - now) / 60000;
@@ -197,40 +217,16 @@ async function handleReservationSubmit(e) {
}
addToHistory(
'Réservation',
`Place ${currentReservation.spotNumber} réservée le ${date} de ${startTime} à ${endTime}${PRICING[duration]}`
'Reservation',
'Place ' + currentReservation.spotNumber + ' reservee le ' + date + ' de ' + startTime + ' a ' + endTime + ' — ' + PRICING[duration] + '€'
);
// Réinitialiser le formulaire
document.getElementById('reservationForm').reset();
initDatePicker();
updatePricePreview();
showConfirmationModal();
}
/**
* Crée une réservation en mode offline (localStorage)
*/
function creerReservationLocale(user, spotId, spot, date, startTime, endTime, duration, vehicle) {
return {
id: Date.now(),
userId: user.id,
userName: user.name,
spotId: spotId,
spotNumber: spot ? spot.number : spotId,
date, startTime, endTime, duration,
vehicle: vehicle.toUpperCase(),
price: PRICING[duration],
status: 'active',
createdAt: new Date().toISOString()
};
}
// ============================================
// MODAL DE CONFIRMATION
// ============================================
function initConfirmationModal() {
document.getElementById('closeConfirmationModal')?.addEventListener('click', hideConfirmationModal);
document.getElementById('closeConfirmationBtn')?.addEventListener('click', hideConfirmationModal);
@@ -238,60 +234,44 @@ function initConfirmationModal() {
function showConfirmationModal() {
if (!currentReservation) return;
const modal = document.getElementById('confirmationModal');
document.getElementById('paySpot').textContent = 'Place ' + currentReservation.spotNumber;
document.getElementById('payDate').textContent = formatDate(currentReservation.date);
document.getElementById('payTime').textContent = currentReservation.startTime + ' - ' + currentReservation.endTime;
document.getElementById('payDuration').textContent = formatDuration(currentReservation.duration);
document.getElementById('payTotal').textContent = currentReservation.price + '€';
modal.classList.remove('hidden');
}
function hideConfirmationModal() {
document.getElementById('confirmationModal').classList.add('hidden');
if (window.Dashboard) {
Dashboard.navigateToPage('my-reservations');
document.querySelector('[data-page="my-reservations"]')?.classList.add('active');
document.querySelector('[data-page="reservation"]')?.classList.remove('active');
}
if (window.Dashboard) Dashboard.refreshStats();
}
// ============================================
// UTILITAIRES
// ============================================
function addToHistory(action, details) {
let history = JSON.parse(localStorage.getItem('smart_parking_history') || '[]');
history.unshift({
id: Date.now(),
action,
details,
timestamp: new Date().toISOString()
});
history.unshift({ id: Date.now(), action, details, timestamp: new Date().toISOString() });
if (history.length > 100) history = history.slice(0, 100);
localStorage.setItem('smart_parking_history', JSON.stringify(history));
}
function formatDate(dateString) {
return new Date(dateString).toLocaleDateString('fr-FR', {
day: '2-digit', month: '2-digit', year: 'numeric'
});
return new Date(dateString).toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: 'numeric' });
}
function formatDuration(minutes) {
if (minutes >= 480) return 'Journée (8h)';
if (minutes >= 480) return 'Journee (8h)';
if (minutes >= 60) {
const h = Math.floor(minutes / 60);
const m = minutes % 60;
return m > 0 ? `${h}h ${m}min` : `${h}h`;
return m > 0 ? h + 'h ' + m + 'min' : h + 'h';
}
return `${minutes} min`;
return minutes + ' min';
}
window.Reservation = { PRICING, TIME_SLOTS, formatDuration, addToHistory };