<?php

namespace App\Controller\BackendAdmin;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\Routing\Annotation\Route;

use Doctrine\ORM\EntityManagerInterface;

use App\Repository\SocieteRepository;

use App\Entity\Societe;

use App\Form\BackendAdmin\SocieteType;

use App\Service\UploaderService;

#[Route('/backend/admin/societe')]
class SocieteController extends AbstractController
{


    /**
     * @var SocieteRepository $societeRepository
     */
    private $societeRepository;

    /**
     * @var EntityManagerInterface $em
     */
    private $em;

    /**
     * @param SocieteRepository $societeRepository
     * @param EntityManagerInterface $em
     * @return $this
     */
    public function __construct(
        SocieteRepository $societeRepository,
        EntityManagerInterface $em
    ){
        $this->societeRepository = $societeRepository;
        $this->em = $em;
    }


    #[Route('/index', name: 'backend_admin_societe_index')]
    public function index(): Response
    {
        return $this->render('backend_admin/societe/index.html.twig', ['societes'=> $this->societeRepository->findBy([], ["id"=>'DESC'])]);
    }

    
    #[Route('/add', name: 'backend_admin_societe_add')]
    public function add(Request $request, UploaderService $uploaderService): Response
    {
        $societe = new Societe();
		
		$form = $this->createForm(SocieteType::class, $societe);

        $form->handleRequest($request);
		if ($form->isSubmitted() && $form->isValid()) {

            $photo = $form->get('photo')->getData();
            if ($photo) {
                $directory = $this->getParameter('societes_directory');
                $societe->setLogo($uploaderService->uploadFile($photo, $directory));
            }
            $this->em->persist($societe);
			$this->em->flush();
		}

        return $this->render('backend_admin/societe/add_update.html.twig',['form'=>$form]);
    }


    #[Route('/update/{id}', name: 'backend_admin_societe_update')]
    public function update(Request $request, Societe $societe, UploaderService $uploaderService): Response
    {
        $form = $this->createForm(SocieteType::class, $societe);

        $form->handleRequest($request);
		if ($form->isSubmitted() && $form->isValid()) {
			
            $photo = $form->get('photo')->getData();
            if ($photo) {
                $pathImage = $this->getParameter('societes_directory').'/'.$societe->getLogo();
                $directory = $this->getParameter('societes_directory');
                $societe->setLogo($uploaderService->uploadFile($photo, $directory));
                if(file_exists($pathImage)){
                    unlink($pathImage);
                }

            }

			$this->em->persist($societe);
			$this->em->flush();
		}

        return $this->render('backend_admin/societe/add_update.html.twig',['form'=>$form]);
    }


    #[Route('/activated/{id}/{active}', name: 'backend_admin_societe_activated')]
    public function activated(Societe $societe, $active)
    {
        $societe->setActive($active);
        $this->em->persist($societe);
        $this->em->flush();
        
        if($active){
            $this->addFlash('success', ['title'=>'Success', 'message'=>'Société a été bien déactiver']);
        }else{
            foreach($societe->getPartenaires() as $partenaire){
                $partenaire->setActive(false);
                $this->em->persist($partenaire);
            }
            $this->em->flush();
            $this->addFlash('success', ['title'=>'Success', 'message'=>'Société a été bien activer']);
        }

        
        return $this->redirectToRoute('backend_admin_societe_index');
    }
    
}