<?php

namespace Modules\Parametrage\Repository;

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

class ParametrageLAD extends AbsRepository
{
    /**
     * listeParamFournisseur
     * @param string $fournisseur
     * @param array<string> $post
     * @return array<mixed>
     */
    public function listeParamFournisseur(string $fournisseur = '', array $post = []) : array
    {
        $arrayReq = [];
        $req = "SELECT CODE, RS, ARTICLE_INIT_FAC_LAD, 
                    (CASE 
                        WHEN ISNULL(t.DES_ART, '') = ''
                        THEN ARTICLES.DESIGN
                        ELSE t.DES_ART
                    END) AS DESIGN
                FROM FOURNISS with(nolock)
                LEFT JOIN ARTICLES with(nolock) on CODE_ART = FOURNISS.ARTICLE_INIT_FAC_LAD
                LEFT JOIN TRAD_ART t WITH(NOLOCK) ON ARTICLES.CODE_ART = t.COD_ART AND t.COD_LNG = '{$_SESSION['Session_Langue']}' ";

        if ($fournisseur != '') {
            $req .=" WHERE CODE = :CODE";
            $arrayReq = [
                ':CODE' => $fournisseur
            ];
            
            $requete = $this->pdo->prepare($req);
            $requete->execute($arrayReq);
        
            return  $requete->fetch();
        }
        $whereArray = FilterTable::splitFilter($post);
        
        if (count($whereArray)>  0 &&  isset($whereArray[1])) {
            $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);
    }
    
    /**
     * @param array<mixed> $post
     */
    public function listeFournisseurExport(array $post) : array
    {
        $arrayReq = [];
        $req = "SELECT CODE, ARTICLE_INIT_FAC_LAD
                FROM FOURNISS";

        $whereArray = FilterTable::splitFilter($post);
                
        if (count($whereArray)>  0 &&  isset($whereArray[1])) {
            $req .= $whereArray[0];
            if (count($whereArray[1]) != 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }

        $req .= " ORDER BY CODE";

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * @param array<mixed> $arrayInsert
     */
    public function updateFournisseurArticle(array $arrayInsert) : bool
    {
        $arrayReq = [];
        $req = "UPDATE FOURNISS 
                SET ARTICLE_INIT_FAC_LAD = :article 
                WHERE CODE = :code";
        
        $arrayReq = [
            ':article'  => $arrayInsert['ARTICLE_INIT_FAC_LAD']??'',
            ':code'     => $arrayInsert['CODE'],
        ];
        
        $requete = $this->pdo->prepare($req);
        return $requete->execute($arrayReq);
    }
}
