<?php

namespace App\Controller\BackendPartner;

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

use Symfony\Component\HttpFoundation\Request;

use Doctrine\ORM\EntityManagerInterface;

use App\Repository\ReservationArticleRepository;
use App\Repository\ReservationServiceRepository;

use App\Entity\ReservationArticleValidation;
use App\Entity\ReservationServiceValidation;

#[Route('/backend/partner/reservation/valider')]
class ReservationValiderController extends AbstractController
{

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

    /**
     * @var ReservationArticleRepository $reservationArticleRepository
     */
    private $reservationArticleRepository;

    /**
     * @var ReservationServiceRepository $reservationServiceRepository
     */
    private $reservationServiceRepository;

    /**
     * @param EntityManagerInterface $em
     * @param ReservationArticleRepository $reservationArticleRepository
     * @param ReservationServiceRepository $reservationServiceRepository
     * @return $this
     */
    public function __construct(
        EntityManagerInterface $em,
        ReservationArticleRepository $reservationArticleRepository,
        ReservationServiceRepository $reservationServiceRepository
    ){
        $this->em = $em;
        $this->reservationArticleRepository = $reservationArticleRepository;
        $this->reservationServiceRepository = $reservationServiceRepository;
    }
    
    #[Route('/valider', name: 'backend_partner_reservation_valider')]
    public function valider(Request $request): Response
    {
        $formArticle = $this->createFormBuilder()
                    ->add('reservnumarticle', \Symfony\Component\Form\Extension\Core\Type\TextType::class, ['required' => true])
                    ->add('save', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class)
                    ->getForm();
        

        $formService = $this->createFormBuilder()
                    ->add('reservnumservice', \Symfony\Component\Form\Extension\Core\Type\TextType::class, ['required' => true])
                    ->add('save', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class)
                    ->getForm();

        return $this->render('backend_partner/reservation/valider.html.twig', ['formArticle'=> $formArticle, 'formService'=>$formService]);
    }

    
    #[Route('/article', name: 'backend_partner_reservation_valider_article')]
    public function article(Request $request): Response
    {
        $data = $request->request->all();
        if(!isset($data['form']['reservnumarticle'])){
            $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Numéro Réservation Vide</b>"]);
            return $this->redirectToRoute('backend_partner_reservation_valider');
        }
        
        
        $numReservation = $data['form']['reservnumarticle'];
        
        if($numReservation){
            $partner = $this->getUser()->getSelectPartner();

        
            $reservationArticle = $this->reservationArticleRepository->findOneBy(['reference'=>$numReservation]);
            
            if($reservationArticle){
                $societe = $reservationArticle->getArticle()->getSociete();
                $valider = $reservationArticle->getReservationArticleValidation();
    
                if($partner->getSociete()->getId() != $societe->getId()){
                    $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Incorrect séléct Société (".$societe->getNom().")</b>"]);
                }else if($valider != null){
                    $dateAdd = $valider->getDateAdd();
                    $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Réservation article déja valider le ".$dateAdd->format('d/m/Y H:i')."</b>"]);
                }else{
                    $reservationArticleValidation = new ReservationArticleValidation();
                    $reservationArticleValidation->setReservationArticle($reservationArticle);
                    $reservationArticleValidation->setPartenaire($partner);
                    $this->em->persist($reservationArticleValidation);
                    $this->em->flush();
                    if($reservationArticleValidation->getId()){
                        $dateAdd = new \DateTime();
                        $reservationArticle->addEtat("valider", $dateAdd->format('Y-m-d H:i'));
                        $this->em->persist($reservationArticle);
                        $this->em->flush();
                        $this->addFlash('success', ['title'=>'Success', 'message'=>'Réservation article <b>valider</b>']);
                    }
                }
            }else{
                $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Numéro Réservation Incorrect</b>"]);
            }
        }else{
            $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Numéro Réservation Vide</b>"]);
        }
        
        return $this->redirectToRoute('backend_partner_reservation_valider');
    }



    #[Route('/service', name: 'backend_partner_reservation_valider_service')]
    public function service(Request $request): Response
    {
        $data = $request->request->all();
        if(!isset($data['form']['reservnumservice'])){
            $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Numéro Réservation Vide</b>"]);
            return $this->redirectToRoute('backend_partner_reservation_valider');
        }
        
        
        $numReservation = $data['form']['reservnumservice'];
       
        if($numReservation){
            $partner = $this->getUser()->getSelectPartner();

        
            $reservationService = $this->reservationServiceRepository->findOneBy(['reference'=>$numReservation]);
            
            if($reservationService){
                $societe = $reservationService->getService()->getSociete();
                $valider = $reservationService->getReservationServiceValidation();
    
                if($partner->getSociete()->getId() != $societe->getId()){
                    $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Incorrect séléct Société (".$societe->getNom().")</b>"]);
                }else if($valider != null){
                    $dateAdd = $valider->getDateAdd();
                    $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Réservation service déja valider le ".$dateAdd->format('d/m/Y H:i')."</b>"]);
                }else{
                    $reservationServiceValidation = new ReservationServiceValidation();
                    $reservationServiceValidation->setReservationService($reservationService);
                    $reservationServiceValidation->setPartenaire($partner);
                    $this->em->persist($reservationServiceValidation);
                    $this->em->flush();
                    if($reservationServiceValidation->getId()){
                        $dateAdd = new \DateTime();
                        $reservationService->addEtat("valider", $dateAdd->format('Y-m-d H:i'));
                        $this->em->persist($reservationService);
                        $this->em->flush();
                        $this->addFlash('success', ['title'=>'Success', 'message'=>'Réservation service <b>valider</b>']);
                    }
                }
            }else{
                $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Numéro Réservation Incorrect</b>"]);
            }
        }else{
            $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Numéro Réservation Vide</b>"]);
        }
        
        return $this->redirectToRoute('backend_partner_reservation_valider');
    }

}
