<?php

namespace App\Controller;

use App\Entity\ExpensePurpose;
use App\Form\ExpensePurposeType;
use App\Repository\ExpensePurposeRepository;
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\HttpFoundation\JsonResponse;

/**
 * @Route("/expensepurpose")
 */
class ExpensePurposeController extends AbstractController
{
    /**
     * @Route("/", name="expense_purpose_index", methods={"GET"})
     */
    public function index(ExpensePurposeRepository $expensePurposeRepository): Response
    {
        return $this->render('expense_purpose/index.html.twig', [
            'expense_purposes' => $expensePurposeRepository->findAll(),
        ]);
    }

    /**
     * @Route("/new", name="expense_purpose_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $expensePurpose = new ExpensePurpose();
        $form = $this->createForm(ExpensePurposeType::class, $expensePurpose);
        $form->handleRequest($request);

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

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

        return $this->render('expense_purpose/new.html.twig', [
            'expense_purpose' => $expensePurpose,
            'form' => $form->createView(),
        ]);
    }

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

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

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

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

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

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

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


    /**
     * @Route("/getexpensepurposebytype", name="app_expense_purpose_getexpensepurposebytype")
     */
    public function getExpensePurposebyTypeId(Request $request, ExpensePurposeRepository $expensePurposeRepository)
    {       
        $expensePurposes = $expensePurposeRepository->findBy(
                                    ['expenseType' => $request->query->get("expenseTypeId")],
                                    ['name' => 'ASC']
                                ); 

        // Serialize into an array the data that we need, in this case only name and id
        // Note: you can use a serializer as well, for explanation purposes, we'll do it manually
        $responseArray = array();
        foreach($expensePurposes as $expensePurpose){
            $responseArray[] = array(
                "id" => $expensePurpose->getId(),
                "name" => $expensePurpose->getName()
            );
        }
        
        // Return array with structure of the neighborhoods of the providen city id
        return new JsonResponse($responseArray);
       
    }

}
