<?php

namespace Modules\ProjetAchat\Repository;

class DataAction
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * @return array<mixed>
    */
    public function getDataAction(string $typeObjet, int $idObjet, int $idData): ?array
    {
        $req = 'select DOA_ACTIONS_DATA.TYPE_OBJET,DOA_ACTIONS_DATA.ID_OBJET,DOA_ACTIONS_DATA.ID_ACTION,DOA_ACTIONS_DATA.ID_DONNEE,DOA_ACTIONS_DATA.ID_DATA,DOA_ACTIONS_DATA.AFFICHEE,DOA_ACTIONS_DATA.MODIFIABLE,DOA.*
        from DOA_ACTIONS_DATA with (nolock) 
            left join DOA with(nolock) on DOA.ID = DOA_ACTIONS_DATA.ID_OBJET 
        where TYPE_OBJET = :typeObjet 
            and ID_OBJET =  :idObjet 
            and ID_DATA  =  :idData';
       
        $arrayReq = [
            ':typeObjet'       => $typeObjet,
            ':idObjet'         => $idObjet,
            ':idData'          => $idData,
        ];

        $dataAction              = $this->pdo->prepare($req);
        $dataAction->execute($arrayReq);

        return $dataAction->fetch();
    }

    
    public function supprimerDataAction(string $typeObjet, int $idObjet, int $idDonnee, ?int $idData = null): void
    {
        $req = 'delete DOA_ACTIONS_DATA 
        where TYPE_OBJET =:typeObjet 
            and   ID_OBJET   =  :idObjet 
            and   ID_DONNEE  =  :idDonnee ';
            
        $arrayReq = [
            ':typeObjet'       => $typeObjet,
            ':idObjet'         => $idObjet,
            ':idDonnee'        => $idDonnee
        ];
        if ($idData) {
            $req .=' and ID_DATA = :idData';
            $arrayReq[':idData'] = $idData;
        }
       

        $dataAction              = $this->pdo->prepare($req);
        $dataAction->execute($arrayReq);
    }
}
