<?php
namespace App\Controller;
use App\Interfaces\CountriesInterface;
use App\Interfaces\IHotels;
use App\Interfaces\RegionInterface;
use App\Interfaces\StaticDataInterface;
use App\Vendor\Helper;
use App\Vendor\Symfony\MAbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/detail/{_locale}', name: 'app_detail_')]
class DetailController extends MAbstractController
{
#[Route('/{id}', name: 'hotel_detail', methods: ['GET','POST'])]
public function hotelDetail(string $id, Request $request, IHotels $hotelService, RegionInterface $regionService, TranslatorInterface $translator, StaticDataInterface $staticDataService, CountriesInterface $countries): Response
{
Helper::clearSession($request);
$params = $this->getParams();
if(isset($params['newParams'])){
$params = $params["newParams"];
$request->query->set('search', $params[0]["search"]);
$request->query->remove("newParams");
}
unset($params["path"]);
$regionData = [];
$country = $countries->getCurrentCountry();
if(isset($params[0]) && isset($params[0]["search"])){
$params["search"] = $params[0]["search"];
unset($params[0]);
if(empty($params["search"]["guests"][0]["children"])){
$params["search"]["guests"][0]["children"][0] = "NaN";
}
if(count($params["search"]["guests"]) > 1){
foreach ($params["search"]["guests"] as &$guest){
if(!isset($guest["children"])){
$guest["children"] = ["NaN"];
}
}
}
if(empty($params["search"]["checkin"]) || empty($params["search"]["checkout"])){
$currentDay = date("Y-m-d");
$params["search"]["checkin"] = date("Y-m-d", strtotime($currentDay . ' +1 day'));
$params["search"]["checkout"] = date("Y-m-d", strtotime($currentDay . ' +2 day'));
}
if(is_numeric($params["search"]["location"])){
$region = $regionService->getRegionById((int)$params["search"]["location"]);
if(!empty($region)){
$regionData["id"] = $region[0]["id"];
$regionData["name"] = $region[0]["name"][$request->getLocale()];
}
} else {
$region = $hotelService->getByHotelId($params["search"]["location"], $request->getLocale());
if(!empty($region)){
$regionData["id"] = $region[0]["id"];
$regionData["name"] = $region[0]["name"];
}
}
$checkIn = empty($params["search"]["checkin"]) ? date("Y-m-d") : $params["search"]["checkin"];
$params["search"]["checkin"] =$checkIn;
$params["search"]["checkout"] = empty($params["search"]["checkout"]) ? date('Y-m-d', strtotime($checkIn . ' +1 day')) : $params["search"]["checkout"];
$hotelService->guestsRoomsDaysCount($params);
if(!empty($params["search"]["guests"])){
foreach ($params["search"]["guests"] as &$guest){
$children = ["NaN"];
if(!empty($guest["children"]) && $guest["children"][0] != "NaN"){
$children = explode(',', $guest["children"][0]);
foreach ($children as &$child){
$child = intval($child);
}
}
$guest["children"] = $children;
}
}
if(!empty($params["search"]["residency"])){
$country = $countries->getCountryByCode($params["search"]["residency"], $request->getLocale());
}
}
$hotel = $hotelService->hotelPage($id, $request);
$metapolicyIncludedData = [];
$metapolicyNoIncludedData = [];
if(!empty($hotel)){
foreach ($hotel["metapolicy_struct"] as $key=>$mStruct){
$tkey = $key;
foreach ($mStruct as $item){
if(!empty($item["inclusion"])){
if($item["inclusion"] == "included"){
$metapolicyIncludedData[$tkey][] = $item;
}else{
if($key == "meal" || $key == "children_meal" ){
$item["meal_type_t"] = $staticDataService->getMeal($item["meal_type"], $request->getLocale());
}
$metapolicyNoIncludedData[$tkey][] = $item;
}
}
if(!empty($item["availability"]) && $item["availability"] == "available"){
$metapolicyNoIncludedData[$tkey][] = $item;
}
if(!isset($item["inclusion"]) && !isset($item["availability"]) ){
if($key == "no_show"){
continue;
}
$metapolicyNoIncludedData[$tkey][] = $item;
}
}
if(!isset($item["inclusion"]) && !isset($item["availability"]) ){
if($key == "no_show"){
$metapolicyNoIncludedData["no_show"][] = $translator->trans('no_show', ['{time}' => $mStruct["time"]]);
}
}
}
$hotel["metapolicyIncludedData"] = $metapolicyIncludedData;
$hotel["metapolicyNoIncludedData"] = $metapolicyNoIncludedData;
}
if($request->getMethod() == "POST"){
return new JsonResponse($hotel);
}
$session = $request->getSession();
if($session->has('hotel')){
$session->remove('hotel');
}
$session->set('hotel', serialize($hotel));
if(empty($this->getUser())){
$params["id"] = $id;
$session->set('back_url', $this->generateUrl($request->get("_route"), $params));
}
return $this->render('detail/hotel_detail.html.twig', [
'controller_name' => 'DetailController',
'hotel' => $hotel,
'params' => $params,
'region' => $regionData,
'country' => $country
]);
}
#[Route('/room', name: 'room_detail')]
public function roomDetail(): Response
{
return $this->render('detail/room_detail.html.twig', [
'controller_name' => 'DetailController',
]);
}
}