<?php

namespace App\Controller;

use App\Entity\StatutTables;
use App\Form\StatutTablesType;
use App\Repository\StatutTablesRepository;
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/tables')]
final class StatutTablesController extends AbstractController
{
    #[Route(name: 'app_statut_tables_index', methods: ['GET'])]
    public function index(StatutTablesRepository $statutTablesRepository): Response
    {
        return $this->render('statut_tables/index.html.twig', [
            'statut_tables' => $statutTablesRepository->findAll(),
        ]);
    }

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

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

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

        return $this->render('statut_tables/new.html.twig', [
            'statut_table' => $statutTable,
            'form' => $form,
        ]);
    }


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

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

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

        return $this->render('statut_tables/edit.html.twig', [
            'statut_table' => $statutTable,
            'form' => $form,
        ]);
    }

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

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