<?php

namespace App\Repository;

use App\Entity\Equipement;
use App\Entity\EquipementOption;
use App\Entity\Option;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function findAll( array $orderBy = ['libelle_court' => 'ASC'] )
    {
        return $this->findBy([], $orderBy);
    }

    // /**
    //  * @return Option[] Returns an array of Option objects
    //  */

    public function findAllArray()
    {
        return $this->createQueryBuilder('o')
            ->orderBy('o.libelle_court', 'ASC')
            ->getQuery()
            ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY)
        ;
    }

    public function findAllByIndexLibelle()
    {
        $qb = $this->createQueryBuilder('o');
        $query = $qb->indexBy('o', 'o.libelle_court')->getQuery();
        return $query->getResult();
    }


    public function getAllIdNom()
    {
        $qb = $this->createQueryBuilder('s')->select('s.libelle_court as value,s.libelle_court as text');
        return $qb->getQuery()->getArrayResult();
    }

    public function getOptionsByType($idType) {
        return $this->createQueryBuilder('eo')
            ->select()
            ->join('eo.equipementTypeOptions', 'eto')
            ->join('eto.equipement_type', 'et')
            ->andWhere('et.id=:idType')
            ->setParameter('idType', $idType)
            ->getQuery()
            ->getResult()
            ;
    }

    public function getIdLibelle()
    {
        $qb = $this->createQueryBuilder('s')->select('s.libelle_court,s.id');
        return $qb->getQuery()->getArrayResult();
    }
}
