<?php

namespace Modules\Outil\Repository;

use Entity\Entity;
use Helpers\QueryBuilder\AbsRepository;

class Multiselect extends AbsRepository
{
    /**
     * getResultEntityDynamique
     *
     * @param  Entity $entity
     * @param  string $where
     * @param  string $notIn
     * @param  array<mixed> $arrayReq
     * @param   array<mixed>  $post
     * @return  array<mixed>|null
     */
    public function getResultEntityDynamique($entity, $where = '', string $notIn = '', array $arrayReq = [], array $post = [], string $clauseWhere = '') : ?array
    {
        $where = trim($where);

        if ($entity->getPrimaryKey() == $entity->getLibelle()) {
            $select = $entity->getPrimaryKey();
        } else {
            $select = $entity->getPrimaryKey().", ".$entity->getLibelle();
        }

        $req = "SELECT ".$select."
        FROM ".$entity->getNameTable();
        $i=0;
        $passageWhere = 0;
        if ($where != '') {
            $passageWhere = 1;
            $i=2;
            $req .=" WHERE (upper(".$entity->getPrimaryKey().") like :where1 OR upper(".$entity->getLibelle().") like :where2) ";
            $arrayReq[':where1'] = '%'.mb_strtoupper($where).'%';
            $arrayReq[':where2'] = '%'.mb_strtoupper($where).'%';
        }

        if ($notIn != '') {
            $notIn = explode('|', substr($notIn, 1));
            if ($passageWhere == 1) {
                $req .= " AND ";
            } else {
                $req .= " WHERE ";
            }
            $passageWhere = 1;
            $req .=$entity->getPrimaryKey()." not in (";
            $passageNotInt=0;
            foreach ($notIn as $not) {
                $i++;
                if ($passageNotInt==0) {
                    $passageNotInt++;
                    $req .=" :where".$i;
                } else {
                    $req .=", :where".$i;
                }
                $arrayReq[':where'.$i] = $not;
            }
            
            
            $req .=") ";
        }

        if ($clauseWhere) {
            if ($passageWhere == 1) {
                $req .= " AND ";
            } else {
                $req .= " WHERE ";
            }
            $passageWhere = 1;

            $req .= $clauseWhere;
        }

        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, $passageWhere);
        if (isset($whereArray['tri'])) {
            $req .= $whereArray[0].$whereArray['tri'];

            if (count($whereArray[1]) != 0) {
                $arrayReq = array_merge($arrayReq, $whereArray[1]);
            }
        }
        
        $paginatorGestion = new \Helpers\Pagination\PaginatorGestion();
        $paginatorGestion->injecterSqlCount($req, $post, $arrayReq, $this->pdo);

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * getRightValue
     *
     * @param  Entity $entity
     * @param  array<mixed> $arrayMultiSelect
     * @param  array<mixed> $post
     * @return array<mixed>
     */
    public function getRightValue($entity, $arrayMultiSelect, $post)
    {
        $arrayReq = [];
        if ($entity->getPrimaryKey() == $entity->getLibelle()) {
            $select = $entity->getPrimaryKey();
        } else {
            $select = $entity->getPrimaryKey().", ".$entity->getLibelle();
        }

        $req = "SELECT ".$select."
        FROM ".$entity->getNameTable();
        $i=0;

        $req .=" where ".$entity->getPrimaryKey()." in (";
        $passageNotInt=0;
        foreach ($arrayMultiSelect as $selected) {
            $i++;
            if ($passageNotInt==0) {
                $passageNotInt++;
                $req .=" :where".$i;
            } else {
                $req .=", :where".$i;
            }
            $arrayReq[':where'.$i] = $selected;
        }

        $req .= ")";

        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
        if (isset($whereArray['tri'])) {
            $req .= $whereArray[0].$whereArray['tri'];

            if (count($whereArray[1]) != 0) {
                $arrayReq = array_merge($arrayReq, $whereArray[1]);
            }
        }

        return $this->fetchAssocAll($req, $arrayReq);
    }
}
