<?php

namespace App\Controller;

use App\Entity\TourClaim;
use App\Entity\TourClaimDetails;
use App\Entity\TourRequest;
use App\Form\TourClaimType;
use App\Form\TourClaimDetailsType;
use App\Repository\TourClaimRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * @Route("/tourclaim")
 */
class TourClaimController extends AbstractController
{
    /**
     * @Route("/{page<[1-9]\d*>}", defaults={"page"=1}, name="tour_claim_index")   
     */
    public function index(Request $request, int $page): Response
    {
        $data = $request->request->get('form');
        if(isset($data['search']))
        {
            $filter = $data['search'];
        }
        else
        {
            $filter = '';
        }

        $tour_claims = $this->getDoctrine()
                         ->getRepository(TourClaim::class)
                         ->getAllTourClaim($page, $filter);

        return $this->render('tour_claim/index.html.twig', [
            'tour_claims' => $tour_claims,
        ]);
    }

    /**
     * @Route("/{id}/new", name="tour_claim_new", methods={"GET","POST"})
     */
    public function new(Request $request, TourRequest $tourRequest): Response
    {
        $tourClaim = new TourClaim();
        $form = $this->createForm(TourClaimType::class, $tourClaim)
                     ->add('createNew', SubmitType::class);
        $form->handleRequest($request);

        $tourClaimDetails = new TourClaimDetails();
        $formDet = $this->createForm(TourClaimDetailsType::class, $tourClaimDetails);
        $formDet->handleRequest($request);  

        if ($request->isXMLHttpRequest()) {
            $claimObj = $request->request->get('claimObj');            
            if (!empty($claimObj)) {

                $claimObj_array = json_decode($claimObj, true);
                $category_array = json_decode($request->request->get('category'));
                $fromPlace_array = json_decode($request->request->get('fromPlace'));
                $startingDate_array = json_decode($request->request->get('startingDate'));
                $toPlace_array = json_decode($request->request->get('toPlace'));
                $endingDate_array = json_decode($request->request->get('endingDate'));
                $travelMode_array = json_decode($request->request->get('travelMode'));
                $amountCurrency_array = json_decode($request->request->get('amountCurrency'));
                $amount_array = json_decode($request->request->get('amount'));
                $remarks_array = json_decode($request->request->get('remarks'));

                $tourClaim->setTourRequest($tourRequest);               
                $tourClaim->setSubmitionDate(new \DateTime("now", new \DateTimeZone("Asia/Dhaka")));
                $tourClaim->setStartingDate(new \DateTime($claimObj_array['StartingDate']));                
                $tourClaim->setEndingDate(new \DateTime($claimObj_array['EndingDate']));
                $tourClaim->setTourDetails($claimObj_array['TourDetails']);  
                                          
                $index = 0;
                foreach($category_array as $row_array) 
                {
                    $tourClaimDetails = new TourClaimDetails();                                        
                    $tourClaimDetails->setCategory($category_array[$index]);
                    $tourClaimDetails->setFromPlace($fromPlace_array[$index]);
                    $tourClaimDetails->setStartingDate(new \DateTime($startingDate_array[$index]));
                    $tourClaimDetails->setToPlace($toPlace_array[$index]); 
                    $tourClaimDetails->setEndingDate(new \DateTime($endingDate_array[$index]));
                    $tourClaimDetails->setTravelMode($travelMode_array[$index]);
                    $tourClaimDetails->setAmountCurrency($amountCurrency_array[$index]);
                    $tourClaimDetails->setAmount($amount_array[$index]);
                    $tourClaimDetails->setRemarks($remarks_array[$index]); 
                  
                    $tourClaim->addTourClaimDetail($tourClaimDetails);

                    $index++;
                }
               
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->persist($tourClaim);
                $entityManager->flush();
             
                $this->addFlash('notice', 'Tour Claim Created successfully!');

                // if ($form->get('createNew')->isClicked()) {
                //     return new JsonResponse(array('redirectTo' => 2));
                // }
        
                return new JsonResponse(array('success' => true));                
            }
        }

        // if ($form->isSubmitted() && $form->isValid()) {
        //     $entityManager = $this->getDoctrine()->getManager();
        //     $entityManager->persist($tourClaim);
        //     $entityManager->flush();

        //     return $this->redirectToRoute('tour_claim_index');
        // }

        return $this->render('tour_claim/new.html.twig', [
            'tour_claim' => $tourClaim,
            'form' => $form->createView(),
            'formDet' => $formDet->createView(),
            'tourRequest' => $tourRequest,
        ]);
    }

    /**
     * @Route("/{id}/show", name="tour_claim_show", methods={"GET"})
     */
    public function show(TourClaim $tourClaim): Response
    {
        return $this->render('tour_claim/show.html.twig', [
            'tour_claim' => $tourClaim,
        ]);
    }

    /**
     * @Route("/{id}/edit", name="tour_claim_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, TourClaim $tourClaim): Response
    {
        $form = $this->createForm(TourClaimType::class, $tourClaim);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

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

        return $this->render('tour_claim/edit.html.twig', [
            'tour_claim' => $tourClaim,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}/delete", name="tour_claim_delete", methods={"DELETE"})
     */
    public function delete(Request $request, TourClaim $tourClaim): Response
    {
        if ($this->isCsrfTokenValid('delete'.$tourClaim->getId(), $request->request->get('_token'))) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($tourClaim);
            $entityManager->flush();
        }

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

}
