<?php declare(strict_types=1);

namespace Modules\Additional\Specifique\Client112\Modules\Facture\Controller;

use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Helpers\Pagination\PaginatorGestion;
use Skeleton\Controller\Controller;
use Modules\Additional\Specifique\Client112\Modules\Facture\Service\ParamSIContratFournisseurService;

class ParamSIContratFournisseurController extends Controller
{
    public function createViewAction(
        ParamSIContratFournisseurService $paramSIContratFournisseurService,
    ): string {
        $post = $this->request->getParsedBody();

        return $this->renderer->render('@Specifique112/Modules/Facture/view/affectation-si-facture/paramSIContratCreateOrUpdate', [
            'menuLeft'   => 'affectationSIContrat',
            'menuActive' => 'Facture',
            'post'       => $post,
        ]);
    }
    public function createAction(
        ParamSIContratFournisseurService $paramSIContratFournisseurService,
    ): Response {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        if (!empty($post)) {
            $validator = $this->createValidator();
            foreach ($post['CODE_FRS'] as $key => $value) {
                if (empty($value) || empty($post['CONTRACT_NUMBER'][$key]) || empty($post['SECT_IMP'][$key])) {
                    continue;
                }
                $validator->zoom('Fourniss', $value, $trad->t('Fournisseur'), $this->pdo, false);
                $validator->isStringAvecEspace($trad->t('Numéro de Contrat'), $post['CONTRACT_NUMBER'][$key], 1, 3000);
                $validator->zoom('Sectimpu', $post['SECT_IMP'][$key], $trad->t('Section d\'imputation'), $this->pdo, false);
            }
            if ($validator->isValid()) {
                foreach ($post['CODE_FRS'] as $key => $value) {
                    if (empty($value) || empty($post['CONTRACT_NUMBER'][$key]) || empty($post['SECT_IMP'][$key])) {
                        continue;
                    }
                    $paramSIContratFournisseurService->insert($value, $post['CONTRACT_NUMBER'][$key], $post['SECT_IMP'][$key]);
                }
            }
        }

        return new Response();
    }

    public function updateViewAction(
        ParamSIContratFournisseurService $paramSIContratFournisseurService,
    ): string {
        $post           = $this->request->getParsedBody();
        $codeFrs        = $this->request->getAttribute('codeFrs');
        $contractNumber = $this->request->getAttribute('contractNumber');
        $codeSI         = $this->request->getAttribute('codeSI');

        return $this->renderer->render('@Specifique112/Modules/Facture/view/affectation-si-facture/paramSIContratCreateOrUpdate', [
            'menuLeft'       => 'affectationSIContrat',
            'menuActive'     => 'Facture',
            'post'           => $post,
            'update'         => true,
            'codeFrs'        => $codeFrs,
            'contractNumber' => $contractNumber,
            'codeSI'         => $codeSI,
        ]);
    }
    public function updateAction(
        ParamSIContratFournisseurService $paramSIContratFournisseurService,
    ): Response {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        if (!empty($post)) {
            $validator = $this->createValidator();
            foreach ($post['CODE_FRS'] as $key => $value) {
                $validator->zoom('Fourniss', $value, $trad->t('Fournisseur'), $this->pdo, false);
                $validator->isStringAvecEspace($trad->t('Numéro de Contrat'), $post['CONTRACT_NUMBER'][$key], 1, 50);
                $validator->zoom('Sectimpu', $post['SECT_IMP'][$key], $trad->t('Section d\'imputation'), $this->pdo, false);
            }
            if ($validator->isValid()) {
                foreach ($post['CODE_FRS'] as $key => $value) {
                    $paramSIContratFournisseurService->update(
                        $value,
                        $post['CONTRACT_NUMBER'][$key],
                        $post['SECT_IMP'][$key],
                        $post['oldCodeFrs'],
                        $post['oldContractNumber'],
                        $post['oldCodeSI'],
                    );
                }
            }
        }

        return new Response();
    }
    public function deleteAction(
        ParamSIContratFournisseurService $paramSIContratFournisseurService,
    ): Response {
        $post           = $this->request->getParsedBody();
        $codeFrs        = $this->request->getAttribute('codeFrs');
        $contractNumber = $this->request->getAttribute('contractNumber');
        $codeSI         = $this->request->getAttribute('codeSI');

        $paramSIContratFournisseurService->delete(
            $codeFrs,
            $contractNumber,
            $codeSI,
        );

        return new Response();
    }

    /**
     * Summary of readAction
     * @param ParamSIContratFournisseurService $paramSIContratFournisseurService
     * @return string
     */
    public function readAction(
        ParamSIContratFournisseurService $paramSIContratFournisseurService,
        PaginatorGestion $paginator,
    ) {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        $paginator->generate(0, 'CODE_FRS');

        $list = $paramSIContratFournisseurService->findAll($post);

        Ariane::generate(0, $trad->t('Paramétrage'));

        return $this->renderer->render('@Specifique112/Modules/Facture/view/affectation-si-facture/paramSIContratList', [
            'menuLeft'       => 'affectationSIContrat',
            'menuActive'     => 'Facture',
            'list'           => $list,
            'post'           => $post,
            'infoPagination' => PaginatorGestion::getInfoPagination(),
        ]);
    }
}
