<?php

namespace Modules\Suivi\Repository;

use Helpers\Outil\Roles;
use Helpers\QueryBuilder\AbsRepository;
use Helpers\Trad\ITrad;
use Skeleton\Router;

class SuiviConsultRepo extends SuiviRepo
{
    public function __construct(\PDO $pdo, Router $router, ITrad $trad)
    {
        parent::__construct($pdo, $router, $trad);
    }

    /**
     * @return array<mixed>
     */
    public function getInfoEntete(string $noAo)
    {
        return [];
    }

    /**
     * @return array<mixed>
     */
    public function getLiseLigne(string $noAo)
    {
        $req = "select  
                    NO_PO, NO_DA, NO_PO_DA, CODE_ART, DES, QTE, UNITE, BUDGET, DELAI, MEMOS.CONTMEMO DES_COMPL
                from AO_LIGNE with (nolock)
                left join MEMOS with (nolock) on MEMOS.NODEMEMO = AO_LIGNE.NM_DCOMP
                where NO_AO = :noAo";

        $arrayReq = [
            ':noAo' => $noAo
        ];

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

    /**
     * @return array<mixed>
     */
    public function getConsultLinkDoc(string $typeDoc, string $idDoc)
    {
        $req = "
            select 
                NO_AO, AO_ENTET.CODE_STE, EMETTEUR, ACH, DTE_AO, DTE_VALID_AO, SOCIETE.RAIS_SOC, HASTUTIL.NOM, CENTRE.RAIS_SOC LIB_CTR
            from AO_ENTET WITH (NOLOCK)
            left join SOCIETE with (nolock) on SOCIETE.CODE_STE = AO_ENTET.CODE_STE
            left join HASTUTIL with (nolock) on HASTUTIL.CODE = AO_ENTET.ACH
            left join CENTRE with (nolock) on CENTRE.CODE_CTR = EMETTEUR
            where 1=1 ".Roles::getClauseSocieteOuvert('SOCIETE');

        $arrayReq = [];

        if ($typeDoc=='DA') {
            $req .=  " and NO_AO in (select distinct(NO_AO) from AO_LIGNE with (nolock) where NO_DA = :idDoc)";
            $arrayReq = [
                ':idDoc' => $idDoc
            ];
        } elseif ($typeDoc == 'CDE') {
            $req .=  "
                and AO_ENTET.NO_AO in (
                    select 
                        distinct(NO_AO) 
                    from AO_LIGNE with (nolock)
                    left join DA_LIGNE with (nolock) on DA_LIGNE.NO_DA = AO_LIGNE.NO_DA AND DA_LIGNE.NO_PO = AO_LIGNE.NO_PO_DA 
                    left join CA_LIGNE with (nolock) on CA_LIGNE.NO_DA = DA_LIGNE.NO_DA and CA_LIGNE.NO_PO_DA = DA_LIGNE.NO_PO
                    where NO_CDE = :idDoc
                )
             ";
            $arrayReq = [
                ':idDoc' => $idDoc
            ];
        }

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