<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;

class Bdd extends QueryBuilder
{
    protected \PDO $pdo;
    
    protected string $table = 'BDD with (nolock)';
    
    protected string $primaryKey = '';
    protected array $primKey = [
        'CODE_ART',
        'CODE_FOU',
        'NO_PAGE',
    ];
    protected string $libelle = '';
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * @param string|array<mixed> $codeFou
     * @return array<mixed>
     */
    public function getAllBDD(string $codeArt, $codeFou, string $tab = '')
    {
        $query = "select BDD.*, MEMOS.CONTMEMO  from BDD with (nolock)
                  left join MEMOS with (nolock) on BDD.NM_TEXTE = MEMOS.NODEMEMO
                  where CODE_ART = :codeArt ";

        $params = [
            ':codeArt'    => $codeArt,
        ];

        if (is_array($codeFou)) {
             $query .= " and ".$this->makeWhereIn('CODE_FOU', $codeFou, $params);
        } else {
            $query .= "and CODE_FOU = :codeFou";
            $params[':codeFou'] = $codeFou;
        }

        if ($tab) {
            $query .= " and ONGLET = :tab";
            $params[':tab'] = $tab;
        }
        
        $data = $this->pdo->prepare($query);
        $data->execute($params);
        
        $return = $data->fetchAll($this->pdo::FETCH_ASSOC);

        if ($return) {
            return $return;
        } else {
            return [];
        }
    }
}
