<?php
namespace OfficeBrain\CustomBundle\CmsBundle\Controller;

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

/**
 * CreditApplicationForm controller.
 *
 * @Route("/{_locale}/creditapplicationform")
 */
class CreditApplicationFormController extends Controller
{
	private $translator;
	
	private $request;
	
	private $projectSetting;
	
	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;
		$this->commonLibraryManager = null;
	}
	
	public function __destruct()
	{
		unset($this->translator);
		unset($this->request);
		unset($this->projectSetting);
		unset($this->responseArray);
		unset($this->session);
		unset($this->creditApplicationFormManager);
		unset($this->commonLibraryManager);
	}
	
	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();
		$this->commonLibraryManager = $this->get('office_brain_common_library');
	}
	

    /**
	 * @author Employee Id: 4471
	 * Description : add tax master
	 * @Route("/credit-application-form-add", name="office_brain_cmsbundle_credit_application_form_add", options={"expose"=true})
	 * @method({"GET"})
	 * @Template("OfficeBrainCustomBundleCmsBundle:CreditApplicationForm:creditApplicationFormAdd.html.twig") 
	 */
	public function addCreditApplicationAction()
	{
		$this->initAction();
		$entity = new CreditApplicationForm();
		$this->form = $this->createForm(new CreditApplicationFormType(), $entity, array(
				'action' => $this->generateUrl('office_brain_cmsbundle_credit_application_form_save'),
				'method' => 'POST',
		));
		$this->responseArray['entity'] = $entity;
		$this->responseArray['credit_application_form'] = $this->form->createView();
		
		return $this->responseArray;
	}
	
	/**
	 * @author Employee Id: 4471
	 * Description : add tax master
	 * @Route("/credit-application-form-save", name="office_brain_cmsbundle_credit_application_form_save")
	 * @method({"POST"})
	 * @Template("OfficeBrainCustomBundleCmsBundle:CreditApplicationForm:creditApplicationFormAdd.html.twig")
	 */
	public function addCreditApplicationSaveAction()
	{
		$this->initAction();
		$entity = new CreditApplicationForm();
        $this->form = $this->createForm(new CreditApplicationFormType(), $entity, array(
				'action' => $this->generateUrl('office_brain_cmsbundle_credit_application_form_save'),
				'method' => 'POST',
		));
        
        $this->form->handleRequest($this->request);
        
        if ($this->form->isValid()) {
        	$formStatus = $this->creditApplicationFormManager->saveFormData($this->form, $entity);
        	$this->responseArray['status'] = 'success';
        	$this->responseArray['success_message'] = "Your credit application form submitted successfully.";
        	return new JsonResponse($this->responseArray);
//             return $this->redirect($this->generateUrl('office_brain_page_homepage_view'));
        }
        else {
        	return new JsonResponse($this->commonLibraryManager->prepareFormJsonError($this->form));
        }

//         return array(
//             'entity' => $entity,
//             'credit_application_form'   => $this->form->createView(),
//         );
	
	}
	
	/**
	 *
	 * @author Employee Id: 4074
	 *
	 * Desciption: This function allow user to download compare product page as pdf.
	 *
	 * @return application/pdf credit-application-form.pdf
	 *
	 * @throws NULL
	 *
	 * @Route("/credit-application-form-pdf",
	 * 	name="office_brain_cmsbundle_credit_application_form_pdf",
	 *  options={"expose"=true}
	 * )
	 *
	 * @Method({"POST"})
	 *
	 * @Template()
	 *
	 */
	public function downloadCreditApplicationFormAsPdfAction()
	{
		$this->initAction();
		$entity = new CreditApplicationForm();
		$this->form = $this->createForm(new CreditApplicationFormType(), $entity, array(
				'action' => $this->generateUrl('office_brain_cmsbundle_credit_application_form_save'),
				'method' => 'POST',
		));
		
		$this->form->handleRequest($this->request);
		
		$this->initAction();
		$data=$this->request;
		$this->responseArray['credit_application_form'] = $this->form->createView();
		
		$response = $this->render('OfficeBrainCustomBundleCmsBundle:CreditApplicationForm:creditApplicationFormPdf.html.twig',$this->responseArray);
		
		$content = $response->getContent();
		$baseURL = $this->container->getParameter('base_url');
		$basePath = $this->container->getParameter('base_path');
		$content = str_replace($baseURL,$basePath,$content);
		
		$this->get('knp_snappy.pdf')->getInternalGenerator()->setTimeout(300);
		return new Response(
				$this->get('knp_snappy.pdf')->getOutputFromHtml($content),
			200,
			array(
				'Content-Type'        => 'application/pdf',
				'Content-Disposition' => 'attachment; filename="credit-application-form.pdf"'
			)
		);
	}
    
    
  
}
