<?php

namespace Modules\Fournisseur\Repository;

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

    /**
     * ListeDistribuees
     * @param string $id
     * @param array<mixed> $post
     * @param mixed $export
     * @return array<mixed>
     */
    // phpcs:ignore
    public function ListeDistribuees(string $id, array $post, $export = false) : ?array
    {
        if ($export) {
            $alias="*";
        } else {
            $alias="*";
        }
        
        $req = "SELECT $alias, REMGAMM.CODEGAMME ,REMGAMM.REMISE ,REMGAMM.CODEFABR, GAMMES.DESIGN, FABRICAN.RS FROM REMGAMM with (nolock)
                LEFT JOIN GAMMES with (nolock) on GAMMES.CODEGAMME = REMGAMM.CODEGAMME and GAMMES.CODEFABR = REMGAMM.CODEFABR
                LEFT JOIN FABRICAN with (nolock)on FABRICAN.CODE=REMGAMM.CODEFABR
                WHERE CODEFOURN=:value1";

        $arrayReq = [':value1' => $id];
        
        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
        
        if (count($whereArray)>  0) {
            $whereArray[0] = str_replace('CODEFABR', 'GAMMES.CODEFABR', $whereArray[0]);
            $whereArray[0] = str_replace('CODEGAMME', 'GAMMES.CODEGAMME', $whereArray[0]);
            
            // @phpstan-ignore-next-line
            $req .= $whereArray[0].$whereArray['tri'];
            // @phpstan-ignore-next-line
            if (count($whereArray[1]) != 0) {
                // @phpstan-ignore-next-line
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }

        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
        
        return  $distribuees->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * detailDistribuees
     * @param string $id
     * @param string $codeFabr
     * @param string $codeGamme
     * @return array<mixed>
     */
    public function detailDistribuees(string $id, string $codeFabr, string $codeGamme) : ?array
    {
        $req="SELECT NOORDRPRE, REMISE FROM LIGREMGA with (nolock) WHERE CODEFOURN=:value1 AND CODEFABR=:value2 AND CODEGAMME=:value3";
        
        $arrayReq = [
            ':value1' => $id,
            ':value2' => $codeFabr,
            ':value3' => $codeGamme,
        ];
        
        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
        
        return  $distribuees->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * ajouterGamme
     * @param string $code
     * @param array<mixed> $post
     * @return void
     */
    public function ajouterGamme(string $code, array $post) : void
    {
        $req = "INSERT INTO REMGAMM VALUES(:code, :value1, :value2, :value3)";
        
        $arrayReq = [
            ':code' => $code,
            ':value1' => $post['CODEFABR'],
            ':value2' => $post['CODEGAMME'],
            ':value3' => $post['REMISE'],
        ];
        
        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
    }
    
    /**
     * ajouterRemise
     * @param string $code
     * @param array<mixed> $post
     * @param array<mixed> $remise
     * @return void
     */
    public function ajouterRemise(string $code, array $post, array $remise) : void
    {
        $i=1;
        foreach ($remise as $value) {
            $req = "INSERT INTO LIGREMGA VALUES (:code, :value1, :value2, ".$i.", ".$value['NOORDRPRE'].", :value3)";
            
            $arrayReq = [
                ':code' => $code,
                ':value1' => $post['CODEFABR'],
                ':value2' => $post['CODEGAMME'],
                ':value3' => $value['REMISE'],
            ];
            
            $distribuees = $this->pdo->prepare($req);
            $distribuees->execute($arrayReq);
            
            $i++;
        }
    }
    
    /**
     * gammeExistFabricant
     * @param string $gamme
     * @param string $fabricant
     * @return array<mixed>
     */
    public function gammeExistFabricant(string $gamme, string $fabricant) : ?array
    {
        $req = "SELECT CODEFABR, CODEGAMME, DESIGN FROM GAMMES with (nolock) WHERE CODEFABR=:value1 AND CODEGAMME=:value2";
        
        $arrayReq = [
            ':value1' => $fabricant,
            ':value2' => $gamme,
        ];

        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
        
        return  $distribuees->fetch();
    }
    
    /**
     * selectGamme
     * @param string $id
     * @param string $fabricant
     * @param string $gamme
     * @return array<mixed>
     */
    public function selectGamme(string $id, string $fabricant, string $gamme) : ?array
    {
        $req = 'SELECT CODEFABR, CODEGAMME, CODEFOURN, REMISE FROM REMGAMM with (nolock) WHERE CODEFOURN=:value1 and CODEFABR=:value2 and CODEGAMME=:value3';
        
        $arrayReq = [
            ':value1' => $id,
            ':value2' => $fabricant,
            ':value3' => $gamme,
        ];
        
        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
        
        return  $distribuees->fetch();
    }
    
    /**
     * selectRemise
     * @param string $id
     * @param string $fabricant
     * @param string $gamme
     * @return array<mixed>
     */
    public function selectRemise(string $id, string $fabricant, string $gamme) : ?array
    {
        $req = 'SELECT CODEFOURN, CODEFABR, CODEGAMME, NOORDEUNI, NOORDRPRE, REMISE FROM LIGREMGA with (nolock) WHERE CODEFOURN=:value1 and CODEFABR=:value2 and CODEGAMME=:value3';
        
        $arrayReq = [
            ':value1' => $id,
            ':value2' => $fabricant,
            ':value3' => $gamme,
        ];
        
        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
        
        return  $distribuees->fetchAll();
    }
    
    /**
     * updateGamme
     * @param string $code
     * @param string $fabricant
     * @param string $gamme
     * @param array<mixed> $post
     * @return void
     */
    public function updateGamme(string $code, string $fabricant, string $gamme, array $post) : void
    {
        $req = 'UPDATE REMGAMM SET REMISE=:value4 WHERE CODEFOURN=:value1 and CODEFABR=:value2 and CODEGAMME=:value3';
        
        $arrayReq = [
            ':value4' => $post['REMISE'],
            ':value1' => $code,
            ':value2' => $fabricant,
            ':value3' => $gamme,
        ];
        
        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
    }
    
    /**
     * suppresionRemise
     * @param string $code
     * @param string $fabricant
     * @param string $gamme
     * @return void
     */
    public function suppresionRemise(string $code, string $fabricant, string $gamme) : void
    {
        $req = 'DELETE FROM LIGREMGA WHERE CODEFOURN=:value1 and CODEFABR=:value2 and CODEGAMME=:value3';
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $fabricant,
            ':value3' => $gamme,
        ];
        
        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
    }
    
    /**
     * suppresionGamme
     * @param string $code
     * @param string $fabricant
     * @param string $gamme
     * @return void
     */
    public function suppresionGamme(string $code, string $fabricant, string $gamme) : void
    {
        $req = 'DELETE FROM REMGAMM WHERE CODEFOURN=:value1 and CODEFABR=:value2 and CODEGAMME=:value3';
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $fabricant,
            ':value3' => $gamme,
        ];
        
        $distribuees = $this->pdo->prepare($req);
        $distribuees->execute($arrayReq);
    }
}
