<?php

namespace Modules\DemandeAchat\Repository;

use Helpers\QueryBuilder\AbsRepository;

class DemandeAchatEventRepo extends AbsRepository
{

    /**
     * @param mixed $code
     * @return array<mixed>
     */
    public function demandeAchatEntete($code)
    {
        return $this->fetchAssocOne(
            "SELECT 
                DA_ENTET.*,
                FOURNISS.RS 
            FROM DA_ENTET with (nolock)
            LEFT JOIN FOURNISS with (nolock) on DA_ENTET.FOU = FOURNISS.CODE
            LEFT JOIN CA_LIGNE with (nolock) on CA_LIGNE.NO_DA = DA_ENTET.NO_DA
            WHERE DA_ENTET.NO_DA = :value",
            [':value' => $code]
        );
    }

    /**
     * @param array<mixed> $param
     */
    private function getMainRequest(array &$param) :string
    {
        return
            "SELECT 
                DA_EVENT.*,
                MEMOS.CONTMEMO as Commentaire,
                " . $this->selectTrad('SUIDADAT.LIBELLE') . ",
                Auteur.NOM as Auteur_NOM,
                Signat.NOM as Signataire_NOM
            FROM DA_EVENT with (nolock)
            LEFT JOIN MEMOS with (nolock) ON DA_EVENT.NM_COMME = MEMOS.NODEMEMO
            LEFT JOIN SUIDADAT with (nolock) ON SUIDADAT.CODE_SDA = DA_EVENT.CODE
            " . $this->joinTrad('SUIDADAT.LIBELLE', $param) . "
            LEFT JOIN HASTUTIL Auteur with (nolock) ON Auteur.CODE = DA_EVENT.UTIL
            LEFT JOIN HASTUTIL Signat with (nolock) ON Signat.CODE = DA_EVENT.SIGNAT
            WHERE 1=1 ";
    }
    
    /**
     * @param mixed $code
     * @param mixed $MODULE
     * @return string[][]
     */
    public function listeEventDemandeAchat($code, $MODULE = null)
    {
        $param = [];
        $req = $this->getMainRequest($param)
            . " AND NO_DA = :value "
            . ($MODULE == 'signataire' ? " AND DA_EVENT.CODE = 'SDA' " : '')
            . " ORDER BY DTE desc, HEURE desc "
        ;
        $param[':value'] = $code;
        return $this->fetchAssocAll($req, $param);
    }

    public function export(string $code, ?string $MODULE = null) :\PDOStatement
    {
        $param = [];
        $req = $this->getMainRequest($param)
            . " AND NO_DA = :value "
            . ($MODULE == 'signataire' ? " AND DA_EVENT.CODE = 'SDA' " : '')
            . " ORDER BY DTE desc, HEURE desc "
        ;
        $param[':value'] = $code;
        return $this->createStatement($req, $param);
    }
    
    /**
     * @return string[][]
     */
    public function listeEvent()
    {
        $param = [];
        $req = "SELECT 
            SUIDADAT.CODE_SDA, 
            " . $this->selectTrad('SUIDADAT.LIBELLE') . "
        FROM SUIDADAT with (nolock) 
        " . $this->joinTrad('SUIDADAT.LIBELLE', $param) . "
        WHERE GERE_HAP = " . \Helpers\Outil\Sql::FALSE;
        
        return $this->fetchAssocAll($req, $param);
    }
    
    /**
     * @param array<mixed> $post
     */
    public function ajoutEvenement($post) :void
    {
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        
        $req = "SELECT MAX(NUM_ORD) NUM_ORD FROM DA_EVENT with (nolock) WHERE NO_DA = :noDA";
        $arrayReq = [':noDA' => $post['NO_DA']];
        
        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);

        $resultatOrder = $da->fetch()['NUM_ORD']??'0';
        $maxOrd = $resultatOrder+1;
        
        $req =
        "INSERT INTO DA_EVENT (NO_DA, NUM_ORD, DTE, HEURE, CODE, UTIL, SIGNAT, HAPLUS, NM_COMME)
        VALUES (:noDA, :numOrd, :dateCre, :heureCre, :codeEve, :Util, '', ".\Helpers\Outil\Sql::setBoolSql(0).", :memos)";
        
        $arrayReq = [
            ':noDA' => $post['NO_DA'],
            ':numOrd' => $maxOrd,
            ':dateCre' => $post['DTE'],
            ':heureCre' => $post['HEURE'],
            ':codeEve' => $post['CODE'],
            ':Util' => $post['UTIL'],
            ':memos' => $post['NM_COMME'],
        ];
        
        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);
    }
    
    /**
     * @param mixed $id
     * @param mixed $ordre
     * @return array<mixed>
     */
    public function findEvent($id, $ordre)
    {
        $req =
        "SELECT DA_EVENT.CODE CODE_EVE,
        MEMOS.NODEMEMO, MEMOS.CONTMEMO 
        FROM DA_EVENT with (nolock)
        LEFT JOIN MEMOS with (nolock) on DA_EVENT.NM_COMME = MEMOS.NODEMEMO
        WHERE DA_EVENT.NO_DA = :value AND DA_EVENT.NUM_ORD = :value1";
        
        $arrayReq = [
            ':value' => $id,
            ':value1' => $ordre
        ];
        
        $commande = $this->pdo->prepare($req);
        $commande->execute($arrayReq);
        return $commande->fetch();
    }
    
    /**
     * @param mixed $id
     * @param mixed $ordre
     * @return void
     */
    public function deleteEvenement($id, $ordre)
    {
        $req = "DELETE DA_EVENT WHERE NO_DA=:id AND NUM_ORD=:ordre";
        
        $arrayReq = [
            ':id' => $id,
            ':ordre' => $ordre
        ];
        
        $commande = $this->pdo->prepare($req);
        $commande->execute($arrayReq);
    }
    
    /**
     * @param array<mixed> $post
     * @return void
     */
    public function modificationEvenement($post)
    {
        $req = "UPDATE DA_EVENT SET NM_COMME = :value2 where NO_DA = :value and NUM_ORD = :value1";
        
        $arrayReq = [
            ':value' => $post['NO_DA'],
            ':value1' => $post['NUM_ORD'],
            ':value2' => $post['NM_COMME']
        ];
        
        $commande = $this->pdo->prepare($req);
        $commande->execute($arrayReq);
    }
}
