<?php

namespace Modules\GlobalApplication\Repository;

use Helpers\Outil\Sql;

class Bdd
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    // affiche la liste des bdd
    /**
     * Liste all the current data of BDD table
     * @param string $code
     * @param array<string> $post
     * @param bool $export
     * @param string $module
     */
    public function listeBdd(string $code, array $post, bool $export = false, string $module = '')
    {
        if ($export) {
            $alias="NO_PAGE NUMERO_PAGE, NUMORDRE NUMERO_ORDRE, TITRE SOMMAIRE, VEPROC EPROC, ONGLET, LANGUE, CONTMEMO TEXTE";
        } else {
            $alias="*, MEMOS.CONTMEMO TEXTEBDD";
        }
        $req = "SELECT ".$alias." from BDD WITH(NOLOCK)
                LEFT JOIN MEMOS with (nolock) on MEMOS.NODEMEMO = BDD.NM_TEXTE
                WHERE BDD.CODE_FOU=:value and CODE_ART=:module";
        
        $arrayReq = [
            ':value' => $code,
            ':module' => $module
        ];
        
        $bdd = $this->pdo->prepare($req);
        
        $bdd->execute($arrayReq);
        return $bdd->fetchAll($this->pdo::FETCH_ASSOC);
    }

   
    /**
     * liste BDD by teams and id
     * @param string $code
     * @param string $page
     * @return array<mixed>
     */
    public function bddGroupement(string $code, string $page) : ?array
    {
        $req = "SELECT * FROM BDD WITH(NOLOCK) WHERE BDD.CODE_ART='Groupement'  and  CODE_FOU=:value1 and NO_PAGE=:value2";
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $page,
        ];
        
        $bdd = $this->pdo->prepare($req);
        $bdd->execute($arrayReq);
        
        return $bdd->fetch();
    }
    
    /**
     * If exist BDD
     * @param string $id
     * @param string $page
     * @param string $module
     * @return bool
     */
    public function bddExist(string $id, string $page, string $module) : bool
    {
        $req = "SELECT * FROM BDD WITH(NOLOCK) WHERE CODE_ART=:value1 AND CODE_FOU=:value2 and NO_PAGE=:value3";
        
        $arrayReq = [
            ':value1' => $module,
            ':value2' => $id,
            ':value3' => $page,
        ];
        
        $bdd = $this->pdo->prepare($req);
        
        $bdd->execute($arrayReq);
        
        $exist =  $bdd->fetch();
        
        if ($exist) {
            return true;
        }
            
            return false;
    }
    
    /**
     * nouveauBdd
     * @param string $id
     * @param string $module
     * @param array<string> $post
     * @return void
     */
    public function nouveauBdd(string $id, string $module, array $post) : void
    {
        $sqlMemo = new \Helpers\Outil\Sql();
        $idMemo = $sqlMemo->generateNum($this->pdo);
        
        $req = "INSERT INTO MEMOS(NODEMEMO,CONTMEMO) VALUES (".$idMemo.", :value)";
        $arrayReq = [
            ':value' => $post['TEXTEBDD']
        ];
        
        $bdd = $this->pdo->prepare($req);
        $bdd->execute($arrayReq);
        
        $req = "INSERT INTO BDD(
            CODE_ART,
            CODE_FOU,
            NO_PAGE,
            TITRE,
            NM_TEXTE,
            NUMORDRE,
            LANGUE,
            VEPROC,
            LIEPARAM,
            ONGLET,
            HERITAGE,
        ) VALUES (:value1, :value2, '".$post['NO_PAGE']."', :value3, :value4, :value5, :value6,  ".SQL::setBoolSql((int)$post['VEPROC']).", ".SQL::setBoolSql(0).", :value9, ".SQL::setBoolSql(0).")";
        $arrayReq = [
            ':value1' => $module,
            ':value2' => $id,
            ':value3' => $post['TITRE'],
            ':value4' => $idMemo,
            ':value5' => $post['NUMORDRE'],
            ':value6' => $post['LANGUE'],
            ':value9' => $post['ONGLET'],
        ];
        
        $bdd = $this->pdo->prepare($req);
        $bdd->execute($arrayReq);
    }
    
    /**
     * deleteBdd
     * @param string $id
     * @param string $page
     * @param string $module
     * @return void
     */
    public function deleteBdd(string $id, string $page, string $module) : void
    {
        $req = "DELETE FROM BDD WHERE CODE_ART=:value1 AND CODE_FOU=:value2 and NO_PAGE=:value3";
        
        $arrayReq = [
            ':value1' => $module,
            ':value2' => $id,
            ':value3' => $page,
        ];

        $bdd = $this->pdo->prepare($req);
        $bdd->execute($arrayReq);
    }
    
    /**
     * bddAffichage
     * @param string $id
     * @param string $page
     * @param string $module
     * @return array<string>
     */
    public function bddAffichage(string $id, string $page, string $module) : ?array
    {
        $req = "SELECT CONTMEMO TEXTEBDD,* FROM BDD WITH(NOLOCK) LEFT JOIN MEMOS with (nolock) on MEMOS.NODEMEMO = BDD.NM_TEXTE WHERE CODE_ART=:value1 AND CODE_FOU=:value2 and NO_PAGE=:value3";
        
        $arrayReq = [
            ':value1' => $module,
            ':value2' => $id,
            ':value3' => $page,
        ];
        
        $bdd = $this->pdo->prepare($req);
        
        $bdd->execute($arrayReq);
        
        return $bdd->fetch();
    }
    
    /**
     * updaetBDD
     * @param string $id
     * @param string $module
     * @param string $page
     * @param array<string> $post
     * @return void
     */
    public function updaetBDD(string $id, string $module, string $page, array $post) : void
    {
        $req = "UPDATE BDD SET TITRE=:value5, NUMORDRE=:value6, VEPROC=".SQL::setBoolSql((int)$post['VEPROC']).", ONGLET=:value8, LANGUE=:value9 WHERE CODE_ART=:value1 and CODE_FOU=:value2 and NO_PAGE=:value3";
        
        $arrayReq = [
            ':value1' => $module,
            ':value2' => $id,
            ':value3' => $page,
            'value5' => $post['TITRE'],
            'value6' => $post['NUMORDRE'],
            'value8' => $post['ONGLET'],
            'value9' => $post['LANGUE'],
        ];
        
        $bdd = $this->pdo->prepare($req);
        $bdd->execute($arrayReq);
    }
}
