<?php

namespace Modules\ProjetAchat\Repository;

class DoaEvent
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    public function rqInsertEventDOA(int $id, string $codeEvent, string $message = ''): void
    {
        $req = "INSERT INTO DOA_EVT (ID_DOA, NUM_ORDRE, DATE_EVT, HEURE_EVT, CODE_USER, CODE_EVT, COMMENTAIRE) VALUES (:id, (SELECT
                CASE WHEN NUM_ORDRE IS NULL THEN '1' ELSE NUM_ORDRE+1 END AS NUM_ORDRE
                FROM (SELECT MAX(NUM_ORDRE) NUM_ORDRE FROM DOA_EVT WITH (NOLOCK) WHERE ID_DOA='$id') test),
                :date, :heure, :user, :codeEvent, :message)";
        
        $arrayReq = [
            ':id' => $id,
            ':date' => date('Ymd'),
            ':heure' => date('His'),
            ':user' => $_SESSION["Session_Login"],
            ':codeEvent' => $codeEvent,
            ':message' => $message,
        ];
        
        $data = $this->pdo->prepare($req);
        $data->execute($arrayReq);
    }
    
    /**
     * @return array<mixed>
    */
    public function getLastOrder(int $idObjet, string $typeObjet): ?array
    {
        $req = "SELECT MAX(NUM_ORDRE) MAX_NUM_ORDRE FROM DOA_EVT WITH (NOLOCK) WHERE ID_DOA = :idDoa AND CODE_EVT = :codeEve ";
        
        $arrayReq = [
            ':idDoa' => $idObjet,
            ':codeEve' => $typeObjet=='D'? 'MOD':'MMO',
        ];
        
        $data = $this->pdo->prepare($req);
        $data->execute($arrayReq);
        
        return $data->fetch();
    }
    
    /**
     * @return array<mixed>
    */
    public function getCommentary(int $idObjet, string $typeObjet, int $numOrder): ?array
    {
        $req = "SELECT COMMENTAIRE FROM DOA_EVT WITH (NOLOCK) WHERE ID_DOA = :idDoa AND CODE_EVT = :codeEve AND NUM_ORDRE = :order ";
        
        $arrayReq = [
            ':idDoa' => $idObjet,
            ':codeEve' => $typeObjet=='D'? 'MOD':'MMO',
            ':order' => $numOrder,
        ];
        
        $data = $this->pdo->prepare($req);
        $data->execute($arrayReq);
        
        return $data->fetch();
    }
    
    public function updateCommentaire(int $idObjet, string $typeObjet, int $numOrder, string $commentaire): void
    {
        $req = "UPDATE DOA_EVT SET COMMENTAIRE = :commentaire WHERE ID_DOA = :idDoa AND CODE_EVT = :codeEve AND NUM_ORDRE = :order ";
        
        $arrayReq = [
            ':idDoa' => $idObjet,
            ':codeEve' => $typeObjet=='D'? 'MOD':'MMO',
            ':order' => $numOrder,
            ':commentaire' => $commentaire,
        ];
        
        $data = $this->pdo->prepare($req);
        $data->execute($arrayReq);
    }
    
    //4474
    public function rqUpdateStatutDOA(int $id, string $statut, string $newProprietaire, ?string $oldStatut = null): void
    {
        $req = "UPDATE DOA SET PROPRIETAIRE = :newProprietaire";

        $arrayReq = [
            ':newProprietaire' => $newProprietaire,
            ':id' => $id
        ];

        if (!$oldStatut || $oldStatut == 'I') {
            $req .= ", STATUT=:statut";
            $arrayReq[':statut'] = $statut;
        }

        $req .= " WHERE ID= :id";
        
        $data = $this->pdo->prepare($req);
        $data->execute($arrayReq);
    }
    
    /**
     * @return array<mixed>
    */
    public function getNameCreate(int $id, bool $modele = true): ?array
    {
        $req = "SELECT CODE_USER, NOM FROM DOA_EVT WITH(NOLOCK)
                LEFT JOIN HASTUTIL WITH(NOLOCK) ON HASTUTIL.CODE = CODE_USER 
                WHERE ID_DOA=:id AND CODE_EVT=:codeEve";
        
        $arrayReq = [
            ':id' => $id,
            ':codeEve' => $modele? 'CMO':'CRE',
        ];
        
        $data = $this->pdo->prepare($req);
        $data->execute($arrayReq);
        
        return $data->fetch();
    }

    /**
     * @return array<mixed>
    */
    public function getEventTerminerAction(int $id, int $action, string $user): ?array
    {
        $req = "SELECT DATE_EVT FROM DOA_EVT with(nolock) WHERE ID_DOA = :id AND ID_ACTION = :idAction AND CODE_USER = :idUser AND CODE_EVT = 'AAT'";

        $arrayReq = [
            ':id' => $id,
            ':idAction' => $action,
            ':idUser' => $user,
        ];
        
        $data = $this->pdo->prepare($req);
        $data->execute($arrayReq);
        
        return $data->fetch();
    }
}
