<?php

namespace Modules\Fournisseur\Repository;

use Helpers\Pagination\PaginatorGestion;
use Helpers\QueryBuilder\AbsRepository;

class Notation extends AbsRepository
{
    
    /**
     * listeAnneNotation
     * @param string $code
     * @param array<mixed> $post
     * @return array<array<mixed>>
     */
    public function listeAnneNotation(string $code, array $post = []) : ?array
    {
        $req = "select COD_FOUR, COD_PER, NOTMOYFO, LIBELLE from FOU_NOT with (nolock)
                LEFT JOIN PERIODES with (nolock) on  PERIODES.CODE_PER = FOU_NOT.COD_PER  
                WHERE COD_FOUR=:value";
        
        $arrayReq[':value']=$code;

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

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * listeCritere
     * @param string $code
     * @param string $annee
     * @return array<mixed>
     */
    public function listeCritere(string $code, string $annee) : ?array
    {
        $req = "SELECT FOU_CRIT.COD_THEM, AVG(NOMOYCRIT) NOMOYCRIT, THEMES.LIBELLE
                FROM CRITERES with (nolock)
                LEFT JOIN FOU_CRIT with (nolock) on FOU_CRIT.COD_THEM = CRITERES.COD_THEM 
                LEFT JOIN THEMES with (nolock) on THEMES.COD_THEM = CRITERES.COD_THEM
                WHERE COD_FOU=:value AND COD_PER=:value1 
                GROUP BY FOU_CRIT.COD_THEM, THEMES.LIBELLE";

        $arrayReq=[
            ':value' => $code,
            ':value1' => $annee,
        ];

        $req = $this->applyFilter($req, $arrayReq, []);

        (new PaginatorGestion())->injecterSqlCount($req, [], $arrayReq);
        
        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * critereFournisseur
     * @param string $code
     * @param string $annee
     * @param string $critere
     * @return array<mixed>
     */
    public function critereFournisseur(string $code, string $annee, string $critere) : ?array
    {
        $code = trim(addslashes($code));
        $annee = trim(addslashes($annee));
        
        $req = "select CRITERES.COD_THEM, CRITERES.COD_CRIT, LIBELLE,COEFPOND, OBLIGAT, DTEDEBVAL, DTEFINVAL, COD_FOU, COD_PER, CRITERES.COD_THEM, CRITERES.COD_CRIT, NOMOYCRIT, NBNOTE, COEFPOND 
                from CRITERES with (nolock)
                FULL JOIN (select  * from FOU_CRIT with (nolock) where COD_FOU='".$code."' And COD_PER='".$annee."') as FOU_CRIT on FOU_CRIT.COD_CRIT = CRITERES.COD_CRIT and FOU_CRIT.COD_THEM = CRITERES.COD_THEM
                WHERE CRITERES.COD_THEM = :value";
        
        $arrayReq[':value']=$critere;
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
}
