diff --git a/assets/css/main.css b/assets/css/main.css index 95f43e0055c1238f9ef9fcf1a15c204efc3daca5..943b8872d603ac053d605c066da964b1a62da644 100644 --- a/assets/css/main.css +++ b/assets/css/main.css @@ -94,4 +94,84 @@ nav { margin: auto; font-size: 50px; color: green; -} \ No newline at end of file +} + +.deconnexion { + color: rgba(255, 255, 255, 0.75); + /* margin-inline: 5px; */ + font-size: 20px; +} + +.deconnexion i { + margin-inline: 5px; + vertical-align: middle; +} + +/* Styles pour la liste des cocktails */ +.card-cocktails-liste { + display: flex; + flex-direction: column; + flex-wrap: nowrap; +} + +.card-cocktails { + display: flex; + align-items: center; + border-bottom: 1px solid #ddd; + padding: 10px 0; + margin-bottom: 10px; +} + +.card-cocktail-image { + width: 50px; + height: 50px; + object-fit: cover; + margin-right: 15px; +} + +.card-cocktail-nom { + font-size: 18px; +} + +.card-cocktail-plus { + font-size: 24px; + cursor: pointer; + color: green; + margin-left: auto; +} + +.card-cocktail-plus:hover { + color: #0056b3; +} + +/* Styles pour les cocktails sélectionnés */ +.selected-cocktail { + display: flex; + align-items: center; + border: 1px solid #ddd; + padding: 10px; + margin-bottom: 10px; +} + +.selected-cocktail-image { + width: 50px; + height: 50px; + object-fit: cover; + margin-right: 15px; +} + +.selected-cocktail-nom { + font-size: 18px; +} + +.selected-cocktail-minus { + font-size: 24px; + cursor: pointer; + color: #dc3545; + margin-left: auto; +} + +.selected-cocktail-minus:hover { + color: #c82333; +} + diff --git a/src/Controller/CommandeController.php b/src/Controller/CommandeController.php index a7ba105d960c1294c9640eba870d6e24036c9cd2..d9be83cbcc03fd3939a1731f7c8f5e22c601e0df 100644 --- a/src/Controller/CommandeController.php +++ b/src/Controller/CommandeController.php @@ -6,6 +6,7 @@ use App\Entity\Commande; use App\Entity\Personne; use App\Form\CommandeType; use App\Repository\UserRepository; +use App\Repository\CocktailRepository; use App\Repository\CommandeRepository; use App\Repository\PersonneRepository; use Doctrine\ORM\EntityManagerInterface; @@ -13,6 +14,7 @@ use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Validator\Constraints\Length; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -26,45 +28,44 @@ class CommandeController extends AbstractController } #[Route('/commande/ajouter', name: 'app_commande_ajouter')] - public function nouvelleCommande(Request $request, EntityManagerInterface $entityManager, UserRepository $userRepository): Response + public function nouvelleCommande(Request $request, CocktailRepository $cocktailRepository, EntityManagerInterface $entityManager, UserRepository $userRepository): Response { $user = $this->security->getUser(); - $idPersonne = $user->getId(); + + $userId = $user->getId(); $nomPersonne = $user->getFirstname(); $prenomPersonne = $user->getLastname(); - $commande = new Commande(); - $form = $this->createForm(CommandeType::class, $commande); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $data = $form->getData(); + $data = json_decode($request->getContent(), true); + $cocktailIds = $data['cocktail_ids'] ?? []; - $nbVerres = count($data->getCocktails()); - - $personne = $userRepository->find($idPersonne); - $personne->setNbCommande($personne->getNbCommande() + 1); - $personne->setNbVerres($personne->getNbVerres() + $nbVerres); + if (empty($cocktailIds)) { + return $this->render('commande/nouvelleCommande.html.twig', [ + 'client' => $nomPersonne . ' ' . $prenomPersonne, + 'liste_cocktails' => $cocktailRepository->findAll(), + 'controller_name' => 'Nouvelle Commande', + ]); + } - // Ajoutez l'utilisateur connecté à la commande - $commande->addUser($personne); + $commande = new Commande(); + $commande->setCommandAt(new \DateTimeImmutable()); - $commande->setCommandAt(new \DateTimeImmutable()); + foreach ($cocktailIds as $cocktailId) { + $cocktail = $cocktailRepository->find($cocktailId); + if ($cocktail) { + $commande->addCocktail($cocktail); + } + } - $entityManager->persist($commande); - $entityManager->flush(); + $user = $userRepository->find($userId); + $commande->addUser($user); - return $this->redirectToRoute('app_home'); - } + $entityManager->persist($commande); + $entityManager->flush(); - return $this->render('commande/nouvelleCommande.html.twig', [ - 'client' => $nomPersonne . ' ' . $prenomPersonne, - 'form' => $form->createView(), - 'controller_name' => 'Nouvelle Commande', - ]); + return new JsonResponse(['success' => true, 'message' => 'Commande créée avec succès.']); } - #[Route('/commande/historique', name: 'app_commande_historique')] public function historiqueCommande(Request $request, EntityManagerInterface $entityManager, CommandeRepository $commandeRepository): Response { @@ -74,13 +75,12 @@ class CommandeController extends AbstractController foreach ($commandes as $commande) { $prixTotal = 0; - $nomPersonne = ''; // Initialisez la variable en dehors de la boucle foreach + $nomPersonne = ''; foreach ($commande->getUsers() as $personne) { $nomPersonne .= $personne->getLastname() . ' ' . $personne->getFirstname() . ', '; } - // Supprimez la virgule et l'espace à la fin du nom $nomPersonne = rtrim($nomPersonne, ', '); $nomCocktails = []; diff --git a/templates/base.html.twig b/templates/base.html.twig index 3482cc7cc4fdfecb4834ea3e99e29e1c6bd75705..97ffc8aab82d190ef096a64721231aa20b68407c 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -47,17 +47,19 @@ <li class="nav-item"> <a class="nav-link {{ app.request.get('_route') == 'app_commande_historique' ? 'active' }}" href="{{ path('app_commande_historique') }}">Historique</a> </li> - <li class="nav-item"> - <a class="nav-link {{ app.request.get('_route') == 'app_commande_ajouter' ? 'active' }}" href="{{ path('app_commande_ajouter') }}">Commander</a> - </li> + {% if app.user %} + <li class="nav-item"> + <a class="nav-link {{ app.request.get('_route') == 'app_commande_ajouter' ? 'active' }}" href="{{ path('app_commande_ajouter') }}">Commander</a> + </li> + {% endif %} </ul> <ul class="navbar-nav ms-auto"> {% if app.user %} <li class="nav-item"> <span>{{app.user.lastname}}</span> <span>{{app.user.firstname}} | </span> - <span>{{app.user.roles['0']|replace({'ROLE_': ''})}}</span> - + <span>{{app.user.roles['0']|replace({'ROLE_': ''})}} | </span> + <a class="deconnexion" href="{{ path('app_logout') }}"><i class='bx bx-log-out'></i>Déconnexion</a> </li> {% else %} <li class="nav-item"> diff --git a/templates/commande/nouvelleCommande.html.twig b/templates/commande/nouvelleCommande.html.twig index 67ea60fd0b6e0248b28bfc961c69e6f7a8566f1b..e9eaa53ae4d97092d20799c595b7d5f2964f9059 100644 --- a/templates/commande/nouvelleCommande.html.twig +++ b/templates/commande/nouvelleCommande.html.twig @@ -1,24 +1,47 @@ {% extends 'base.html.twig' %} -{% block title %}{{controller_name}}{% endblock %} +{% block title %}{{ controller_name }}{% endblock %} {% block body %} <div class="container mt-5"> <div class="row justify-content-center"> - <div class="col-md-6"> - <div class="card"> - <div class="card-header"> - <h1 class="text-center my-4">Nouvelle Commande</h1> + <div class="container"> + <div class="row"> + <!-- Liste des cocktails --> + <div class="col-md-6"> + <div class="card"> + <div class="card-header"> + <h1 class="text-center my-4">Choix sur la liste</h1> + </div> + <div class="card-cocktails-liste"> + {% for cocktail in liste_cocktails %} + <div class="card-cocktails d-flex align-items-center mb-3"> + <img class="card-cocktail-image mr-3" src="{{ asset('img/' ~ cocktail.image) }}" alt=""> + <span class="card-cocktail-nom">{{ cocktail.nom }}</span> + <i class='card-cocktail-plus bx bx-plus ml-auto' onclick="addCocktail('{{ cocktail.id }}', '{{ asset('img/' ~ cocktail.image) }}', '{{ cocktail.nom }}')"></i> + </div> + {% endfor %} + </div> + </div> </div> - <div class="card-body"> - {{ form_start(form) }} - <h4>Client : {{client}}</h4> - <div class="form-group mb-3"> - <label for="{{ form.cocktails.vars.id }}">Cocktails</label> - {{ form_widget(form.cocktails, {'attr': {'class': 'form-control'}}) }} + + <!-- Suivi de la commande --> + <div class="col-md-6"> + <div class="card"> + <div class="card-header"> + <h1 class="text-center my-4">Nouvelle Commande</h1> + </div> + <div class="card-body"> + <h4>Client : {{ client }}</h4> + <div id="order-summary" class="form-group mb-3"> + <label for="order-summary">Cocktails sélectionnés</label> + <div id="selected-cocktails" class="list-group"> + <!-- Les cocktails sélectionnés seront ajoutés ici --> + </div> + </div> + <button type="button" class="btn btn-primary" onclick="submitForm()">Create</button> + </div> </div> - <button type="submit" class="btn btn-primary">Create</button> - {{ form_end(form) }} </div> </div> </div> @@ -26,9 +49,78 @@ </div> <script> - $(document).ready(function() { - $('#commande_personnes').select2(); - $('#commande_cocktails').select2(); - }); + let selectedCocktailIds = []; + + function addCocktail(id, image, nom) { + const selectedCocktails = document.getElementById('selected-cocktails'); + + if (document.getElementById('selected-' + id)) { + return; + } + + selectedCocktailIds.push(id); + + const cocktailDiv = document.createElement('div'); + cocktailDiv.className = 'selected-cocktail d-flex align-items-center'; + cocktailDiv.id = 'selected-' + id; + + const img = document.createElement('img'); + img.src = image; + img.className = 'selected-cocktail-image'; + cocktailDiv.appendChild(img); + + const span = document.createElement('span'); + span.className = 'selected-cocktail-nom'; + span.innerText = nom; + cocktailDiv.appendChild(span); + + const icon = document.createElement('i'); + icon.className = 'selected-cocktail-minus bx bx-minus ml-auto'; + icon.onclick = function() { removeCocktail(id); }; + cocktailDiv.appendChild(icon); + + selectedCocktails.appendChild(cocktailDiv); + } + + function removeCocktail(id) { + const cocktailDiv = document.getElementById('selected-' + id); + if (cocktailDiv) { + cocktailDiv.remove(); + selectedCocktailIds = selectedCocktailIds.filter(cocktailId => cocktailId !== id); + } + } + + function submitForm() { + if (selectedCocktailIds.length === 0) { + alert('Veuillez sélectionner au moins un cocktail.'); + return; + } + + const data = { + cocktail_ids: selectedCocktailIds + }; + + fetch('{{ path('app_commande_ajouter') }}', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRF-Token': '{{ csrf_token('commande') }}' + }, + body: JSON.stringify(data) + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + alert('Commande créée avec succès!'); + window.location.href = '{{ path('app_home') }}'; + } else { + alert('Erreur lors de la création de la commande.'); + } + }) + .catch(error => { + console.error('Erreur:', error); + alert('Erreur lors de la création de la commande.'); + }); + } </script> {% endblock %}