<?php

namespace App\Controller\Frontend;


use App\Controller\AbstractController;
use App\Entity\IndividualData\IndividualData;
use App\Form\IndividualDataForm;
use App\Repository\Site\BlockStore;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ContribuateController extends AbstractController
{
    public function __construct(
        EntityManagerInterface $em,
        BlockStore $blockStore
    ) {
        $this->blockStore = $blockStore;
        parent::__construct($em);
    }

    public function contribuate(Request $request, EntityManagerInterface $entityManager): Response
    {
        $blockContrib = $this->blockStore->getOneOnlineByDevAlias('contribution-individuel');

        $individualData = new IndividualData();
        $form = $this->createForm(IndividualDataForm::class, $individualData, ["context" => "frontend"]);


        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $individualData->setStatus(0);
            $entityManager->create($individualData);
            $entityManager->flush();
            $this->addFlash('success', 'Votre contribution a bien été prise en compte !');
        }

        return $this->render(
            'frontend/contribuate.html.twig',
            [
                'nbContrib' => $this->countContrib(),
                'form' => $form->createView(),
                'blockContrib' => $blockContrib,
            ]
        );
    }
}