Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <div id="sncf-vendeur" style="font-family: Arial, sans-serif; background: #f7f7f7; padding: 20px; max-width: 600px; margin: auto; border-radius: 10px;">
- <h2 style="text-align: center; color: #0099cc;">Mon Billet Étudiant SNCF</h2>
- <div id="main-menu" style="display: flex; flex-wrap: wrap; justify-content: center; gap: 20px; margin-top: 20px;">
- <div onclick="startBuying()" style="background: #0099cc; width: 280px; height: 160px; color: white; display: flex; flex-direction: column; align-items: center; justify-content: center; border-radius: 10px; cursor: pointer;">
- <div style="font-size: 18px; font-weight: bold;">Acheter un billet</div>
- </div>
- </div>
- <div id="buy-ticket" style="display:none; margin-top: 30px; text-align: center;">
- <h2 style="color: #0099cc;">Acheter un billet</h2>
- <div style="margin-bottom: 15px;">
- <select id="destination" style="padding: 10px; font-size: 16px; width: 80%;">
- <option value="">-- Choisir une destination --</option>
- <option value="Rennes">Rennes</option>
- <option value="Lille">Lille</option>
- <option value="Strasbourg">Strasbourg</option>
- <option value="Marseille">Marseille</option>
- <option value="Biarritz">Biarritz</option>
- <option value="Toulouse">Toulouse</option>
- <option value="Nice">Nice</option>
- <option value="Bordeaux">Bordeaux</option>
- <option value="Lyon">Lyon</option>
- <option value="Nantes">Nantes</option>
- </select>
- </div>
- <div style="display: flex; justify-content: center; gap: 10px; margin-bottom: 20px;">
- <button onclick="setTripType('AS')" style="padding: 12px 20px; border: none; background: #0099cc; color: white; border-radius: 8px;">Aller-simple</button>
- <button onclick="setTripType('AR')" style="padding: 12px 20px; border: none; background: #0099cc; color: white; border-radius: 8px;">Aller-retour</button>
- </div>
- <div id="next-steps" style="display:none;"></div>
- <div style="margin-top: 20px;">
- <button onclick="backToMenu()" style="padding: 10px 20px; background: #ccc; border: none; border-radius: 8px;">Retour au menu</button>
- </div>
- </div>
- </div>
- <script>
- let trajetType = "";
- let classeChoisie = null;
- let nbPassagers = 1;
- let horaireAller = null;
- let horaireRetour = null;
- let prixBase1 = 0;
- let prixBase2 = 0;
- let dayDepart = null;
- let dayRetour = null;
- const distances = {
- Rennes: 350,
- Lille: 220,
- Strasbourg: 490,
- Marseille: 775,
- Biarritz: 685,
- Toulouse: 680,
- Nice: 930,
- Bordeaux: 580,
- Lyon: 465,
- Nantes: 385
- };
- const prixKm = 0.095;
- const joursSemaine = ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"];
- function startBuying() {
- document.getElementById('main-menu').style.display = 'none';
- document.getElementById('buy-ticket').style.display = 'block';
- }
- function backToMenu() {
- document.getElementById('buy-ticket').style.display = 'none';
- document.getElementById('main-menu').style.display = 'flex';
- document.getElementById('next-steps').style.display = 'none';
- resetAll();
- }
- function resetAll() {
- trajetType = "";
- classeChoisie = null;
- nbPassagers = 1;
- horaireAller = null;
- horaireRetour = null;
- prixBase1 = prixBase2 = 0;
- dayDepart = dayRetour = null;
- document.getElementById('next-steps').innerHTML = '';
- }
- function setTripType(type) {
- trajetType = type;
- const dest = document.getElementById('destination').value;
- if (!dest) {
- alert("Choisissez d'abord une destination.");
- return;
- }
- let html = `<div style="margin-bottom:15px;">
- <label><strong>Jour de départ :</strong></label><br>
- <select id="day-depart-select" style="padding:10px;font-size:16px;width:80%;margin-top:5px;">`;
- joursSemaine.forEach(j => html += `<option>${j}</option>`);
- html += `</select></div>`;
- if (type === 'AR') {
- html += `<div style="margin-bottom:15px;">
- <label><strong>Jour de retour :</strong></label><br>
- <select id="day-retour-select" style="padding:10px;font-size:16px;width:80%;margin-top:5px;">`;
- joursSemaine.forEach(j => html += `<option>${j}</option>`);
- html += `</select></div>`;
- }
- html += '<button onclick="confirmDays()" style="padding:10px 20px;background:#0099cc;color:white;border:none;border-radius:8px;">Valider les jours</button>';
- const next = document.getElementById('next-steps');
- next.innerHTML = html;
- next.style.display = 'block';
- }
- function confirmDays() {
- const dest = document.getElementById('destination').value;
- const dist = distances[dest];
- prixBase1 = Math.round(dist * prixKm + 30 + Math.random() * 10);
- prixBase2 = Math.round(dist * prixKm + Math.random() * 10);
- dayDepart = document.getElementById('day-depart-select').value;
- if (trajetType === 'AR') {
- dayRetour = document.getElementById('day-retour-select').value;
- }
- document.getElementById('next-steps').innerHTML = generateHorairesHTML();
- }
- function randomHorairePair(minH, maxH) {
- const h1 = minH + Math.floor(Math.random() * (maxH - minH + 1));
- const m1 = Math.floor(Math.random() * 60);
- let d1 = new Date(2000, 0, 1, h1, m1);
- let delay = 45 + Math.floor(Math.random() * (120 - 45 + 1));
- let d2 = new Date(d1.getTime() + delay * 60000);
- let latest = new Date(2000, 0, 1, maxH, 0);
- if (d2 > latest) d2 = latest;
- const toStr = d => `${d.getHours()}h${d.getMinutes().toString().padStart(2,'0')}`;
- return [toStr(d1), toStr(d2)];
- }
- function generateHorairesHTML() {
- const mAller = randomHorairePair(6, 9),
- aAller = randomHorairePair(13, 16),
- mRetour = randomHorairePair(6, 9),
- aRetour = randomHorairePair(13, 16);
- let html = `<p><strong>Jour départ :</strong> ${dayDepart}</p>
- <h3 style="color:#0099cc;">Départ :</h3>`;
- html += buildHorairesSection(mAller, 'aller', 'Matin');
- html += buildHorairesSection(aAller, 'aller', 'Après-midi');
- if (trajetType === 'AR') {
- html += `<p><strong>Jour retour :</strong> ${dayRetour}</p>
- <h3 style="color:#0099cc;">Retour :</h3>`;
- html += buildHorairesSection(mRetour, 'retour', 'Matin');
- html += buildHorairesSection(aRetour, 'retour', 'Après-midi');
- }
- html += buildClasseAndPassagers();
- return html;
- }
- function buildHorairesSection(hs, type, label) {
- let sec = `<div style='margin-bottom:10px;'><strong>${label} :</strong></div>
- <div style='display:flex;justify-content:center;gap:10px;margin-bottom:20px;'>`;
- hs.forEach(h => {
- sec += `<div onclick="selectHoraire(this,'${type}')" style="background:#eee;padding:20px 15px;border-radius:10px;width:100px;text-align:center;cursor:pointer;">
- ${h}<br><small>${type==='aller'?'Aller':'Retour'}</small>
- </div>`;
- });
- sec += '</div>';
- return sec;
- }
- function selectHoraire(el, type) {
- if (type === 'aller') {
- if (horaireAller) {
- horaireAller.style.background = '#eee';
- horaireAller.style.color = 'black';
- }
- horaireAller = (horaireAller === el ? null : el);
- } else {
- if (horaireRetour) {
- horaireRetour.style.background = '#eee';
- horaireRetour.style.color = 'black';
- }
- horaireRetour = (horaireRetour === el ? null : el);
- }
- if (horaireAller === el || horaireRetour === el) {
- el.style.background = '#c55a11';
- el.style.color = 'white';
- }
- updatePrixEtCode();
- }
- function buildClasseAndPassagers() {
- let html = '<h3 style="color:#0099cc;">Classe :</h3><div style="display:flex;justify-content:center;gap:10px;margin-bottom:20px;">';
- html += '<button class="classe-btn" onclick="chooseClasse(1)" style="background:#eee;padding:10px;border:none;border-radius:5px;">1ère</button>';
- html += '<button class="classe-btn" onclick="chooseClasse(2)" style="background:#eee;padding:10px;border:none;border-radius:5px;">2e</button>';
- html += `</div><h3 style="color:#0099cc;">Passagers :</h3><div style="display:flex;justify-content:center;gap:10px;margin-bottom:20px;">`;
- for (let i = 1; i <= 4; i++) {
- html += `<button class="perso-btn" onclick="choosePassagers(${i})" style="background:#eee;padding:10px;border:none;border-radius:5px;">${i}</button>`;
- }
- html += `</div>
- <div id="prix-affiche" style="font-weight:bold;text-align:center;margin-bottom:10px;"></div>
- <div id="code-affiche" style="font-weight:bold;color:#c55a11;text-align:center;margin-bottom:15px;"></div>
- <div style="text-align:center;"><button onclick="resetAll()" style="padding:10px 20px;background:#ccc;border:none;border-radius:8px;">Réinitialiser</button></div>`;
- return html;
- }
- function chooseClasse(c) {
- classeChoisie = c;
- document.querySelectorAll('.classe-btn').forEach(b => {
- b.style.background = '#eee';
- b.style.color = 'black';
- });
- document.querySelectorAll('.classe-btn')[c - 1].style.background = '#0099cc';
- document.querySelectorAll('.classe-btn')[c - 1].style.color = 'white';
- updatePrixEtCode();
- }
- function choosePassagers(n) {
- nbPassagers = n;
- document.querySelectorAll('.perso-btn').forEach(b => {
- b.style.background = '#eee';
- b.style.color = 'black';
- });
- document.querySelectorAll('.perso-btn')[n - 1].style.background = '#0099cc';
- updatePrixEtCode();
- }
- // ---- Correction : identique à l'acheteur (jour inclus, même hash) ----
- function updatePrixEtCode() {
- if (!horaireAller || (trajetType === 'AR' && !horaireRetour) || !classeChoisie) return;
- // horaires triés
- const horaires = [horaireAller.innerText.split('\n')[0]];
- if (trajetType === 'AR') {
- horaires.push(horaireRetour.innerText.split('\n')[0]);
- }
- horaires.sort();
- // calcul prix inchangé
- const base = (classeChoisie === 1 ? prixBase1 : prixBase2);
- let multDay = 1;
- if (['Vendredi', 'Dimanche'].includes(dayDepart)) multDay = 1.2;
- if (['Mardi', 'Mercredi'].includes(dayDepart)) multDay = 0.9;
- const h = parseInt(horaireAller.innerText);
- let multPeriod = 1;
- if (h >= 6 && h <= 9) multPeriod = 1.1;
- else if (h >= 13 && h <= 16) multPeriod = 0.95;
- const prix = Math.round(base * multDay * multPeriod) * nbPassagers;
- document.getElementById('prix-affiche').innerText = `Tarif : ${prix} €`;
- // génération du hash
- const parts = [
- trajetType === 'AS' ? 'ALLER SIMPLE' : 'ALLER-RETOUR',
- dayDepart.toUpperCase(),
- ...(trajetType === 'AR' ? [dayRetour.toUpperCase()] : []),
- horaires.join('-'),
- classeChoisie,
- prix,
- nbPassagers
- ];
- const str = parts.join('|');
- let hash = 0;
- for (let i = 0; i < str.length; i++) {
- hash = (hash * 31 + str.charCodeAt(i)) % 100000000;
- }
- document.getElementById('code-affiche').innerText = `Numéro de réservation : ${hash.toString(36).toUpperCase()}`;
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement