<?php

namespace Traits\Validator;

use Helpers\CaracCompl\CaracCompl as CaracComplCaracCompl;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Sql;
use Helpers\Trad\ITrad;
use Helpers\Validator\Validator;

trait CaracCompl
{

    abstract public function getTrad() :ITrad;

    /**
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function validatorCC(\PDO $pdo, &$post, string $idPost = '')
    {
        $array = json_decode(($post['saveValidatorCC'.$idPost]??''), true);

        $init       = $array['init']??'';
        $typeDoc    = $array['typeDoc']??'';
        $idDoc      = $array['idDoc']??'';
        $idLigne    = $array['idLigne']??'';
        $idPanier   = $array['idPanier']??'0';
        $article    = $array['article']??'';
        $ssFamille  = $array['ssFamille']??'';
        $famille    = $array['famille']??'';
        $societe    = $array['societe']??'';

        $Validator = new Validator($pdo, $this->getTrad());

        $caracCompl = new CaracComplCaracCompl($pdo, $article, $ssFamille, $famille);
        if ($init) {
            $listeCC = $caracCompl->getListeCCInit();
        } else {
            $listeCC = $caracCompl->getCCDocc($typeDoc, $idDoc, $idLigne, $idPanier);
        }
        
        $returnCS = [];
        foreach ($listeCC as $cc) {
            if (sql::boolSql($cc['CC_IS_TITLE'])) {
                continue;
            }

            $key = 'CC_'.$cc['CC_ID'];

            if ($cc['FORMAT']=='L') {
                $value = null;
                if (isset($post[$key])) {
                    $value = $post[$key];
                }
            } else {
                $value = $post[$key]??'';
            }

            $obli = (Sql::toBool($cc['CC_REQUIRED']) && (($typeDoc == 'BR' || $typeDoc == 'BRT') || ($typeDoc != 'BR' && $typeDoc != 'BRT' && !SQL::boolSql($cc['CC_UPD_ONLY_RECEIPT']))))? 1:0;

            $array = ChampSupp::getListeTableStd();
            $zoom=[];
            foreach ($array as $k => $valeur) {
                if ($k == $cc['CC_TYPE']) {
                    $zoom=$array[$k];
                    break;
                }
            }

            if (sql::boolSql($cc['TBLSECTO'])) {
                if ($societe == '') {
                    $societe = $_SESSION['Session_SocUsr'];
                }

                $codeSte = $societe;
            } else {
                $codeSte = '';
            }

            if (sql::boolSql($cc['CONTROLE']) && !sql::boolSql($cc['CTRL_STD'])) {
                $Validator->validatorTSDON($cc['NOM'], $value, $cc['CC_TYPE'], $pdo, !$obli, $codeSte);
            } elseif ($zoom) {
                if (key_exists(2, $zoom)) {
                    $where = [$zoom[2] => 'T'];
                } else {
                    $where = array();
                }

                $Validator->zoom($zoom[0], $value, $cc['NOM'], $pdo, !$obli, $where);
            } elseif ($cc['FORMAT']=='D') {
                $Validator->isDate($value, '', '', !$obli, $cc['NOM']);

                $post[$key]=$value;
            } elseif ($cc['FORMAT']=='N' || $cc['FORMAT']=='1') {
                $value = str_replace(' ', '', $value);
                $post[$key]=$value;
                $Validator->isFloat($cc['NOM'], $value, $cc['PE_LONG'].'.'.$cc['PD_LONG'], $obli);
                $post[$key]=$value;
            } elseif ($cc['FORMAT']=='L') {
                $Validator->isCheck($value);
                if (isset($this->setValueForWSDL) && $this->setValueForWSDL) {
                    $post[$key] = $value? 'O':'N';
                } else {
                    $post[$key] = $value;
                }
            } elseif ($cc['FORMAT']=='M') {
                $Validator->isStringAvecEspace($cc['NOM'], $value, $obli, '3000');
            } elseif ($cc['FORMAT']=='H') {
                $Validator->isHourWithSeconde($cc['NOM'], $value);
            } elseif ($cc['FORMAT']=='E') {
                $Validator->adresseMail($cc['NOM'], $value, $obli, 100, true);
            } elseif ($cc['FORMAT']=='U') {
                $Validator->isUrlValide($cc['NOM'], $value, $obli);
            } else {
                $Validator->isStringAvecEspace($cc['NOM'], $value, $obli, ($cc['ALP_LONG']=='0'? '':$cc['ALP_LONG']));
            }

            $returnCS[$cc['CC_ID']] = $post[$key];
        }

        $this->validate += $Validator->validate;
        $this->error .= $Validator->error;

        return $returnCS;
    }
}
