<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\JsonResponse;
use OfficeBrain\CustomBundle\CmsBundle\Entity\CreditApplicationForm;
use OfficeBrain\CustomBundle\CmsBundle\Form\CreditApplicationFormType;
use Symfony\Component\Validator\Constraints\DateTime;

/**
 * CreditApplicationForm controller.
 *
 * @Route("/admin/{_locale}/creditapplicationform")
 */
class CreditApplicationFormAdminController extends Controller
{
	private $translator;
	
	private $request;
	
	private $projectSetting;
	
	private $recordsPerPage;
	
	private $responseArray;
	
	private $session;
	
	private $creditApplicationFormManager;
	
	public function __construct()
	{
		$this->translator = null;
		$this->request = null;
		$this->projectSetting = array();
		$this->responseArray = array();
		$this->session = null;
		$this->creditApplicationFormManager = null;
	}
	
	public function __destruct()
	{
		unset($this->translator);
		unset($this->request);
		unset($this->projectSetting);
		unset($this->responseArray);
		unset($this->session);
		unset($this->creditApplicationFormManager);
	}
	
	public function initAction()
	{
		$this->translator = $this->get('translator');
		$this->request = $this->getRequest();
		$this->projectSetting = $this->request->get('project_setting');
		$this->responseArray = array();
		$this->session = $this->get('session');
		$this->creditApplicationFormManager = $this->get('office_brain_cms_credit_application_form_manager');
		$this->request = $this->getRequest();
	}
	

	/**
	 * @author Employee Id: 5497
	 * Description : Get the list of Credit Applications
	 *
	 * @Route("/list/{instance_id}/{page}", name="creditapplicationform",
	 * options={"expose"=true},
	 * requirements={"instance_id"="\d+", "page"="\d+"},
	 * defaults={"instance_id"="0", "page"=1})
	 *
	 * @Method("GET")
	 * @Template()
	 *
	 * @return array
	 */
    public function indexAction()
    {
        $this->initAction();
        $this->responseArray = $this->creditApplicationFormManager->getCreditApplicationList();
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 5497
     * Description : Get the list of Credit Applications
     *
     * @Route("/list-ajax/{page}", name="creditapplicationformajax",
     * options={"expose"=true},
     * requirements={ "page"="\d+"},
     * defaults={ "page"=1})
     *
     * @Method("POST")
     * @Template()
     *
     * @return array
     */
    public function promoCouponListAjaxAction()
    {
    	$this->initAction();
    	$this->responseArray = $this->creditApplicationFormManager->getCreditApplicationList();
    
    	return $this->responseArray;
    }
    
    /**
     * Creates a new CreditApplicationForm entity.
     *
     * @Route("/", name="creditapplicationform_create")
     * @Method("POST")
     * @Template("OfficeBrainCustomBundleCmsBundle:CreditApplicationForm:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new CreditApplicationForm();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

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

            return $this->redirect($this->generateUrl('creditapplicationform_show', array('id' => $entity->getId())));
        }

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

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

        return $form;
    }

    /**
     * Displays a form to create a new CreditApplicationForm entity.
     *
     * @Route("/new",
	 * 	name="office_brain_cmsbundle_new",
	 *  options={"expose"=true}
	 * )
     * @Method({"POST,GET"})
     * @Template()
     */
    public function newAction()
    {
        $entity = new CreditApplicationForm();
        $form   = $this->createCreateForm($entity);

        
        $this->request = $this->getRequest();
        if($this->request->getMethod() == 'POST'){
        
        	$form   = $this->createCreateForm($entity);
        	
        	$form->handleRequest($this->request);
	        if ($form->isValid()) {
	        	$em = $this->getDoctrine()->getManager();
	        	$em->persist($entity);
	        	$em->flush();
	        
	        	return $this->redirect($this->generateUrl('creditapplicationform_show', array('id' => $entity->getId())));
	        }
        }
        
        return array(
            'entity' => $entity,
            'credit_application_form'   => $form->createView(),
        );
    }

    /**
     * Finds and displays a CreditApplicationForm entity.
     *
     * @Route("/{id}", name="creditapplicationform_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('OfficeBrainCustomBundleCmsBundle:CreditApplicationForm')->find($id);

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

        $deleteForm = $this->createDeleteForm($id);

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

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

        $entity = $em->getRepository('OfficeBrainCustomBundleCmsBundle:CreditApplicationForm')->find($id);

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

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

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

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

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

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

        $entity = $em->getRepository('OfficeBrainCustomBundleCmsBundle:CreditApplicationForm')->find($id);

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

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

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

            return $this->redirect($this->generateUrl('creditapplicationform_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }
    /**
     * Deletes a CreditApplicationForm entity.
     *
     * @Route("/{id}", name="creditapplicationform_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('OfficeBrainCustomBundleCmsBundle:CreditApplicationForm')->find($id);

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

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

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

    /**
     * Creates a form to delete a CreditApplicationForm 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('creditapplicationform_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}
