<?php

namespace App\Controller;

use App\Entity\CategoryPage;
use App\Form\CategoryPageType;
use App\Repository\CategoryPageRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

/**
 * @IsGranted("ROLE_ADMIN")
 * @Route("/category/page")
 */
class CategoryPageController extends AbstractController
{
    /**
     * @Route("/", name="category_page_index", methods={"GET"})
     */
    public function index(CategoryPageRepository $categoryPageRepository): Response
    {
        return $this->render('category_page/index.html.twig', [
            'category_pages' => $categoryPageRepository->findAll(),
        ]);
    }

    /**
     * @Route("/new", name="category_page_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $categoryPage = new CategoryPage();
        $form = $this->createForm(CategoryPageType::class, $categoryPage);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($categoryPage);
            $entityManager->flush();

            return $this->redirectToRoute('category_page_index');
        }

        return $this->render('category_page/new.html.twig', [
            'category_page' => $categoryPage,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="category_page_show", methods={"GET"})
     */
    public function show(CategoryPage $categoryPage): Response
    {
        return $this->render('category_page/show.html.twig', [
            'category_page' => $categoryPage,
        ]);
    }

    /**
     * @Route("/{id}/edit", name="category_page_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, CategoryPage $categoryPage): Response
    {
        $form = $this->createForm(CategoryPageType::class, $categoryPage);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('category_page_index');
        }

        return $this->render('category_page/edit.html.twig', [
            'category_page' => $categoryPage,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="category_page_delete", methods={"POST"})
     */
    public function delete(Request $request, CategoryPage $categoryPage): Response
    {
        if ($this->isCsrfTokenValid('delete'.$categoryPage->getId(), $request->request->get('_token'))) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($categoryPage);
            $entityManager->flush();
        }

        return $this->redirectToRoute('category_page_index');
    }
}
