<?php

namespace Modules\Ajax\Controller;

use Entity\Model\SSiteSto;
use Exception;
use GuzzleHttp\Psr7\Response;
use Helpers\Pagination\PaginatorGestion;
use Modules\Ajax\Repository\InitCS;
use Modules\Stock\Service\CsInitializerService;
use Skeleton\Controller\Controller;

class InitChampController extends Controller
{


    public function csInitAction() :Response
    {
        
        $post = $this->request->getParsedBody();
        $TABDESTI = $post['TABDESTI'];
        $CODENR =  $post['CODENR'];
        $TAB_SOUR =  $post['TAB_SOUR'];
        $cs = new InitCS($this->pdo);
        if ($TAB_SOUR == 'PROPRIETAIRE') {
            $TAB_SOUR = 'HASTUTIL';
        } elseif ($TAB_SOUR == 'EMET') {
            $TAB_SOUR = 'CENTRE';
        } else {
            return new Response(200, [], '0');
        }
        $liste = $cs->listeFavorisAccesRapide($TABDESTI, $CODENR, $TAB_SOUR);

        
        return $this->createJsonResponse($liste);
    }

    /**
     * @throws Exception
     */
    public function initializeCsAction(): string
    {
        $post = $this->request->getParsedBody();
        $codeTab = $post['TABLE_DESTINATAIRE'];
        $codeEnr = $post['CODE_ENR'];
        $tablesSources = json_decode($post['TABLES_SOURCES'], true);
        (new CsInitializerService($this->pdo))->initializeCs($post, $codeTab, $codeEnr, $tablesSources, $post['CODE_STE']);

        return $this->renderer->render('@Ajax/initChamp/initialize_cs', [
            'post' => $post,
            'codeTab' => $codeTab,
            'codeEnr' => $codeEnr,
        ]);
    }
}
