<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;

class Bdd extends QueryBuilder
{
    protected string $table = 'BDD with (nolock)';
    protected string $primaryKey = '';
    protected array $primKey = [
        'CODE_ART',
        'CODE_FOU',
        'NO_PAGE',
    ];
    protected string $libelle = '';

    /**
     * @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 and VEPROC = 0x54 ";

        $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;
        }

        $query .= " order by NUMORDRE";
        
        $data = $this->pdo->prepare($query);
        $data->execute($params);
        
        $return = $data->fetchAll($this->pdo::FETCH_ASSOC);

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