<?php

namespace App\Repository;

use App\Entity\TourRequest;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Pagerfanta\Pagerfanta;
use Doctrine\ORM\Query;
use Pagerfanta\Adapter\DoctrineORMAdapter;

/**
 * @method TourRequest|null find($id, $lockMode = null, $lockVersion = null)
 * @method TourRequest|null findOneBy(array $criteria, array $orderBy = null)
 * @method TourRequest[]    findAll()
 * @method TourRequest[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class TourRequestRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, TourRequest::class);
    }

    public function getAllTourRequest(int $page = 1,  $tag): Pagerfanta
    {
        // Create our query
        $query = $this->createQueryBuilder('c')
                        ->where('c.placeVisit LIKE  :val')
                        ->andWhere('c.tourPurpose LIKE  :val')                                                  
                        ->setParameter('val','%'.$tag.'%')                    
                        ->orderBy('c.id', 'DESC')
                        ->getQuery();
        
        // No need to manually get get the result ($query->getResult())

        // $paginator = $this->paginate($query, $currentPage);

         return $this->createPaginator($query, $page);
    }

    private function createPaginator(Query $query, int $page): Pagerfanta
    {
        $paginator = new Pagerfanta(new DoctrineORMAdapter($query));
        $paginator->setMaxPerPage(TourRequest::NUM_ITEMS);
        $paginator->setCurrentPage($page);

        return $paginator;
    }

    // /**
    //  * @return TourRequest[] Returns an array of TourRequest objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('t')
            ->andWhere('t.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('t.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?TourRequest
    {
        return $this->createQueryBuilder('t')
            ->andWhere('t.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
