<?php

namespace Modules\Fournisseur\Repository;

use Helpers\QueryBuilder\AbsRepository;

class ZoneGeo extends AbsRepository
{
    /**
     * listeZoneGeo
     * @param string $code
     * @param array<mixed> $post
     * @param mixed $export
     * @return array<mixed>
     */
    public function listeZoneGeo(string $code, array $post, $export = false) : ?array
    {
        if ($export) {
            $alias="LIBDECOUP, ZONEGEO, LIBZONGEO";
        } else {
            $alias="*";
        }
        
        $req = "select ".$alias." from DGEOFOUR with (nolock)
                left join ZONESGEO with (nolock) on ZONESGEO.ZONEGEOG = DGEOFOUR.ZONEGEO and DGEOFOUR.DCOUP = ZONESGEO.CODEDCP
                left join DECOUPAG with (nolock) on DECOUPAG.CODEDCP = ZONESGEO.CODEDCP
                WHERE CODE_FOU=:value";

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

        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);

        if (count($whereArray)>  0) {
            $req .= $whereArray[0];
            if (count($whereArray[1]) != 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }

        $zoneGeo = $this->pdo->prepare($req);
        $zoneGeo->execute($arrayReq);
        
        return  $zoneGeo->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * listeDecoupZG
     * @return array<mixed>
     */
    public function listeDecoupZG() : ?array
    {
        $arrayReq = [];
        $req = "SELECT 
            DECOUPAG.CODEDCP, 
            {$this->selectTrad('DECOUPAG.LIBDECOUP')},
            DECOUPAG.NM_DESCR, 
            DECOUPAG.MODIFPAR
        FROM DECOUPAG WITH (NOLOCK) 
        {$this->joinTrad('DECOUPAG.LIBDECOUP', $arrayReq)}";

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * listeZoneGeoCreation
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function listeZoneGeoCreation(array $post) : ?array
    {
        $req = "SELECT * FROM ZONESGEO 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]);
            }
        }
        
        $zoneGeo = $this->pdo->prepare($req);
        $zoneGeo->execute($arrayReq);
        
        return  $zoneGeo->fetchAll();
    }
}
