<?php

namespace Modules\Fournisseur\Repository;

use Helpers\QueryBuilder\AbsRepository;

class FourEventRepo extends AbsRepository
{

    /**
     * @param array<mixed> $arrayReq
     */
    private function getMainRequest(array &$arrayReq) :string
    {
        return "SELECT 
            FOUR_EVE.*, 
            " . $this->selectTrad('FOUR_TYP_EVE.LIBELLE') . ",
            FOUR_TYP_EVE.GERE_HAP,
            HASTUTIL.NOM as Auteur_NOM
        FROM FOUR_EVE with (nolock)
        LEFT JOIN HASTUTIL with (nolock) on HASTUTIL.CODE = FOUR_EVE.ID_USER
        LEFT JOIN FOUR_TYP_EVE with(nolock) on FOUR_TYP_EVE.COD_EVE = FOUR_EVE.COD_EVE
        " . $this->joinTrad('FOUR_TYP_EVE.LIBELLE', $arrayReq) . "
        WHERE FOUR_EVE.CODE=:codeFrs order by NUM_ORDR DESC";
    }
    
    /**
     * listeEvenentFrs
     * @param string $codeFrs
     * @return array<array<string>>
     */
    public function listeEvenentFrs(string $codeFrs) : ?array
    {
        $arrayReq = [':codeFrs' => $codeFrs];
        return $this->fetchAssocAll(
            $this->getMainRequest($arrayReq),
            $arrayReq
        );
    }

    /**
     * @param string $codeFrs
     */
    public function export(string $codeFrs) :\PDOStatement
    {
        $arrayReq = [':codeFrs' => $codeFrs];
        return $this->createStatement(
            $this->getMainRequest($arrayReq),
            $arrayReq
        );
    }
    
    /**
     * deleteEvenement
     * @param string $codeFrs
     * @param string $numOrdre
     * @return void
     */
    public function deleteEvenement(string $codeFrs, string $numOrdre) : void
    {
        $req = "DELETE FROM FOUR_EVE with (nolock) where CODE=:codeFrs and NUM_ORDR =:numOrdre";
        
        $arrayReq = [':codeFrs' => $codeFrs, ':numOrdre' => $numOrdre];
        
        $req = $this->pdo->prepare($req);
        $req->execute($arrayReq);
    }
    
    /**
     * detailEvent
     * @param string $codeFrs
     * @param string $numOrdre
     * @return array<mixed>
     */
    public function detailEvent(string $codeFrs, string $numOrdre) : ?array
    {
        $arrayReq = [
            ':codeFrs' => $codeFrs,
            ':numOrdre' => $numOrdre
        ];
        $req = "SELECT 
                FOUR_EVE.*, 
                FOUR_EVE.COD_EVE as CODE_EVE,
                " . $this->selectTrad('FOUR_TYP_EVE.LIBELLE') . ",
                FOUR_TYP_EVE.GERE_HAP,
                FOUR_EVE.COMMENTAIRE as CONTMEMO 
            FROM FOUR_EVE with (nolock)
            LEFT JOIN FOUR_TYP_EVE with (nolock) 
                ON FOUR_TYP_EVE.COD_EVE = FOUR_EVE.COD_EVE
            " . $this->joinTrad('FOUR_TYP_EVE.LIBELLE', $arrayReq) . "
            WHERE FOUR_EVE.CODE = :codeFrs AND FOUR_EVE.NUM_ORDR = :numOrdre";

        $req = $this->pdo->prepare($req);
        $req->execute($arrayReq);
        
        return $req->fetch();
    }
    
    /**
     * listeEvent
     * @return array<mixed>
     */
    public function listeEventType() : ?array
    {
        $param = [];
        $req = "SELECT 
                COD_EVE, 
                ". $this->selectTrad('FOUR_TYP_EVE.LIBELLE') .", 
                GERE_HAP
            FROM FOUR_TYP_EVE WITH (NOLOCK) 
            " . $this->joinTrad('FOUR_TYP_EVE.LIBELLE', $param) . "
            WHERE GERE_HAP = 0x46";

        return $this->fetchAssocAll($req, $param);
    }
    
    /**
     * modificationEvenement
     * @param array<mixed> $array
     * @return void
     */
    // phpcs:ignore
    public function modificationEvenement(array $array) : void
    {
        $req = "UPDATE FOUR_EVE SET COMMENTAIRE =:commentaire where CODE=:codeFrs and NUM_ORDR =:numOrdre";
        
        $arrayReq = [
            ':codeFrs' => $array['CODE'],
            ':numOrdre' => $array['NO_ORDR'],
            ':commentaire' => $array['COMMENTAIRE']
        ];
        
        $req = $this->pdo->prepare($req);
        $req->execute($arrayReq);
    }
    
    /**
     * insertEvenement
     * @param array<mixed> $array
     * @return void
     */
    public function insertEvenement(array $array) : void
    {
        
        $maxRow = $this->fetchAssocOne(
            "SELECT MAX(NUM_ORDR) NUM_ORD FROM FOUR_EVE with (nolock) WHERE CODE=:codeFrs",
            [':codeFrs' => $array['CODE']]
        );

        $maxOrd = intval($maxRow['NUM_ORD'] ?? 0) + 1;
        
        $req = "insert into FOUR_EVE (CODE,NUM_ORDR, DTE_EVE, HEU_EVE,ID_USER,COD_EVE, COMMENTAIRE) VALUES(:codeFrs, :numOrdre, '".date('Ymd')."', '".date('His')."', :user, :codeEve, :commentaire)";
        $arrayReq = [
            ':codeFrs' => $array['CODE'],
            ':numOrdre' => $maxOrd,
            ':user' => $_SESSION['Session_Login'],
            ':commentaire' => $array['COMMENTAIRE'],
            ':codeEve' => $array['EVENT'],
        ];

        $req = $this->pdo->prepare($req);
        $req->execute($arrayReq);
    }
    
    /**
     * listeSuiviFacSend
     * @param string $id
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function listeSuiviFacSend(string $id, array $post) : ?array
    {
        //$req = "select * from FOUR_EVE where CODE = :codeFrs and COD_EVE = 'ENF' ";
        
        $req = "select DTE_EVE, HEU_EVE, COMMENTAIRE, NOM, FAC_FIC_NAME, NUM_ORDR from FOUR_EVE with(nolock)
                    left join OXALINK_INVOICE_DEPOSAL with(nolock) ON CODE_FOU = CODE and NUM_ORDER = NUM_ORDR
                    left join OXALINK_USER with (nolock) ON USER_ID = OXALINK_INVOICE_DEPOSAL.ID_USER_OXALINK
                    where CODE=:codeFrs and COD_EVE='ENF'";
        
        $arrayReq = [':codeFrs' => $id];
        
        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
        
        if (count($whereArray)>  0) {
            $req .= $whereArray[0].$whereArray['tri'];
            if (count($whereArray[1]) != 0) {
                $arrayReq =  array_merge($arrayReq, $whereArray[1]);
            }
        }
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * evenementPDF
     * @param string $id
     * @param string $order
     * @return array<mixed>
     */
    public function evenementPDF(string $id, string $order) : ?array
    {
        $req = "SELECT * FROM OXALINK_INVOICE_DEPOSAL with(nolock) where CODE_FOU = :codeFou and NUM_ORDER = :numOrder";
        
        $arrayReq = [
            ':codeFou' => $id,
            ':numOrder' => $order,
        ];
        
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
        
        return $requete->fetch();
    }
}
