<?php

namespace Modules\Tables\Service;

use Entity\Model\AdrCtr;
use Entity\Model\Centre;
use Entity\Model\Country;
use Entity\Model\CtcCtr;
use Entity\Model\DGeoCtre;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Fichier\Fichier;
use Helpers\Fichier\Gestion_Fic_PJ;
use Helpers\Outil\Sql;
use Helpers\Parametres\Constante;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;

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

    /**
     * @param array<mixed> $post
     * @param mixed $file
     * @param array<mixed> $centre
     */
    public function updateInsertCentre($post, $file, $centre = null) : void
    {
        $gestionFic = new Gestion_Fic_PJ($this->pdo, true);
        $centreEntity   = new Centre($this->pdo);
        $paysEntity     = new Country($this->pdo);

        $logoEntete = false;
        if ($file['importLOGO_ENT']??false) {
            $file_nameEntete = $file['importLOGO_ENT']->getClientFilename();

            $newPath = Fichier::UploadDocAtt($file_nameEntete);
            $nameFile = pathinfo($file_nameEntete);
            $nameFile = $nameFile['filename'].'.'.($nameFile['extension']??'');
            
            $file['importLOGO_ENT']->moveTo($newPath["racine"].$newPath["new_name"]);
            if ($centre) {
                $logoEntete = $gestionFic->updateFicher($centre['LOGO_ENT'], $newPath['new_name']);
            } else {
                $logoEntete = $gestionFic->insertFichier($newPath['new_name']);
            }

            $logoEntete = '%_images%'.$logoEntete;
        }

        $logoPied = false;
        if ($file['importLOGO_PIE']??false) {
            $file_namePied = $file['importLOGO_PIE']->getClientFilename();

            $newPath = Fichier::UploadDocAtt($file_namePied);
            $nameFile = pathinfo($file_namePied);
            $nameFile = $nameFile['filename'].'.'.($nameFile['extension']??'');
            
            $file['importLOGO_PIE']->moveTo($newPath["racine"].$newPath["new_name"]);
            if ($centre) {
                $logoPied = $gestionFic->updateFicher($centre['LOGO_PIE'], $newPath['new_name']);
            } else {
                $logoPied = $gestionFic->insertFichier($newPath['new_name']);
            }

            $logoPied = '%_images%'.$logoPied;
        }

        if ($centre) {
            $arrayUpdate = [
                'RAIS_SOC'                  => $post['RAIS_SOC'],
                'ADRESSE'                   => $post['ADRESSE'],
                'VILLE'                     => $paysEntity->formatingCPV($post['PAYS'], $post['CPOSTAL'], $post['VILLE1']),
                'PAYS'                      => $post['PAYS'],
                'TELEPHONE'                 => $post['TELEPHONE'],
                'FAX'                       => $post['FAX'],
                'E_MAIL'                    => $post['E_MAIL'],
                'NUM_TVAIC'                 => $post['NUM_TVAIC'],
                'ZONELEGAL'                 => $post['ZONELEGAL'],
                'CODE_STE'                  => $post['CODE_STE'],
                'numerique_EMISSION'        => sql::setBoolSql($post['EMISSION']),
                'numerique_LIVRAISON'       => sql::setBoolSql($post['LIVRAISON']),
                'numerique_FACTURAT'        => sql::setBoolSql($post['FACTURAT']),
                'CTRFACLIE'                 => $post['CTRFACLIE'],
                'PROFILDA'                  => $post['PROFILDA'],
                'SITSTOCK'                  => $post['SITSTOCK'],
                'CPOSTAL'                   => $post['CPOSTAL'],
                'VILLE1'                    => $post['VILLE1'],
                'NM_COMM'                   => Sql::updateMemo($this->pdo, '', $post['NM_COMM']),
                'TX_TAXLOC'                 => $post['TX_TAXLOC'],
                'ACHETEUR'                  => $post['ACHETEUR'],
                'numerique_BRAUTOPREAFF'    => sql::setBoolSql($post['BRAUTOPREAFF']),

                //prochaine version quand on traitera la partie transit
                //'TRS_INTER'     => $post[''],
                //'MOD_TRS'       => $post[''],
                //'DEV_FEX'       => $post[''],
                //'SEPMIL'        => $post[''],
                //'SEPDEC'        => $post[''],
                //'LNGFACT'       => $post[''],
                //'COD_MQT'       => $post[''],
            ];

            if ($logoEntete) {
                $arrayUpdate['LOGO_ENT'] = $logoEntete;
            } elseif ($post['file_nameLOGO_ENT'] === '') {
                $arrayUpdate['LOGO_ENT'] = '';
            }

            if ($logoPied) {
                $arrayUpdate['LOGO_PIE'] = $logoPied;
            } elseif ($post['file_nameLOGO_PIE'] === '') {
                $arrayUpdate['LOGO_PIE'] = '';
            }

            $centreEntity->update($arrayUpdate, $centre['CODE_CTR']);

            ChampSupp::updateCS($post, $this->pdo);
        } else {
            $centreEntity->insert([
                'CODE_CTR'                  => $post['CODE_CTR'],
                'RAIS_SOC'                  => $post['RAIS_SOC'],
                'ADRESSE'                   => $post['ADRESSE'],
                'VILLE'                     => $paysEntity->formatingCPV($post['PAYS'], $post['CPOSTAL'], $post['VILLE1']),
                'PAYS'                      => $post['PAYS'],
                'TELEPHONE'                 => $post['TELEPHONE'],
                'FAX'                       => $post['FAX'],
                'E_MAIL'                    => $post['E_MAIL'],
                'NUM_TVAIC'                 => $post['NUM_TVAIC'],
                'ZONELEGAL'                 => $post['ZONELEGAL'],
                'CODE_STE'                  => $post['CODE_STE'],
                'numerique_EMISSION'        => sql::setBoolSql($post['EMISSION']),
                'numerique_LIVRAISON'       => sql::setBoolSql($post['LIVRAISON']),
                'numerique_FACTURAT'        => sql::setBoolSql($post['FACTURAT']),
                'CTRFACLIE'                 => $post['CTRFACLIE'],
                'LOGO_ENT'                  => $logoEntete?:'',
                'LOGO_PIE'                  => $logoPied?:'',
                'PROFILDA'                  => $post['PROFILDA'],
                'SITSTOCK'                  => $post['SITSTOCK'],
                'CPOSTAL'                   => $post['CPOSTAL'],
                'VILLE1'                    => $post['VILLE1'],
                'NM_COMM'                   => sql::updateMemo($this->pdo, '', $post['NM_COMM']),
                'TX_TAXLOC'                 => $post['TX_TAXLOC'],
                'ACHETEUR'                  => $post['ACHETEUR'],
                'numerique_BRAUTOPREAFF'    => sql::setBoolSql($post['BRAUTOPREAFF']),

                //prochaine version quand on traitera la partie transit
                //'TRS_INTER'     => $post[''],
                //'MOD_TRS'       => $post[''],
                //'DEV_FEX'       => $post[''],
                //'SEPMIL'        => $post[''],
                //'SEPDEC'        => $post[''],
                //'LNGFACT'       => $post[''],
                //'COD_MQT'       => $post[''],
            ]);

            ChampSupp::ajouterCS($post, $this->pdo, $post['CODE_CTR']);
        }
    }

    /**
     * @param array<mixed> $listeAdresse
     */
    public function updateInsertAdresse(string $idTable, $listeAdresse) : void
    {
        $paysEntity     = new Country($this->pdo);
        $adrCtrEntity   = new AdrCtr($this->pdo);

        foreach ($listeAdresse as $adresse) {
            if ($adresse['statut'] == 'S') {
                $adrCtrEntity->delete([
                    'COD_CTR' => $idTable,
                    'COD_ADR' => $adresse['COD_ADR'],
                ]);
            } elseif ($adresse['statut'] == 'A') {
                $adrCtrEntity->insert([
                    'COD_CTR'       => $idTable,
                    'COD_ADR'       => $adresse['COD_ADR'],

                    'LIBELLE'       => $adresse['LIBELLE'],
                    'ADRESSE'       => $adresse['ADRESSE'],
                    'VILLE'         => $paysEntity->formatingCPV($adresse['PAYS'], $adresse['CPOSTAL'], $adresse['VILLE1']),
                    'CODPAYS'       => $adresse['PAYS'],
                    'TELEPHONE'     => $adresse['TELEPHONE'],
                    'FAX'           => $adresse['FAX'],
                    'EMAIL'         => $adresse['E_MAIL'],
                    'CPOSTAL'       => $adresse['CPOSTAL'],
                    'VILLE1'        => $adresse['VILLE1'],
                ]);
            } elseif ($adresse['statut'] == 'M' || $adresse['statut'] == 'U') {
                $adrCtrEntity->update([
                    'LIBELLE'       => $adresse['LIBELLE'],
                    'ADRESSE'       => $adresse['ADRESSE'],
                    'VILLE'         => $paysEntity->formatingCPV($adresse['PAYS'], $adresse['CPOSTAL'], $adresse['VILLE1']),
                    'CODPAYS'       => $adresse['PAYS'],
                    'TELEPHONE'     => $adresse['TELEPHONE'],
                    'FAX'           => $adresse['FAX'],
                    'EMAIL'         => $adresse['E_MAIL'],
                    'CPOSTAL'       => $adresse['CPOSTAL'],
                    'VILLE1'        => $adresse['VILLE1'],
                ], [
                    'COD_CTR' => $idTable,
                    'COD_ADR' => $adresse['COD_ADR'],
                ]);
            }
        }
    }

    /**
     * @param array<mixed> $listeContact
     */
    public function updateInsertContact(string $idTable, $listeContact): void
    {
        $ctcCtrEntity   = new CtcCtr($this->pdo);
        foreach ($listeContact as $contact) {
            if ($contact['statut'] == 'S') {
                $ctcCtrEntity->delete([
                    'COD_CTR' => $idTable,
                    'CTC_IND' => $contact['CTC_IND'],
                ]);
            } elseif ($contact['statut'] == 'A') {
                $ctcCtrEntity->insert([
                    'COD_CTR'               => $idTable,
                    'CTC_IND'               => $contact['CTC_IND'],

                    'CTC_NOM'               =>  $contact['CTC_NOM'],
                    'CTC_MAIL'              =>  $contact['CTC_MAIL'],
                    'CTC_GSM'               =>  $contact['CTC_GSM'],
                    'CTC_TEL'               =>  $contact['CTC_TEL'],
                    'CTC_FAX'               =>  $contact['CTC_FAX'],
                    'CTC_FCT'               =>  $contact['CTC_FCT'],
                    'numerique_CTC_RCA'     =>  sql::setBoolSql($contact['CTC_RCA']),
                    'numerique_CTC_ROP'     =>  sql::setBoolSql($contact['CTC_ROP']),
                    'numerique_CTC_DARDA'   =>  sql::setBoolSql($contact['CTC_DARDA']),
                    //'numerique_CTC_REN'     =>  sql::setBoolSql($contact['CTC_REN']),
                ]);
            } elseif ($contact['statut'] == 'M' || $contact['statut'] == 'U') {
                $ctcCtrEntity->update([
                    'CTC_IND'               =>  $contact['CTC_IND'],
                    'CTC_NOM'               =>  $contact['CTC_NOM'],
                    'CTC_MAIL'              =>  $contact['CTC_MAIL'],
                    'CTC_GSM'               =>  $contact['CTC_GSM'],
                    'CTC_TEL'               =>  $contact['CTC_TEL'],
                    'CTC_FAX'               =>  $contact['CTC_FAX'],
                    'CTC_FCT'               =>  $contact['CTC_FCT'],
                    'numerique_CTC_RCA'     =>  sql::setBoolSql($contact['CTC_RCA']),
                    'numerique_CTC_ROP'     =>  sql::setBoolSql($contact['CTC_ROP']),
                    'numerique_CTC_DARDA'   =>  sql::setBoolSql($contact['CTC_DARDA']),
                    //'numerique_CTC_REN'     =>  sql::setBoolSql($contact['CTC_REN']),
                ], [
                    'COD_CTR' => $idTable,
                    'CTC_IND' => $contact['CTC_IND'],
                ]);
            }
        }
    }

    /**
     * @param array<mixed> $listeZoneGeo
     */
    public function updateInsertzoneGeo(string $idTable, $listeZoneGeo): void
    {
        $dGeoCtrEntity  = new DGeoCtre($this->pdo);

        $dGeoCtrEntity->delete(['CODE_CTR' => $idTable]);

        foreach ($listeZoneGeo as $zoneGeo) {
            $dGeoCtrEntity->insert([
                'CODE_CTR'  => $idTable,
                'DCOUP'     => $zoneGeo['DCOUP'],
                'ZONEGEO'   => $zoneGeo['ZONEGEO'],
            ]);
        }
    }
}
