<?php

namespace Anis\CommerceBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Anis\CommerceBundle\Entity\Client;
use Anis\CommerceBundle\Form\ClientType;


/**
 * Client controller.
 *
 * @Route("/client")
 * @Template()
 */
class ClientController extends Controller
{

    /**
     * Lists all Client entities.
     *
     * @Route("/{page}", name="client1", requirements={"page" = "\d+"})
     * @Route("/", name="client")
     * @Method("GET")
     *
     */
    public function indexAction($page = 1)
    {


        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('AnisCommerceBundle:Client')->findAllLimited($page);


        $query = $em->createQuery('SELECT COUNT(u.id) FROM AnisCommerceBundle:Client u');
        $count = $query->getSingleScalarResult();


        /** Search by criteria for a client */
//        $em = $this->getDoctrine()->getManager();
//        $query = $em->getRepository('AnisCommerceBundle:Client')->getListBy(array('firstname' => 'Khalil'));
//        var_dump($query);die();
        /** End Search */

//        $cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter();
//        $exporter = $cme->getExporter('xml', 'c:/temp/exp2.xml');
//        $classes = array(
//            $em->getClassMetadata('Anis\CommerceBundle\Entity\Client')
//        );
//        $exporter->setMetadata($classes);
//        $exporter->setOverwriteExistingFiles(true);
//        $exporter->export();


       /* $html = $this->renderView('AnisCommerceBundle:Client:index.html.twig',
            array('entities' => $entities,
                'page' => $page,
                'pages' => ($count - ($count % 10) ) / 10 + 1
            ));

        return new Response(
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
            200,
            array(
                'Content-Type'          => 'application/pdf'

            ));*/

        return array(
            'entities' => $entities,
            'page' => $page,
            'pages' => (($count - 1)  - (($count - 1) % 10) ) / 10 + 1
        );
    }

    /**
     * Search for clients by criteria.
     *
     * @Route("/search", name="client_search")
     * @Method("POST")
     */
    public function searchAction(Request $request)
    {

        //die(var_dump($request->request));
        $em = $this->getDoctrine()->getManager();

        $query = $em->getRepository('AnisCommerceBundle:Client')->getListBy($request->request->all());
        $clients = array();
        foreach($query as $client){

            $clientArray = $client->toArray();
//            $clientArray["showURL"] = $this->generateUrl("client_show", array('id' => $client->getId()));
            $clientArray["editURL"] = $this->generateUrl("client_edit", array('id' => $client->getId()));
            $clientArray["deleteURL"] = $this->generateUrl("client_delete", array('id' => $client->getId()));
            $clients[] = $clientArray;
        }


        //die(var_dump($query));
        $response = new Response(json_encode($clients));

        $response->headers->set('Content-Type', 'application/json');
        return $response;
    }
    /**
     * Creates a new Client entity.
     *
     * @Route("/", name="client_create")
     * @Method("POST")
     * @Template("AnisCommerceBundle:Client:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new Client();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('client'));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
    * Creates a form to create a Client entity.
    *
    * @param Client $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createCreateForm(Client $entity)
    {
        $form = $this->createForm(new ClientType(), $entity, array(
            'action' => $this->generateUrl('client_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new Client entity.
     *
     * @Route("/new", name="client_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Client();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }


    /**
     * Displays a form to edit an existing Client entity.
     *
     * @Route("/{id}/edit", name="client_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AnisCommerceBundle:Client')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Client entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'form'   => $editForm->createView(),
        );
    }

    /**
    * Creates a form to edit a Client entity.
    *
    * @param Client $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Client $entity)
    {
        $form = $this->createForm(new ClientType(), $entity, array(
            'action' => $this->generateUrl('client_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing Client entity.
     *
     * @Route("/{id}", name="client_update")
     * @Method("PUT")
     * @Template("AnisCommerceBundle:Client:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AnisCommerceBundle:Client')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Client entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('client'));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }
    /**
     * Deletes a Client entity.
     *
     * @Route("/{id}", name="client_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

//        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('AnisCommerceBundle:Client')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Client entity.');
            }

            $em->remove($entity);
            $em->flush();
//        }

        return $this->redirect($this->generateUrl('client'));
    }

    /**
     * Creates a form to delete a Client entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('client_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}
