<?php

namespace Modules\GlobalApplication\Repository;

use Helpers\Outil\Sql;

class Delegation
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    public function listeDelegationRecue($module, $post, $sansTri = 0)
    {
        $arrayReq = [];
        $today = Date('Ymd');

        // Spec 3523
        $req = "SELECT distinct SIGNAT, DELEG, DTE_DEB, DTE_FIN, hasig.NOM, 'ABS' as TYPEDEL,
        (   
            select count(*) from WFT_SIG with (nolock) 
            left outer join WF_GRP_SIG_LIST grp with(nolock) on (grp.WF_GS_ENT_TYPE = WFT_SIG.TYPE_ENT and grp.WF_GS_CODE = WFT_SIG.GROUPE_SIG) 
            where TYPE_ENT = '$module' and (WFT_SIG.SIGNAT=SIG_DELEG.SIGNAT or grp.WF_GS_CODE_SIG=SIG_DELEG.SIGNAT) and SIGNABLE=0x54
        ) nbSign
        FROM SIG_DELEG with(nolock)
        LEFT JOIN HASTUTIL hasig with(nolock) on SIG_DELEG.SIGNAT = hasig.CODE and (hasig.SIG_DA = 0x54 or hasig.SIG_CDE = 0x54 or hasig.SIG_BAP = 0x54 or hasig.SIG_REF = 0x54)
        WHERE DELEG = :user
        AND DTE_DEB <= $today
        AND (DTE_FIN >= $today or DTE_FIN = '00000000')";

        $arrayReq = [':user' => $_SESSION['Session_Login']];

        if (Sql::boolSql(Sql::paravrac($this->pdo, 'PARAMSIG', 'DELEG_AUTO_SUP_HIER'))) {
            $req .= " UNION 
            SELECT CODE as SIGNAT, SUPHIER as DELEG, '00000000', '00000000', NOM, 'HIE' as TYPEDEL,
            (
                select count(*) from WFT_SIG with (nolock) 
                left outer join WF_GRP_SIG_LIST grp with(nolock) on (grp.WF_GS_ENT_TYPE = WFT_SIG.TYPE_ENT and grp.WF_GS_CODE = WFT_SIG.GROUPE_SIG) 
                where TYPE_ENT = '$module' and (WFT_SIG.SIGNAT=HASTUTIL.CODE or grp.WF_GS_CODE_SIG=HASTUTIL.CODE) and SIGNABLE=0x54
            ) nbSign
            FROM HASTUTIL with(nolock)
            WHERE SUPHIER = '".$_SESSION['Session_Login']."' AND (SIG_DA = 0x54 or SIG_CDE = 0x54 or SIG_BAP = 0x54 or SIG_REF = 0x54)";
        }

        if (Sql::boolSql(Sql::paravrac($this->pdo, 'PARAMSIG', 'DELEG_AUTO_RESP_UO'))) {
            $req .= " UNION 
            SELECT CODE as SIGNAT, '".$_SESSION['Session_Login']."' as DELEG, '00000000', '00000000', NOM, 'HIE' as TYPEDEL,
            (
                select count(*) from WFT_SIG with (nolock) 
                left outer join WF_GRP_SIG_LIST grp with(nolock) on (grp.WF_GS_ENT_TYPE = WFT_SIG.TYPE_ENT and grp.WF_GS_CODE = WFT_SIG.GROUPE_SIG) 
                where TYPE_ENT = '$module' and (WFT_SIG.SIGNAT=HASTUTIL.CODE or grp.WF_GS_CODE_SIG=HASTUTIL.CODE) and SIGNABLE=0x54
            ) nbSign
            FROM HASTUTIL with(nolock)
            WHERE CODE_UO in (SELECT CODE_UO FROM UO with(nolock) WHERE RESP_HIER = '".$_SESSION['Session_Login']."')
            AND (SIG_DA = 0x54 or SIG_CDE = 0x54 or SIG_BAP = 0x54 or SIG_REF = 0x54)";
        }

        if (!$sansTri) {
            $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
            $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
            $req .= $filtreAvance->generateSql(1);
            $filtreAvance->pushArray($arrayReq);

            if (count($whereArray)>  0) {
                $req .= $whereArray[0].$whereArray['tri'];
                
                if (count($whereArray[1]) != 0) {
                    $arrayReq = array_merge($arrayReq, $whereArray[1]);
                }
            }
        }

        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);
        
        return $da->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * listeFournisseurByCode
     * @return array<mixed>
     */
    public function listeFournisseurByCode() : ?array
    {
        $req =" SELECT * FROM WFT_SIG with (nolock) where TYPE_ENT = 'FOURNISS' and SIGNAT = '".$_SESSION['Session_Login']."'";
        
        $incident = $this->pdo->prepare($req);
        $incident->execute();
        
        return $incident->fetchAll($this->pdo::FETCH_ASSOC);
    }
}
