<?php

namespace Modules\Consultation\Repository;

use Helpers\Pagination\PaginatorGestion;
use Helpers\QueryBuilder\AbsRepository;

class ConsultationLigneRepo extends AbsRepository
{
    /**
     * Summary of listeArticle
     * @param mixed $post
     * @return string[][]
     */
    public function listeArticles($post): ?array
    {
        $paginator = new PaginatorGestion();
        $paginator->generate(30, 'DESIGNAT_ART', 'ASC');

        $req = "SELECT	ARTFOURN.FOU as CODE_FOU,
                        FOURNISS.RS as RS_FOU,
                        ARTICLES.CODE_ART, 
                        ARTICLES.DESIGN as DESIGNAT_ART, 
                        SSFAMILL.CODE_SFA, 
                        SSFAMILL.DESIGNAT as DESIGNAT_SFA, 
                        FAMILLE.CODE_FAM, 
                        FAMILLE.DESIGNAT as DESIGNAT_FAM 
                FROM ARTICLES 
                    LEFT JOIN ARTFOURN on ARTICLES.CODE_ART = ARTFOURN.CODE_ART 
                    LEFT JOIN FOURNISS on ARTFOURN.FOU = FOURNISS.CODE
                    LEFT JOIN SSFAMILL on ARTICLES.SSFAM = SSFAMILL.CODE_SFA 
                    LEFT JOIN FAMILLE on SSFAMILL.CODE_FAM = FAMILLE.CODE_FAM
                WHERE 1=1 
        ";
        $param = [];

        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
        if (count($whereArray) >  0) {
            $req .= $whereArray[0] . $whereArray['tri'];

            if (count($whereArray[1]) != 0) {
                $param = array_merge($param, $whereArray[1]);
            }
        }

        $paginator->injecterSqlCount(
            $req,
            $post,
            $param,
            $this->pdo
        );

        return $this->fetchAssocAll($req, $param);
    }
}
