<?php

namespace Modules\GlobalApplication\Repository;

use Helpers\Outil\Sql;

class WorkflowSignature
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    /**
     * getListeSignataireAttendu
     * @param string $module
     * @param string $codeDemande
     * @param bool $fetch
     * @param string $user
     * @return array<mixed>
     */
    public function getListeSignataireAttendu(string $module, string $codeDemande, bool $fetch = false, $user = '') : ?array
    {
        if ($fetch == true) {
            $req = "select * 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 WFT_SIG.CODE_DEMANDE = :codeDemande and WFT_SIG.TYPE_ENT = :module  and WFT_SIG.SIGNABLE=0x54 and (WFT_SIG.SIGNAT = :user1 or grp.WF_GS_CODE_SIG = :user2)";
            
            $arrayReq = [
                ':codeDemande' => $codeDemande,
                ':module' => $module,
                ':user1' => $user,
                ':user2' => $user,
            ];

            $WF = $this->pdo->prepare($req);
            $WF->execute($arrayReq);
            
            return $WF->fetch(\PDO::FETCH_ASSOC);
        } else {
            $req = "select * from WFT_SIG  with (nolock)
                LEFT JOIN HASTUTIL  with (nolock) on HASTUTIL.CODE = WFT_SIG.SIGNAT
                left outer join WF_GRP_SIG grp with(nolock) on (grp.WF_GS_ENT_TYPE = WFT_SIG.TYPE_ENT and grp.WF_GS_CODE = WFT_SIG.GROUPE_SIG)
                WHERE CODE_DEMANDE=:codeDemande and TYPE_ENT=:module order by ORDRE";
            
            $arrayReq = [
                ':codeDemande' => $codeDemande,
                ':module' => $module,
            ];

            $WF = $this->pdo->prepare($req);
            $WF->execute($arrayReq);
            
            return $WF->fetchAll(\PDO::FETCH_ASSOC);
        }
    }

    /**
     * delegByCDE
     * @param string $user
     * @return array<mixed>
     */
    public function delegByCDE(string $user) : ?array
    {
        return $this->getSignatDeleg([$user]);
    }

    /**
     * @param array<string> $listSignatCode
     * @return array<mixed>
     */
    public function getSignatDeleg(array $listSignatCode) :array
    {
        if (empty($listSignatCode)) {
            return [];
        }

        $param_in = [];
        foreach (array_values($listSignatCode) as $i => $code) {
            $param_in[':param_'.$i] = $code;
        }
        $req = "SELECT 
                SIG_DELEG.SIGNAT, 
                SIG_DELEG.DELEG, 
                SIG_DELEG.DTE_DEB, SIG_DELEG.DTE_FIN,
                DelegUser.NOM as DelegUser_NOM
            FROM SIG_DELEG with(nolock)
            LEFT JOIN HASTUTIL as DelegUser with(nolock) ON DelegUser.CODE = SIG_DELEG.DELEG
            WHERE SIG_DELEG.SIGNAT IN (".implode(',', array_keys($param_in)).") 
                AND (
                    SIG_DELEG.DTE_FIN >= :date_now 
                    OR SIG_DELEG.DTE_FIN = '00000000'
                )
            ";
        
        $stmt = $this->pdo->prepare($req);
        $stmt->execute($param_in + [
            ':date_now' => date('Ymd'),
        ]);
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
    
    /**
     * insertSignataire
     * @param array<mixed> $array
     * @return void
     */
    public function insertSignataire(array $array) : void
    {
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

        if ($array['INFORME']=='N') {
            $array['INFORME']=Sql::setBoolSql(0);
        } else {
            $array['INFORME']=Sql::setBoolSql(1);
        }

        if ($array['choixType'] == 'GS') {
            $groupe_sig = $array['GROUPE_SIG'];
        } else {
            $signat = $array['SIGNAT'];
        }


            
        $req = "INSERT INTO WFT_SIG (SIGNAT, TYPE_ENT, CODE_DEMANDE, DTE_MISE, ORDRE, SIGNABLE, ROLES,INFORME, ID_ROLE, TSTAMP_MOB, GROUPE_SIG) 
                VALUES(
                    '".($signat ?? '')."',
                    '".$array['TYPE_ENT']."',
                    '".$array['CODE_DEMANDE']."',
                    '00000000',
                    '".$array['ORDRE']."',
                    ".(Sql::setBoolSql(0)).",
                    '',
                    ".$array['INFORME'].",
                    '0',
                    '',
                    '".($groupe_sig ?? '')."'
                )";


        $WF = $this->pdo->prepare($req);
        $WF->execute();
    }
    
    /**
     * droitModifSignature
     * @param string $codeDemande
     * @param string $user
     * @return array<string>
     */
    public function droitModifSignature(string $codeDemande, string $user) : array
    {
        $req = 'select * from WFT_DEM_ENT with (nolock) right outer join WFT_SIG with (nolock) on WFT_DEM_ENT.TYPE_ENT = WFT_SIG.TYPE_ENT and WFT_DEM_ENT.CODE_DEMANDE = WFT_SIG.CODE_DEMANDE
                right outer join  WFT_SIG_ROLE with (nolock) on  WFT_SIG.CODE_DEMANDE = WSRO_CODE_DEMANDE and WFT_SIG.TYPE_ENT = WSRO_TYPE_ENT and WFT_SIG.SIGNAT = WSRO_SIGNAT
                right outer join WFT_ROLE_DON_MODI with (nolock) on WSRO_ID_ROLE = WRDM_ID_ROLE and WDEN_ID_PAR = WRDM_ID_PAR
                where WFT_DEM_ENT.CODE_DEMANDE = :codeDemande
                and WFT_SIG.SIGNAT = :user';
        
        $arrayReq = [
            ':codeDemande' => $codeDemande,
            ':user' => $user,
        ];
        
        
        $WF = $this->pdo->prepare($req);
        
        $WF->execute($arrayReq);
        return $WF->fetch();
    }
}
