<?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\Entity\Service;
use App\Entity\ServiceRdv;
use App\Entity\ServiceRdvReservation;

use App\Repository\ServiceRDVRepository;

use App\Service\BackendAdmin\ServiceService;

#[Route('/backend/admin/service/rdv')]
class ServiceRDVController extends AbstractController
{

    #[Route('/add/{id}', name: 'backend_admin_service_rdv_add')]
    public function add(Service $service, Request $request, ServiceService $serviceService): Response
    {
        if ($request->isMethod('POST')) {
            
            $data = $request->request->all();
            
            if(isset($data['Sun'])){$excludeDay[] = 'Sun'; }
            if(isset($data['Mon'])){$excludeDay[] = 'Mon'; }
            if(isset($data['Tue'])){$excludeDay[] = 'Tue'; }
            if(isset($data['Wed'])){$excludeDay[] = 'Wed'; }
            if(isset($data['Thu'])){$excludeDay[] = 'Thu'; }
            if(isset($data['Fri'])){$excludeDay[] = 'Fri'; }
            if(isset($data['Sat'])){$excludeDay[] = 'Sat'; }
            
            $Param['id'] =  $service->getId();
            $Param['startDate'] =  $data['date-debut'];
            $Param['endDate'] =  $data['date-fin'];
            $Param['startTime'] =  $data['time-debut'];
            $Param['endTime'] =  $data['time-fin'];
            $Param['between'] =  $data['duree-entre'];
            $Param ['duree'] = $service->getDuree(); 
            $Param['excludeDay'] = $excludeDay;
            $Param['save'] = 'true';

            $ranges = $serviceService->getListRendezVous(new \DateTime($Param['startDate']), new \DateTime($Param['endDate']), $Param['startTime'], $Param['endTime'], $Param ['duree'], $Param['between'], $excludeDay);
            $RendezVous = [];
            foreach($ranges as $range){
                
                $RendezVous[$range['debut']->format('d/m/Y')][] = ['debut'=>$range['debut']->format('H:i'), 'fin'=>$range['fin']->format('H:i')];
            }
            $Param['arrRendezVous'] = $RendezVous;

            return $this->render('backend_admin/service_rdv/show_new_rdv.html.twig', $Param);
        }
        return $this->render('backend_admin/service_rdv/add.html.twig', ["service"=>$service]);
    }


    #[Route('/show/{id}', name: 'backend_admin_service_rdv_show')]
    public function show(ServiceRdv $serviceRdv): Response
    {
        $Param =  $serviceRdv->toArray();
        $Param['save'] = 'false';

        return $this->render('backend_admin/service_rdv/show_new_rdv.html.twig', $Param);
    }


    #[Route('/rdv_ajax_show', name: 'backend_admin_service_rdv_ajax_show')]
    public function rdv_ajax_show(Request $request, ServiceRDVRepository $serviceRdvRepository)
    {
        $idRdv = $request->get('idRdv');
        $serviceRdvs = $serviceRdvRepository->findBy(['service'=>$idRdv],["id"=>'DESC']);
        $result = "";
        foreach($serviceRdvs as $serviceRdv){
            $result .= "<tr>";
            $result .= "<td>".$serviceRdv->getId()."</td>";
            $result .= "<td>".$serviceRdv->getStringStartDate()."</td>";
            $result .= "<td>".$serviceRdv->getStringEndDate()."</td>";
            $result .= "<td>".$serviceRdv->getStringStartTime()."</td>";
            $result .= "<td>".$serviceRdv->getStringEndTime()."</td>";
            $result .= "<td>".$serviceRdv->getBetweenRdv()."</td>";
            $result .= "<td>".$serviceRdv->getExcludeDay()."</td>";
            $result .= '<td><a href="rdv/show/'.$serviceRdv->getId().'" class="btn btn-info btn-action"><i class="fa-solid fa-magnifying-glass"></i></a></td>';
            $result .= "</tr>";
        }
        return new Response($result);
    }


    #[Route('/save/{id}', name: 'backend_admin_service_rdv_save')]
    public function save(Service $service, 
                        Request $request, 
                        ServiceService $serviceService,
                        EntityManagerInterface $em): Response
    {
        $Param['startDate'] = $request->get('startDate');
        $Param['endDate'] = $request->get('endDate'); 
        $Param['startTime'] = $request->get('startTime'); 
        $Param['endTime'] = $request->get('endTime'); 
        $Param['duree'] = $service->getDuree(); 
        $Param['between'] = $request->get('between');
        $Param['excludeDay'] = $excludeDay =  $request->get('excludeDay');
        $Param['id'] = $service->getId();
        $Param['save'] = 'false';

        $ranges = $serviceService->getListRendezVous(new \DateTime($Param['startDate']), new \DateTime($Param['endDate']), $Param['startTime'], $Param['endTime'], $Param ['duree'], $Param['between'], $excludeDay);
        
        $serviceRdv = new ServiceRdv();
        $serviceRdv->setStartDate(new \DateTime($Param['startDate']));
        $serviceRdv->setEndDate(new \DateTime($Param['endDate']));
        $serviceRdv->setDuree($Param['duree']);
        $serviceRdv->setStartTime(new \DateTime($Param['startTime']));
        $serviceRdv->setEndTime(new \DateTime($Param['endTime']));
        $serviceRdv->setBetweenRdv($Param['between']);
        $serviceRdv->setExcludeDay(implode(",", $Param['excludeDay']));
        $serviceRdv->setService($service);
        $serviceRdv->setReservationJson($ranges);
        $serviceRdv->setCreateBy($this->getUser());
        $em->persist($serviceRdv);
        $em->flush();

        $RendezVous = [];
        foreach($ranges as $reservation){
            $serviceRdvReservation = new ServiceRdvReservation();
            $serviceRdvReservation->setDateDebut($reservation['debut']);
            $serviceRdvReservation->setDateFin($reservation['fin']);
            $serviceRdvReservation->setServiceRdv($serviceRdv);
            $serviceRdvReservation->setService($service);
            $em->persist($serviceRdvReservation);
            $RendezVous[$reservation['debut']->format('d/m/Y')][] = ['debut'=>$reservation['debut']->format('H:i'), 'fin'=>$reservation['fin']->format('H:i')];
        }
        $Param['arrRendezVous'] = $RendezVous;

        $em->flush();
       
        return $this->render('backend_admin/service_rdv/show_new_rdv.html.twig', $Param);
    }
    

    
}

