<?php

namespace Modules\Panier\Repository;

use Helpers\QueryBuilder\AbsRepository;

class ImportPanier extends AbsRepository
{

    /**
     * @var \PDO
     */
    protected $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * @return array<mixed>
     */
    public function getArchitecture(string $idFormat)
    {
        $req = "SELECT IMP_PAN_L.*,
                    (CASE 
                        WHEN ISNULL(t.TRAD, '') = ''
                        THEN IMP_PAN_R.NOM_CHAMP COLLATE French_BIN
                        ELSE t.TRAD
                    END) AS NOM_CHAMP,IMP_PAN_R.ATTRIBUT,IMP_PAN_R.MASQUE 
                FROM IMP_PAN_L with(nolock) 
                LEFT OUTER JOIN IMP_PAN_R WITH(NOLOCK) 
                    ON IMP_PAN_R.CODE_CHAMP = IMP_PAN_L.CODE_CHAMP 
                LEFT OUTER JOIN TXTTRAD t WITH(NOLOCK)
                    ON t.TXT = IMP_PAN_R.NOM_CHAMP COLLATE French_BIN
                    AND t.COD_LNG = :langue	
                    AND t.COD_PROD = 'OXAPROC' 
                WHERE IMP_PAN_L.ID_PROFIL = :idFormat
                    AND NUM_COL > 0 
                ORDER by NUM_COL";
            
        $arrayReq = [
            ':langue' => $_SESSION['Session_Langue'],
            ':idFormat' => $idFormat,
        ];

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

    /**
     * @param string $idUser
     * @param string $codeSte
     * @return array<mixed>
     */
    public function getCentreBySocUser(string $idUser, string $codeSte)
    {
        $req = "SELECT distinct socUser.* FROM (
                select CODE_STE, CTRLIV, CTR_ADR from HASTUTIL where CODE = :user1
                union 
                select COD_STE, CTRLIVR, CTREMET from SOC_USR where COD_USR = :user2 
                ) socUser where CODE_STE = :codeSte
                ";

        $arrayReq = [
            ':user1' => $idUser,
            ':user2' => $idUser,
            ':codeSte' => $codeSte,
        ];

        return $this->fetchAssocOne($req, $arrayReq);
    }

    /**
     * @param string $article
     * @param string $unite
     * @param bool $critere
     * @return array<mixed>
     */
    public function uniteExists(string $article, string $unite, bool $critere = false)
    {
        return $this->fetchAssocOne(
            "SELECT * from (
                select
                    UNIT, UNITES.LIBELLE
                from ARTICLES with(nolock) 
                LEFT JOIN UNITES with(nolock) on ARTICLES.UNIT = UNITES.CODE_UNI 
                where CODE_ART = :codeArt "
            . ( $critere ? "
                UNION 

                select 
                    CODE_UNI2 UNIT, UNITES.LIBELLE
                from CONV_ART with(nolock)
                LEFT JOIN UNITES with(nolock) on CONV_ART.CODE_UNI2 = UNITES.CODE_UNI 
                where CODE_ART  = :codeArt2

                UNION 

                select 
                    CODE_UNI2 UNIT, UNITES.LIBELLE
                from CONVERS with(nolock) 
                LEFT JOIN UNITES with(nolock) on CONVERS.CODE_UNI2 = UNITES.CODE_UNI 
                where CODE_UNI1 = (select UNIT from ARTICLES with(nolock) where CODE_ART = :codeArt3) " :
                ""
            )
            . " ) unite where UNIT = :codeUnit",
            [
                ':codeArt' => $article,
                ':codeUnit' => $unite,
            ]
            + ($critere ? [
                ':codeArt2' => $article,
                ':codeArt3' => $article,
            ] : [])
        );
    }

    /**
     * Summary of getInfoArticle
     * @param string $codeArt
     * @return array<mixed>
     */
    public function getInfoArticle($codeArt = '')
    {
        $req = "select 
                    ISNULL(TRAD_ART.DES_ART, ARTICLES.DESIGN) DESIGN,
                    ARTICLES.UNIT, ARTICLES.CRITIQUE, ARTICLES.CODEFAB, ARTICLES.CODEGAM, ARTICLES.SSFAM
                from ARTICLES with (nolock)
                left join TRAD_ART with (nolock) on ARTICLES.CODE_ART = TRAD_ART.COD_ART and COD_LNG = :langue
                where ARTICLES.CODE_ART = :codeArt"
            ;

        $arrayReq = [
            ':codeArt' => $codeArt,
            ':langue' => $_SESSION['Session_Langue'],
        ];

        return $this->fetchAssocOne($req, $arrayReq);
    }
}
