<?php

namespace Modules\Reception\Controller;

use Entity\Model\BrLigne;
use Entity\Model\Fourninc;
use Entity\Model\Incident;
use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\Parametres\Constante;
use Modules\Reception\Repository\ControleQualite;
use Modules\Reception\Repository\Incident as RepositoryIncident;
use Modules\Reception\Service\ControleQualiteService;
use Modules\Reception\Service\IncidentService;
use Skeleton\Controller\Controller;

class ControleQuanliteController extends Controller
{
    public function listeAction() : string
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        $listeCQ = (new ControleQualite($this->pdo))->getControleQualite();

        Ariane::generate(0, $trad->t('Contrôle quanlité'));

        return $this->renderer->render('@Reception/ControleQualite/liste', [
            'menuActive'    => 'eproc',
            'post'          => $post,
            'listeCQ'       => $listeCQ,
        ]);
    }

    public function ajaxDetailControleAction() : string
    {
        $idDoc      = $this->request->getAttribute('idDoc');
        $typeCtrl   = $this->request->getAttribute('typeCtrl');

        $listeLigne = (new ControleQualite($this->pdo))->getListeLigneCQByType($idDoc, $typeCtrl);
        
        return $this->renderer->render('@Reception/ControleQualite/detail', [
            'listeLigne' => $listeLigne,
        ]);
    }

    public function saisieControleQualiteAction() : string
    {
        $idDoc      = $this->request->getAttribute('idDoc');
        $post       = $this->request->getParsedBody();
        $trad       = $this->getTranslationService();

        $listeLigne = (new ControleQualite($this->pdo))->getListeLigneCQ($idDoc);

        foreach ($listeLigne as &$ligne) {
            $ligne['PU_REMISE'] =  $ligne['PU'] * (1 - $ligne['REMISE']);
        }

        Ariane::generate(1, $trad->t('Saisie du contrôle qualité'));

        return $this->renderer->render('@Reception/ControleQualite/saisie', [
            'listeLigne' => $listeLigne,
            'idDoc' => $idDoc,
            'post' => $post,
            'droits' => [
                'HA_BR_PX' => Roles::droitsUtilisateur($this->pdo, 'HA_BR_PX'),
            ],
        ]);
    }

    /**
     * @return string|Response
     */
    public function validateAction()
    {
        $idDoc          = $this->request->getAttribute('idDoc');
        $post           = $this->request->getParsedBody();
        $trad           = $this->getTranslationService();
        $validator      = $this->createValidator();

        $controleQualiteService = new ControleQualiteService($this->pdo, $trad);
        $dataValider = $controleQualiteService->validator($validator, $post);

        if ($validator->isValid()) {
            $controleQualiteService->saveControleQualite($idDoc, $dataValider['updateLigne'], $dataValider['updateIncident']);

            return new Response(443, [], (string) \json_encode([
                'statut'    => 'O',
                'path'      => Ariane::getBackPath(),
                'post'      => Ariane::getBackPost(),
            ]));
        } else {
            return $this->renderer->render('@popup-erreur/ErreurCreation', [
                'ErreurValid' => $validator->getError(),
            ]);
        }
    }
}
