<?php

namespace OfficeBrain\Bundle\PrioritizeSupplierBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 *
 * @author 4488
 * @Route("admin/{_locale}/")
 */
class PrioritizeSupplierAdminController extends Controller
{
	protected $locale;
	protected $userId;
	protected $page;
	protected $prioritizeSupplierRecordsPerPage;
	protected $prioritizesupplierManager;
	public function initAction()
	{
		$this->request=$this->getRequest();
		$this->session= $this->request->getSession();
		$this->entityManager = $this->getDoctrine()->getEntityManager();
		$this->location=$this->get('kernel')->getRootDir().'/../web/images/uploads/';
		$this->translator = $this->get('translator');
		$this->projectSetting=$this->request->get('project_setting');
		$this->page = $this->request->get('page', '1');
		$this->prioritizeSupplierRecordsPerPage = 4;
		$this->responseArray['page']=$this->page;
		$this->responseArray['records_per_page'] = $this->prioritizeSupplierRecordsPerPage;
		$this->keywords = $this->request->get('keywords',null);
		$this->requestprioritizeSupplierId = $this->request->get('prioritizeSupplierId',null);
		$this->requestedStatus=$this->request->get('status',null);
		$this->userId = $this->projectSetting['user_id'];
		$this->requestUserId = $this->request->get('userId',null);
		$this->option = $this->request->get('option',null);
		$this->responseArray['prioritizeSupplierId'] = $this->requestprioritizeSupplierId;
		$this->localeTab=$this->request->get("countryCode",null);
		$this->locale=$this->request->get("_locale",null);
		$this->prioritizesupplierManager=$this->get("office_brain_prioritizesupplier_manager");
		$this->prioritizesupplierAccessField= $this->projectSetting['access_rule']['promo_discount']['option']['prioritize_supplier'];
		$this->prioritizesupplierConfigurationField=$this->projectSetting['configuration_rule']['promo_discount']['option']['prioritize_supplier'];
		
		$this->responseArray['prioritizesupplierConfigurationField']=$this->prioritizesupplierConfigurationField;
		$this->responseArray['prioritizesupplierAccessField'] = $this->prioritizesupplierAccessField;
	}
	
	/**
	 * @Route("prioritize-supplier/list",
	 * name="office_brain_prioritizesupplierbundle_list",
	 * options={"expose"=true})
	 * @Template("OfficeBrainPrioritizeSupplierBundle:admin:prioritizSupplier.html.twig")
	 */
	public function prioritizSupplierAction()
	{
		$this->initAction();
		$this->responseArray['culture']=$this->locale;
		$allSuppliers=$this->prioritizesupplierManager->getAllSupplierPrioritize($this->locale,$this->userId,$this->page,$this->prioritizeSupplierRecordsPerPage);
		$this->responseArray['recordSet']=$allSuppliers;
		unset($allSuppliers);
		return $this->responseArray;
	}
	/**
	 * @Route("prioritize-supplier/save",
	 * name="office_brain_prioritizesupplierbundle_save")
	 */
	public function savePrioritizeSupplierAction()
	{
		$this->initAction();
		if($this->request->getMethod() == 'POST'){
			$prioritySupplier=$this->request->request->all();
			$this->prioritizesupplierManager->savePrioritySupplier($prioritySupplier);
			$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.prioritize_supplier.success'));
		}
		return $this->redirect($this->generateUrl('office_brain_prioritizesupplierbundle_list'));
		
	}
	/**
	 * @Route("prioritize-supplier/reset",name="office_brain_prioritizesupplierbundle_reset")
	 */
	public function resetAction()
	{
		$this->initAction();
		if($this->prioritizesupplierManager->removePrioritizeSupplier())
			$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.prioritize_supplier.reset'));
		return $this->redirect($this->generateUrl('office_brain_prioritizesupplierbundle_list'));
	} 
}
