<?php

namespace App\Controller;

use App\Entity\StatutCommandes;
use App\Form\StatutCommandesType;
use App\Repository\StatutCommandesRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/statut/commandes')]
final class StatutCommandesController extends AbstractController
{
    #[Route(name: 'app_statut_commandes_index', methods: ['GET'])]
    public function index(StatutCommandesRepository $statutCommandesRepository): Response
    {
        return $this->render('statut_commandes/index.html.twig', [
            'statut_commandes' => $statutCommandesRepository->findAll(),
        ]);
    }

    #[Route('/new', name: 'app_statut_commandes_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager): Response
    {
        $statutCommande = new StatutCommandes();
        $form = $this->createForm(StatutCommandesType::class, $statutCommande);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->persist($statutCommande);
            $entityManager->flush();

            return $this->redirectToRoute('app_statut_commandes_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('statut_commandes/new.html.twig', [
            'statut_commande' => $statutCommande,
            'form' => $form,
        ]);
    }


    #[Route('/{id}/edit', name: 'app_statut_commandes_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, StatutCommandes $statutCommande, EntityManagerInterface $entityManager): Response
    {
        $form = $this->createForm(StatutCommandesType::class, $statutCommande);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->flush();

            return $this->redirectToRoute('app_statut_commandes_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('statut_commandes/edit.html.twig', [
            'statut_commande' => $statutCommande,
            'form' => $form,
        ]);
    }

    #[Route('/{id}', name: 'app_statut_commandes_delete', methods: ['POST'])]
    public function delete(Request $request, StatutCommandes $statutCommande, EntityManagerInterface $entityManager): Response
    {
        if ($this->isCsrfTokenValid('delete'.$statutCommande->getId(), $request->getPayload()->getString('_token'))) {
            $entityManager->remove($statutCommande);
            $entityManager->flush();
        }

        return $this->redirectToRoute('app_statut_commandes_index', [], Response::HTTP_SEE_OTHER);
    }
}
