<?php

namespace App\Controller;

use App\Entity\Vendor;
use App\Form\VendorType;
use App\Repository\VendorRepository;
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\TextType;

/**
 * @Route("/vendor")
 */
class VendorController extends AbstractController
{   

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

        $vendor = $this->getDoctrine()
                         ->getRepository(Vendor::class)
                         ->getAllVendors($page, $filter);

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


    /**
     * @Route("/new", name="vendor_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $vendor = new Vendor();
        $form = $this->createForm(VendorType::class, $vendor);
        $form->handleRequest($request);

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

            $this->addFlash('notice', 'Vendor Created successfully!');

            return $this->redirectToRoute('vendor_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->renderForm('vendor/new.html.twig', [
            'vendor' => $vendor,
            'form' => $form,
        ]);
    }

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

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

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

            $this->addFlash('notice', 'Vendor Updated successfully!');

            return $this->redirectToRoute('vendor_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->renderForm('vendor/edit.html.twig', [
            'vendor' => $vendor,
            'form' => $form,
        ]);
    }

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

            $this->addFlash('notice', 'Vendor Deleted successfully!');
        }

        return $this->redirectToRoute('vendor_index', [], Response::HTTP_SEE_OTHER);
    }

     /**
     * @Route("/searchBar", name="vendor_searchBar")
     */
    public function SearchBar()
    {
        $form = $this->createFormBuilder(null)
                     ->add('search', TextType::class, array(
                            'attr' => [
                                'class' => 'search-input',
                                'placeholder' => 'Search by any value'
                            ],
                            'label'=>false,
                        ))
                     ->getForm();

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