<?php

namespace Modules\Fournisseur\Repository;

use Helpers\QueryBuilder\AbsRepository;

class MotsClefs extends AbsRepository
{

    
    /**
     * listeMotClef
     * @param string $code
     * @param array<mixed> $post
     * @param mixed $export
     * @return array<mixed>
     */
    public function listeMotClef(string $code, array $post = [], $export = false) : ?array
    {
        if (!$export) {
            $req = "SELECT * FROM CLEFFOUR with (nolock) WHERE COD_FOUR=:value";
        } else {
            $req = "SELECT MOTCLEF FROM CLEFFOUR with (nolock) WHERE COD_FOUR=:value";
        }
        $arrayReq[':value'] = $code;
        
        if (isset($post['filter_MOTCLEF']) && $post['filter_MOTCLEF']) {
            $req .=" and UPPER(MOTCLEF) LIKE :value3";
            $arrayReq[':value3'] = strtoupper('%'.$post['filter_MOTCLEF'].'%');
        }
        
        $motClef = $this->pdo->prepare($req);
        $motClef->execute($arrayReq);
        return $motClef->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * ListeCreationMotClef
     * @param array<mixed> $liste
     * @param array<mixed> $post
     * @return array<mixed>
     */
    // phpcs:ignore
    public function ListeCreationMotClef(array $liste, array $post = []) : ?array
    {
        $arrayReq=[];
        
        if (count($liste)>=1) {
            $array=[];
            foreach ($liste as $motclef) {
                if ($motclef['ETAT'] != 'S') {
                    array_push($array, $motclef['MOTCLEF']);
                }
            }
            $array = "'".implode("','", $array)."'";
            
            $value = "  and MOTCLEF in (".$array.")";
        } else {
            $value = " AND 1=2";
        }
            
        $req="SELECT 
            (CASE 
                WHEN ISNULL(t.TRA_MOT, '') = ''
                THEN MOTCLEF.MOTCLEF 
                ELSE t.TRA_MOT
            END) AS MOTCLEF
        FROM MOTCLEF WITH (NOLOCK) 
            LEFT OUTER JOIN TRAD_MCLE t WITH(NOLOCK) ON t.MOT_CLE = MOTCLEF.MOTCLEF AND t.COD_LNG = '{$_SESSION['Session_Langue']}'
        WHERE 1=1 ".$value."";
        
        if (isset($post['filter_MOTCLEF']) && $post['filter_MOTCLEF']) {
            $req .=" and upper(MOTCLEF.MOTCLEF) LIKE :value";
            $arrayReq[':value'] = strtoupper('%'.$post['filter_MOTCLEF'].'%');
        }
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * ListeAllMotClef
     * @param array<mixed> $liste
     * @param array<mixed> $post
     * @return array<mixed>
     */
    // phpcs:ignore
    public function ListeAllMotClef(array $liste, array $post = []) : ?array
    {
        $arrayReq=[];

        if (count($liste)>=1) {
            $array=[];
            foreach ($liste as $motclef) {
                if ($motclef['ETAT'] != 'S') {
                    array_push($array, $motclef['MOTCLEF']);
                }
            }
            $array = "'".implode("','", $array)."'";
            
            $value = "  and MOTCLEF not in (".$array.")";
        } else {
            $value = "";
        }
            
        $req="SELECT 
            (CASE 
                WHEN ISNULL(t.TRA_MOT, '') = ''
                THEN MOTCLEF.MOTCLEF 
                ELSE t.TRA_MOT
            END) AS MOTCLEF 
        FROM MOTCLEF WITH (NOLOCK) 
            LEFT OUTER JOIN TRAD_MCLE t WITH(NOLOCK) ON t.MOT_CLE = MOTCLEF.MOTCLEF AND t.COD_LNG = '{$_SESSION['Session_Langue']}'
        WHERE 1=1 ".$value."";
        
        if (isset($post['filter_popupMOTCLEF']) && $post['filter_popupMOTCLEF']) {
            $req .=" and upper(MOTCLEF.MOTCLEF) LIKE :value";
            $arrayReq[':value'] = strtoupper('%'.$post['filter_popupMOTCLEF'].'%');
        }

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    
    /**
     * listeMotClefXML
     * LISTE MOT CLEF VENANT D'UN XML
     *
     * @param array<mixed> $listeMotClef
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function listeMotClefXML(array $listeMotClef, array $post) : ?array
    {
        $arrayReq=[];
        if ($listeMotClef) {
            $req  = " select * from (";
            
            $first = true;
            foreach ($listeMotClef as $motClef) {
                if (!$first) {
                    $req .= " union ";
                }
                $req .= " select '".$motClef['MOTCLEF']."' MOTCLEF, '".$motClef['ETAT']."' ETAT ";
                
                $first=false;
            }
            
            $req .= " ) motClef where 1=1";
            
            
            if (isset($post['filter_MOTCLEF']) && $post['filter_MOTCLEF']) {
                $req .=" and UPPER(MOTCLEF) LIKE :value3";
                $arrayReq[':value3'] = strtoupper('%'.$post['filter_MOTCLEF'].'%');
            }
            
            if (isset($post['filter_ETAT']) && $post['filter_ETAT']) {
                $req .=" and ETAT = :value4";
                $arrayReq[':value4'] = $post['filter_ETAT'];
            }
            
            return $this->fetchAssocAll($req, $arrayReq);
        }

        return [];
    }
}
