<?php
namespace Modules\Fournisseur\Repository;

class ShortList
{
    
    private \PDO $pdo;
    
    /**
     * __construct
     * @param \PDO $pdo
     */
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * ListeShortList
     * @param array<mixed> $post
     * @param mixed $export
     * @return mixed
     */
    // phpcs:ignore
    public function ListeShortList(array $post, $export = false)
    {
        if ($export) {
            $alias="CODLST CODE, LIBELLE, COMM COMMENTAIRE, DTE_DEB DATE_DEBUT, DTE_FIN DATE_FIN";
        } else {
            $alias="*";
        }
        
        $req = "select ".$alias." from LST_FOURN with (nolock)";

        $arrayReq = [];
        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 0);
        if (count($whereArray)>  0) {
            $req .= $whereArray[0].$whereArray['tri'];
            
            if (count($whereArray[1]) != 0) {
                $arrayReq = array_merge($arrayReq, $whereArray[1]);
            }
        }


        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
        
        return  $shortList->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * CreationShortList
     * @param array<mixed> $post
     * @return void
     */
    // phpcs:ignore
    public function CreationShortList(array $post) : void
    {
        $req = "INSERT INTO LST_FOURN (LIBELLE, COMM, DTE_DEB, DTE_FIN) VALUES(:value1, :value2, :value3, :value4)";

        $arrayReq = [
            ':value1' => $post['LIBELLE'],
            ':value2' => $post['COMM'],
            ':value3' => $post['DTE_DEB'],
            ':value4' => $post['DTE_FIN'],
        ];

        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
    }
    
    /**
     * UpdateShortList
     * @param string $code
     * @param array<mixed> $post
     * @return void
     */
    // phpcs:ignore
    public function UpdateShortList(string $code, array $post) : void
    {
        $req = "UPDATE LST_FOURN SET LIBELLE=:value1, COMM=:value2, DTE_DEB=:value3, DTE_FIN=:value4 
        WHERE CODLST=".trim($code)."";
        
        $arrayReq = [
            ':value1' => $post['LIBELLE'],
            ':value2' => $post['COMM'],
            ':value3' => $post['DTE_DEB'],
            ':value4' => $post['DTE_FIN'],
        ];
        
        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
    }
    
    /**
     * shortListExiste
     * @return array<mixed>
     */
    public function shortListExiste() : ?array
    {
        $req = "select * from LST_FOURN lst with(nolock) 
        where lst.DTE_DEB <= :date1 and (lst.DTE_FIN = '00000000' or lst.DTE_FIN >= :date2) 
        and exists (
            select TOP 1 * from FOU_LSTFOURN foulst with(nolock) where foulst.CODLST = lst.CODLST
        )";
        
        $arrayReq = [
            ':date1' => date('Ymd'),
            ':date2' => date('Ymd')
        ];
        
        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
        
        return  $shortList->fetchAll($this->pdo::FETCH_ASSOC);
    }
}
