<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;

class OxaUserPanierCsDon extends QueryBuilder
{
    protected string $table = 'oxa_users_panier_CS_DON with (nolock)';
    public string $titre;
    protected array $primKey = ['id_user', 'id_panier', 'NUM_FIX', 'ID_LIGNE', 'COD_TAB'];

    /**
     * @return array<mixed>
     */
    public function getArchitecture(\Helpers\Trad\ITrad $trad)
    {
        return  [];
    }

    public function clearCsLigne(string $idPanier, string $idUser):void
    {
        $req = "delete from oxa_users_panier_CS_DON where id_user = :idUser and id_panier = :idPanier and ID_LIGNE != '0'
                and not exists (SELECT * FROM oxa_users_panier_ligne where id_lig = oxa_users_panier_CS_DON.ID_LIGNE)";

        $arrayReq = [
            ':idUser' => $idUser,
            ':idPanier' => $idPanier,
        ];

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

    /**
     * @param string $idUser
     * @param string $idPanier
     * @return array<mixed>
     */
    public function getListeCSWithFormat(string $idUser, string $idPanier)
    {
        $req = "select oxa_users_panier_CS_DON.*,CS_TYPES.FORMAT  from oxa_users_panier_CS_DON with (nolock)
        left join CS_CHPS with (nolock) on CS_CHPS.COD_TAB = oxa_users_panier_CS_DON.COD_TAB and  CS_CHPS.NUM_FIX = oxa_users_panier_CS_DON.NUM_FIX
        left join CS_TYPES  (nolock) on CS_TYPES.TYPE = CS_CHPS.TYPE
        where id_user = :idUser and id_panier = :idPanier";

        $arrayReq = [
            ':idUser' => $idUser,
            ':idPanier' => $idPanier,
        ];

        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
        $retour = $requete->fetchAll($this->pdo::FETCH_ASSOC);
        if ($retour) {
            return $retour;
        } else {
            return [];
        }
    }
}
