<?php

namespace Modules\Fournisseur\Repository;

use Helpers\Outil\Sql;
use Helpers\Filter\FilterTable;
use Helpers\QueryBuilder\AbsRepository;

class Groupement extends AbsRepository
{
    /**
     * listeGroupement
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function listeGroupement(array $post) : ?array
    {
        $req = "SELECT CODE, RS, CPOSTAL, VILLE1, COUNTRY.LIBELLE, COND_PAIE, MODE_PAIE, LIV, AUTOCC, 
                MODELIVR.LIBELLE LIB_LIV, MODEPAIE.LIBELLE LIB_MODEPAIE, CONDPAIE.LIBELLE LIB_COND_PAIE
                from GRPFOU with (nolock)
                left join COUNTRY with (nolock) on COUNTRY.CODE_PAYS = GRPFOU.CODE_PAYS
                left join MODELIVR with (nolock) on MODELIVR.CODE_MDL = GRPFOU.LIV
                left join MODEPAIE with (nolock) on MODEPAIE.CODE_MDP = GRPFOU.MODE_PAIE
                left join CONDPAIE with (nolock) on CONDPAIE.CODE_CDP = GRPFOU.COND_PAIE
                where 1=1";
        $arrayReq=[];
        
        if (isset($post['filter_CODE']) && $post['filter_CODE']) {
            $req .= ' and UPPER(CODE) LIKE UPPER(:value)';
            $arrayReq[':value']='%'.$post['filter_CODE'].'%';
        }
        if (isset($post['filter_RS']) && $post['filter_RS']) {
            $req .= ' and UPPER(RS) LIKE UPPER(:value1)';
            $arrayReq[':value1']='%'.$post['filter_RS'].'%';
        }
        if (isset($post['filter_CPOSTAL']) && $post['filter_CPOSTAL']) {
            $req .= ' and UPPER(CPOSTAL) LIKE UPPER(:value2)';
            $arrayReq[':value2']='%'.$post['filter_CPOSTAL'].'%';
        }
        if (isset($post['filter_VILLE1']) && $post['filter_VILLE1']) {
            $req .= ' and UPPER(VILLE1) LIKE UPPER(:value3)';
            $arrayReq[':value3']='%'.$post['filter_VILLE1'].'%';
        }
        if (isset($post['filter_LIBELLE']) && $post['filter_LIBELLE']) {
            $req .= ' AND UPPER(COUNTRY.LIBELLE) LIKE UPPER(:value4)';
            $arrayReq[':value4']='%'.$post['filter_LIBELLE'].'%';
        }
        
        if (isset($post['filter_COND_PAIE']) && $post['filter_COND_PAIE']) {
            $req .= ' AND UPPER(COND_PAIE) = :value5';
            $arrayReq[':value5']=$post['filter_COND_PAIE'];
        }
        if (isset($post['filter_MODE_PAIE']) && $post['filter_MODE_PAIE']) {
            $req .= ' AND UPPER(MODE_PAIE) = :value6';
            $arrayReq[':value6']=$post['filter_MODE_PAIE'];
        }
        if (isset($post['filter_LIV']) && $post['filter_LIV']) {
            $req .= ' AND UPPER(LIV) = :value7';
            $arrayReq[':value7']=$post['filter_LIV'];
        }

        if (isset($post['filter_AUTOCC']) && $post['filter_AUTOCC']) {
            $value= $post['filter_AUTOCC']=='oui'? Sql::setBoolSql(1):Sql::setBoolSql(0);
            $req .= ' AND UPPER(AUTOCC) = '.$value.' ';
        }
        
        $whereArray = FilterTable::splitFilter($post);
        $req .= $whereArray['tri']??'';

        $paginatorGestion = new \Helpers\Pagination\PaginatorGestion();
        $paginatorGestion->injecterSqlCount($req, $post, $arrayReq, $this->pdo);

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * ficheGroupement
     * @param string $code
     * @return array<mixed>
     */
    public function ficheGroupement($code) : ?array
    {
        $req = "select * from GRPFOU with (nolock) where CODE = :value";
        $arrayReq[':value']=$code;
                
        $groupement = $this->pdo->prepare($req);
        $groupement->execute($arrayReq);
        
        return $groupement->fetch($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * contact
     * @param string $code
     * @return array<mixed>
     */
    public function contact(string $code) : ?array
    {
        $req = "select * from CONTGF with (nolock) where CODE_GF = :value";
        $arrayReq[':value']=$code;
        
        $groupement = $this->pdo->prepare($req);
        $groupement->execute($arrayReq);

        return $groupement->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * fournisSecondaire
     * @param string $code
     * @param array<mixed> $post
     * @param mixed $export
     * @return array<mixed>
     */
    public function fournisSecondaire(string $code, array $post, $export = false) : ?array
    {
        $req = "select CODE,RS, VILLE1, CPOSTAL, LIBELLE from FOURNISS with (nolock) left join COUNTRY with (nolock) on COUNTRY.CODE_PAYS = FOURNISS.CODE_PAYS where FOURPRIN = :value";
        $arrayReq[':value']=$code;
        
        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);

        if (count($whereArray)>  0) {
            $tri = $whereArray['tri']??'';
            
            $req .= $whereArray[0].$tri;
            
            
            if (count($whereArray[1]) != 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }
        
        $groupement = $this->pdo->prepare($req);
        $groupement->execute($arrayReq);
        
        return $groupement->fetchAll($this->pdo::FETCH_ASSOC);
    }
}
