<?php

namespace App\Controller\BackendPartner;

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

use Doctrine\ORM\EntityManagerInterface;

use App\Entity\Partenaire;

use App\Repository\PartenaireRepository;


#[Route('/backend/partner/partenaire')]
class PartenaireController extends AbstractController
{

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

    /**
     * @var PartenaireRepository $partenaireRepository
     */
    private $partenaireRepository;

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

    #[Route('/selected', name: 'backend_partner_partenaire_selected')]
    public function update_selected(Request $request): Response
    {
        $idPartner = $request->get('id_partner');
        
        $partenaires = $this->getUser()->getPartenairesActive();
       
        foreach($partenaires as $partenaire){
            if($idPartner == $partenaire->getId()){
                $partenaire->setSelected(true);
            }else{
                $partenaire->setSelected(false);
            }
            $this->em->persist($partenaire);

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

        $referer = $request->headers->get('referer');
        
        return $this->redirect($referer);
    }
    

    #[Route('/gestion', name: 'backend_partner_partenaire_gestion')]
    public function gestion(Request $request): Response
    {
        $societe = $this->getUser()->getSelectPartner()->getSociete();
        //$partenaires =  $this->partenaireRepository->findBy();

        return $this->render('backend_partner/partenaire/gestion.html.twig');
    }
    
    
    #[Route('/add', name: 'backend_partner_partenaire_add')]
    public function add(Request $request): Response
    {

    }


    


    
}