<?php

namespace AppBundle\Controller;

use adminBundle\Entity\Category;
use Doctrine\ORM\Query\AST\WhereClause;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class CategoriesController extends Controller
{
    /**
     * @Route("/categories/{id}/{page}",
     *     name="voir_categories",
     *     requirements={"id" = "\d+", "page" = "\d+"},
     *     defaults={"page"=1}
     * )
     */
    public function indexAction($id, $page, Category $categorie, Request $request) {
        $em = $this->getDoctrine()->getManager();
        $cat = $em->getRepository('adminBundle:Category')->find($id);


        $offset = ($page - 1) * 4;

        $nbrePage = ceil($em->getRepository('adminBundle:Product')->nbreProducts($id) / 4);

        //die(dump($page, $offset));

  	    $products = $em->getRepository('adminBundle:Product')->myFindProductSelonCategorie($id, $offset, 4);
        //die(dump($page, $offset, $products));

        return $this->render('Public/Categories/Categories.html.twig', [
            'products' => $products,
            'pages' => $nbrePage,
            'pageActive' => $page,
            'categoryId' => $id,
        ]);
    }
}