<?php

namespace OfficeBrain\Bundle\CoreBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Response;
use OfficeBrain\Bundle\CoreBundle\Form\CurrencyConversionType as CurrencyConversionType;
use OfficeBrain\Bundle\CoreBundle\Manager\Service\CurrencyConversionSevice  as CurrencyConversionManager;
use OfficeBrain\Bundle\CoreBundle\Entity\Contry;




Class CurrencyConversionAdminController extends Controller
{
	public function __construct()
	{
		$this->request = null;
		//$this->currencyCode = array('USD'=>'USD','CAD'=>'CAD');
	}
	
	public function __destruct()
	{
		unset($this->request);
		//unset($this->currencyCode);
	}
	
	public function initAction()
	{
		$this->request = $this->getRequest();
		$this->commonLibraryManager = $this->get('office_brain_common_library');
		$this->currencyConversionManager = $this->get('office_brain_currency_convesion_manager');
		$this->currencyCode=$this->currencyConversionManager->getCurrencyCodeList();
	}
	
    /**
     * @author Employee Id: 4471
     * Description : get listing of different currency conversion
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/admin/{_locale}/currency-conversion-list",
     * name="office_brain_corebundle_currencyconversionadmin_currencyconversionlist",
     * )
     * @Template("OfficeBrainCoreBundle:Admin:listCurrencyConversionAdmin.html.twig")
     * @Method({"GET"})
     */
	
	public function listCurrencyConversionAdminAction()
	{
		$this->initAction (); 
		$response=array();
		$listCC = $this->currencyConversionManager->getCurrencyConversionList();
		return array('data'=>$listCC);
	}
	
	/**
     * @author Employee Id: 4471
     * Description : add currency conversion
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/admin/{_locale}/currency-conversion-add",
     * name="office_brain_corebundle_currencyconversionadmin_currencyconversionadd",
     * options={"expose"=true}
     * )
     * @Template("OfficeBrainCoreBundle:Admin:addCurrencyConversionAdmin.html.twig")
     * @Method({"GET"})
     */
	
	public function addCurrencyConversionAdminAction()
	{
		$this->initAction (); 
		$this->form = $this->createForm(new CurrencyConversionType($this->currencyCode,'add'));
        $this->responseArray['form'] = $this->form->createView();
        return $this->responseArray;
	}
	
	
	/**
     * @author Employee Id: 4471
     * Description : add currency conversion
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/admin/{_locale}/currency-conversion-add-submit",
     * name="office_brain_corebundle_currencyconversionadmin_currencyconversionadd_submit",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"POST"})
     */
	
	public function submitAddCurrencyConversionAdminAction()
	{
		$this->initAction();
        $this->form = $this->createForm(new CurrencyConversionType($this->currencyCode,'add'));
        $this->form->handleRequest($this->request);
    	
    	if ($this->form->isValid()) {
    		$data['toCurrency'] = $this->form['to_currency']->getData();
			$data['fromCurrency'] = $this->form['from_currency']->getData();
			$data['conversionRate'] = $this->form['conversion_rate']->getData();
			$data['enable'] = $this->form['enable']->getData();
			$response = $this->currencyConversionManager ->addCurrencyConversion($data);
			if($response == 'Sucess')
			{
				$this->responseArray['status'] = 'success';
	    		$this->responseArray['close_modal'] = true;
				$this->responseArray['page_url'] = $this->generateUrl('office_brain_corebundle_currencyconversionadmin_currencyconversionlist');
	    		$this->responseArray['success_message'] = $this->get('translator')->trans('currency_conversion.add_success');
			}
			else
			{
				$this->responseArray['status'] = 'Error';
	    		$this->responseArray['close_modal'] = true;
				$this->responseArray['page_url'] = $this->generateUrl('office_brain_corebundle_currencyconversionadmin_currencyconversionlist');
	    		$this->responseArray['error_message'] = $this->get('translator')->trans('currency_conversion.add_error');
			}
			
    	} else {
   			$this->responseArray = $this->commonLibraryManager->prepareFormJsonError($this->form);
    	}

    	return new JsonResponse($this->responseArray);
	}


	/**
     * @author Employee Id: 4471
     * Description : edit currency conversion by id
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/admin/{_locale}/currency-conversion-edit/{ccId}",
     * name="office_brain_corebundle_currencyconversionadmin_currencyconversionedit",
     * options={"expose"=true}
     * )
     * @Template("OfficeBrainCoreBundle:Admin:editCurrencyConversionAdmin.html.twig")
     * @Method({"GET"})
     */
	
	public function editCurrencyConversionAdminAction()
	{
		$this->initAction (); 
		$ccId = $this->request->get('ccId');
		$idCCDetail = $this->currencyConversionManager->getCurrencyConversionDetailById($ccId);
		$this->form = $this->createForm(new CurrencyConversionType($this->currencyCode,'edit',$idCCDetail),$idCCDetail);
        $this->responseArray['form'] = $this->form->createView();
		$this->responseArray['ccId'] = $ccId;
		$this->responseArray['data'] = $idCCDetail;
        return $this->responseArray;
	}
	
	
	/**
     * @author Employee Id: 4471
     * Description : edit currency conversion
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/admin/{_locale}/currency-conversion-edit-submit",
     * name="office_brain_corebundle_currencyconversionadmin_currencyconversionedit_submit",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"POST"})
     */
	
	public function submitEditCurrencyConversionAdminAction()
	{
		$this->initAction();
		
		if ($this->request->getMethod() == 'POST'){
			
		$id=$this->request->get('ccId');
		$idCCDetail = $this->currencyConversionManager->getCurrencyConversionDetailById($id);
        $this->form = $this->createForm(new CurrencyConversionType($this->currencyCode,'edit',$idCCDetail),$idCCDetail);
        $this->form->handleRequest($this->request);
    	if ($this->form->isValid()) {
			$data['conversionRate'] = $this->form['conversion_rate']->getData();
			$data['enable'] = $this->form['enable']->getData();
			$data['id'] = $this->request->get('ccId');
			$response = $this->currencyConversionManager ->editCurrencyConversion($data);
			if($response == 'Sucess')
			{
				$this->responseArray['status'] = 'success';
	    		$this->responseArray['close_modal'] = true;
	    		$this->responseArray['page_url'] = $this->generateUrl('office_brain_corebundle_currencyconversionadmin_currencyconversionlist');
	    		$this->responseArray['success_message'] = $this->get('translator')->trans('currency_conversion.edit_success');
			}
			else
			{
				$this->responseArray['status'] = 'Error';
	    		$this->responseArray['close_modal'] = true;
	    		$this->responseArray['page_url'] = $this->generateUrl('office_brain_corebundle_currencyconversionadmin_currencyconversionlist');
	    		$this->responseArray['error_message'] = $this->get('translator')->trans('currency_conversion.edit_error');
			}
			
    	} else {
   			$this->responseArray = $this->commonLibraryManager->prepareFormJsonError($this->form);
    	}
    	}

    	return new JsonResponse($this->responseArray);
	}
	
	/**
     * @author Employee Id: 4471
     * Description : delete currency conversion by id
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/admin/{_locale}/currency-conversion-delete/{ccId}",
     * name="office_brain_corebundle_currencyconversionadmin_currencyconversiondelete",
     * )
     * @Template()
     * @Method({"GET"})
     */
	
	public function deleteCurrencyConversionAdminAction()
	{
		$this->initAction (); 
		$ccId = $this->request->get('ccId');
		$deleteCC = $this->currencyConversionManager->deleteCurrencyConversionList($ccId);
		if($deleteCC == '1')
		{
			$this->responseArray['status'] = 'success';
    		$this->responseArray['close_modal'] = true;
    		$this->responseArray['success_message'] = $this->get('translator')->trans('currency_conversion.delete_success');
		}
		else
		{
			$this->responseArray['status'] = 'Error';
    		$this->responseArray['close_modal'] = true;
    		$this->responseArray['error_message'] = $this->get('translator')->trans('currency_conversion.delete_error');
		}
		return new JsonResponse($this->responseArray);
	}
	
	/**
     * @author Employee Id: 4471
     * Description : get currency conversion rate
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/admin/{_locale}/currency-conversion-rate",
     * name="office_brain_corebundle_currencyconversionadmin_currencyconversionrate",
	 * options={"expose"=true}
     * )
     * @Template()
     * @Method({"POST"})
     */
	
	public function currencyConversionRateAction()
	{
		$this->initAction ();
		$toCurrency = $_POST['toCurrency'];
		$fromCurrency = $_POST['fromCurrency'];
		$this->responseArray = $this->currencyConversionManager->convertCurrencyRate($fromCurrency,$toCurrency);
		return new JsonResponse($this->responseArray);
	}

	/**
     * @author Employee Id: 4471
     * Description : change currency conversion status
     * @param 1 : No paramater
     * @throws Exception: Null
	 * @Route("/admin/{_locale}/currency-conversion-update-status",
     * name="office_brain_corebundle_currencyconversionadmin_currencyconversionupdatestatus",
	 * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
	
	public function updateStatusCurrencyConversionAction()
	{
		$this->initAction ();
		$statusCC = $this->currencyConversionManager->updateStatusCurrencyConversion();
		if(count($statusCC)>0)
		{
			$this->responseArray['enable'] = $statusCC->getEnable();
			$this->responseArray['status'] = 'success';
    		$this->responseArray['success_message'] = $this->get('translator')->trans('currency_conversion.change_status_sucess');
		}
		return new JsonResponse($this->responseArray);
	}
}

?>