<?php

namespace Modules\Additional\Specifique\Client13\Modules\Panier\Controller;

use Entity\Model\OxaUserPanierCsDon;
use Entity\Model\OxaUserPanierFour;
use GuzzleHttp\Psr7\Response;
use Helpers\Outil\Sql;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;
use Modules\Ajax\Repository\Listes;
use Modules\Additional\Specifique\Client13\Modules\Panier\Service\CSPanierService;
use Skeleton\Controller\Controller;

class CSPanierController extends Controller
{
    
    
    /**
     * pageInit
     *
     * @return void
     */
    public function pageInit()
    {
    }
    
    /**
     * popupCSPanierAction
     *
     * @return string
     */
    public function popupCSPanierAction() : string
    {
        $csPanierService = new CSPanierService($this->pdo);
        $listeCSinit = $csPanierService->numCS();

        $listeOpe = [
            $listeCSinit['numCSMutualisation']  => 'Mutualisation',
            $listeCSinit['numCSAcdVeolia']      => 'Accord-cadre Veolia',
            $listeCSinit['numCSAcdSEM']         => 'Accord-cadre groupe SEM',
            $listeCSinit['numCSCttSupEgal']     => 'Contrat > ou = à X K euros',
            $listeCSinit['numCSCttInf']         => 'Contrat w à X K euros',
            $listeCSinit['numCSConsultSimplie'] => 'Consultation simplifiée',
            $listeCSinit['numCSCdeSansConsult'] => 'Commande sans consultation',
        ];

        $oxaUserPanierCsDon = new OxaUserPanierCsDon($this->pdo);
        $listeCSTMP = $oxaUserPanierCsDon->findAll([
            'id_user' => $_SESSION['id_user'],
            'id_panier' => $_SESSION['id_panier'],
            'COD_TAB' => 'DA_ENTET',
            'ID_LIGNE' => '0'
        ]);

        $listeCS = [];

        $typeOpe = null;
        foreach ($listeCSTMP as $cs) {
            $listeCS[$cs['NUM_FIX']] = $cs['VALEUR'];

            if ($cs['VALEUR'] == 'O' && isset($listeOpe[$cs['NUM_FIX']])) {
                $typeOpe = $cs['NUM_FIX'];
            }
        }

        $oxaUserPanierFourEntity = new OxaUserPanierFour($this->pdo);
        $listeFourPanier = $oxaUserPanierFourEntity->findAll(['id_user' => $_SESSION['id_user'], 'id_panier' => $_SESSION['id_panier']]);

        $listeAcd = [];
        if (count($listeFourPanier) == 1) {
            $codeFour = $listeFourPanier[0]['COD_FOURN'];

            $listeRepo = new Listes($this->pdo);
            $listeAcdTmp = $listeRepo->AccordCadre(['CODE_FOUR' => $codeFour, 'valide' => 1]);

            foreach ($listeAcdTmp as $acd) {
                $listeAcd[$acd['CODE_ACD']] = $acd['LIBELLE'];
            }
        }

        return $this->renderer->render('@Specifique13/popupCSPanier', [
            'listeOpe' => $listeOpe,
            'listeCS' => $listeCSinit,
            'typeOpe' => $typeOpe,
            'listeAcd' => $listeAcd,
            'listeFourPanier' => $listeFourPanier,
            'valueCS' => $listeCS,
        ]);
    }

    /**
     * @return Response
     */
    public function validateCsAction()
    {
        $post    = $this->request->getParsedBody();

        $validator = $this->createValidator();
        $trad = $this->getTranslationService();

        $updateCS = [];
        $csPanierService = new CSPanierService($this->pdo);
        $listeCSinit = $csPanierService->numCS();

        $listeOpe = [
            $listeCSinit['numCSMutualisation']  => 'Mutualisation',
            $listeCSinit['numCSAcdVeolia']      => 'Accord-cadre Veolia',
            $listeCSinit['numCSAcdSEM']         => 'Accord-cadre groupe SEM',
            $listeCSinit['numCSCttSupEgal']     => 'Contrat > ou = à X K euros',
            $listeCSinit['numCSCttInf']         => 'Contrat w à X K euros',
            $listeCSinit['numCSConsultSimplie'] => 'Consultation simplifiée',
            $listeCSinit['numCSCdeSansConsult'] => 'Commande sans consultation',
        ];

        if ($post['TYPE_OPE'] == $listeCSinit['numCSAcdVeolia'] || $post['TYPE_OPE'] == $listeCSinit['numCSAcdSEM']) {
            $acd = $post['CS_EXTENTIONdebut-----finDA_ENTETdebut-----findebut-----fin'.$listeCSinit['numAcd']];

            $validator->isObligatoire($acd, $trad->t('accord-cadre'));
        } else {
            $acd = '';
        }

        foreach ($listeOpe as $keyCS => $cs) {
            if ($keyCS == $post['TYPE_OPE']) {
                $valeur = 'O';
            } else {
                $valeur = 'N';
            }

            $updateCS[] = [
                'key' => [
                    'id_user'       => $_SESSION['id_user'],
                    'id_panier'     => $_SESSION['id_panier'],
                    'NUM_FIX'       => $keyCS,
                    'COD_TAB'       => 'DA_ENTET',
                    'ID_LIGNE'      => '0',
                ],
                'value' => $valeur,
            ];
        }

        $updateCS[] = [
            'key' => [
                'id_user'       => $_SESSION['id_user'],
                'id_panier'     => $_SESSION['id_panier'],
                'NUM_FIX'       => $listeCSinit['numAcd'],
                'COD_TAB'       => 'DA_ENTET',
                'ID_LIGNE'      => '0',
            ],
            'value' => $acd,
        ];

        $CSDerog = 'N';
        $CSDerogTxt = '';
        if (isset($post['CS_EXTENTIONdebut-----finDA_ENTETdebut-----findebut-----fin'.$listeCSinit['numCSDerog']])) {
            $CSDerog = 'O';

            $validator->isStringAvecEspace('', $post['CS_EXTENTIONdebut-----finDA_ENTETdebut-----findebut-----fin'.$listeCSinit['numCSDerogTxt']], 0, 30);
        
            $CSDerogTxt = $post['CS_EXTENTIONdebut-----finDA_ENTETdebut-----findebut-----fin'.$listeCSinit['numCSDerogTxt']];
        }

        $updateCS[] = [
            'key' => [
                'id_user'       => $_SESSION['id_user'],
                'id_panier'     => $_SESSION['id_panier'],
                'NUM_FIX'       => $listeCSinit['numCSDerog'],
                'COD_TAB'       => 'DA_ENTET',
                'ID_LIGNE'      => '0',
            ],
            'value' => $CSDerog,
        ];

        $updateCS[] = [
            'key' => [
                'id_user'       => $_SESSION['id_user'],
                'id_panier'     => $_SESSION['id_panier'],
                'NUM_FIX'       => $listeCSinit['numCSDerogTxt'],
                'COD_TAB'       => 'DA_ENTET',
                'ID_LIGNE'      => '0',
            ],
            'value' => $CSDerogTxt,
        ];

        if ($validator->isValid()) {
            $oxaUserPanierCsDon = new OxaUserPanierCsDon($this->pdo);

            foreach ($updateCS as $cs) {
                $oxaUserPanierCsDon->update(['VALEUR' => $cs['value']], $cs['key']);
            }

            return new Response(200, [], (string) \json_encode([
                'STATUT' => 'O',
            ]));
        } else {
            return new Response(200, [], (string) \json_encode([
                'STATUT' => 'E',
                'erreur' => $validator->getError(),
            ]));
        }
    }
}
