<?php
namespace OfficeBrain\Bundle\RequestInfoBundle\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\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use OfficeBrain\Bundle\RequestInfoBundle\Form\CommentRequestInfoType;
use OfficeBrain\Bundle\RequestInfoBundle\Entity\CommentRequestInfo;


/**
 *
 *
 * @Route("", defaults={"rule_service"="rule_charges"})
 */
class RequestInfoAdminController extends Controller
{
	protected $user;
	protected $form;
	protected $request;
	protected $formData;
	protected $responseArray;
	protected $commonLibraryManager;
	
	/**
	 *
	 * @author Employee Id: 4203
	 *
	 * Desciption: Define member variables with thier default value
	 *
	 * @param NULL
	 *
	 * @return NULL
	 *
	 * @throws NULL
	 *
	 */
	public function __construct()
	{
		$this->user = null;
		$this->form = null;
		$this->request = null;
		$this->formData = null;
		$this->responseArray = array();
		$this->commonLibraryManager = null;
		$this->locale = null;
		$this->productId = null;
		$this->productSlug = null;
		$this->queueApiManager = null;
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 *
	 * Desciption: Unset member variables
	 *
	 * @param NULL
	 *
	 * @return NULL
	 *
	 * @throws NULL
	 *
	 */
	public function __destruct()
	{
		unset($this->user);
		unset($this->form);
		unset($this->request);
		unset($this->formData);
		unset($this->commonLibraryManager);
		unset($this->productId);
		unset($this->productSlug);
		unset($this->locale);
		unset($this->productId);
		unset($this->productSlug);
		unset($this->locale);
		unset($this->queueApiManager);
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 *
	 * Desciption: Create and initialize various required object and member variables
	 *
	 * @param NULL
	 *
	 * @return NULL
	 *
	 * @throws NULL
	 *
	 */
	public function initAction()
	{
		$this->request = $this->getRequest();
		$this->projectSetting = $this->request->get('project_setting');
		$this->productId = $this->request->get('product_id',null);
		$this->productSlug = $this->request->get('product_slug',null);
		$this->cultureId = $this->projectSetting['culture_id'];
		$this->locale = $this->projectSetting['culture_code'];
		$this->responseArray = array();
		$this->configurationRule = $this->projectSetting['configuration_rule']['transaction']['request_info'];
		$this->requestInfoManager = $this->get('office_brain_request_info_manager');
		$this->commonLibraryManager = $this->get('office_brain_common_library');
		$this->page = $this->request->get('page', '1');
		$this->recordsPerPage = $this->request->get('records_per_page', $this->configurationRule['list_admin']['option']['no_of_product']);
		$this->erpIntegration = $this->request->get('project_setting')['configuration_rule']['erp_integration'];
         
         if ($this->has('office_brain_queue_api_manager')) 
         {
            $this->request->attributes->set('object', 'RequestInfo');
            $this->queueApiManager = $this->get('office_brain_queue_api_manager');
        }
	}
	
	/**
     * @author Employee Id: 4248
     * Description : Use to submit guest user info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/my-inquiry-list",
     * name="office_brain_requestinfobundle_requestinfoadmin_myinquirylist",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function myInquiryListAction()
    {
    	$this->initAction();
		$this->responseArray['records_per_page'] = $this->recordsPerPage;
		$this->responseArray['page'] = $this->page;
		$this->responseArray['data'] = $this->requestInfoManager->getRequestAdminSampleList($this->page,$this->recordsPerPage);
		//$this->responseArray['data'] = array();
    	return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4248
     * Description : Use to submit guest user info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/list-request-info",
     * name="office_brain_requestinfobundle_requestinfoadmin_listrequestinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"POST"})
     */
    public function listRequestInfoAction()
    { 
    	$this->initAction();
    	$this->responseArray['records_per_page'] = $this->recordsPerPage;
    	$this->responseArray['page'] = $this->page;
    	$this->responseArray['data'] = $this->requestInfoManager->getRequestInfoAdminList($this->page,$this->recordsPerPage);
    	return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4248
     * Description : Use to submit guest user info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/detail-request-info/{request_info_id}",
     * name="office_brain_requestinfobundle_requestinfoadmin_detailrequestinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function adminDetailRequestInfoAction()
    {
    	$this->initAction();
    	$this->responseArray['data'] = $this->requestInfoManager->getRequestInfoDetailById();
    	$productInfoRequest = $this->responseArray['data']->getProductInfoRequest();
    	foreach($productInfoRequest as $productInfoRequestIndex => $productInfoRequestData){
    		$variationPricing = $productInfoRequestData->getVariationPricing();
    		if($variationPricing != ''){
    			$this->responseArray['variationPricing'] = $variationPricing;
    		}
    	}
    	return $this->responseArray;
    }	
    
     /**
     * @author Employee Id: 4217
     * Description : Use to render Commemt Request Info form.
     * @param 1 : No paramater
     * @return type : Form view.
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/add-comment-request-info/{request_id}",
     * name="office_brain_requestinfobundle_requestinfo_adminaddcommentrequestinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function adminAddCommentRequestInfoAction()
    {
    	$this->initAction();
	   	//$userDetail = $this->requestInfoManager->getUserDetail($this->request->get('user_id'),'user');
    	$this->form=$this->createForm(new CommentRequestInfoType(), new CommentRequestInfo() );
    	$this->responseArray['form'] = $this->form->createView ();
    	$this->responseArray['request_id'] = $this->request->get('request_id');
    	//$this->responseArray['user_name'] = $userDetail->getFirstName().' '.$userDetail->getLastName();
    	return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4217
     * Description : Use to submit Commemt Request Info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/add-comment-request-info",
     * name="office_brain_requestinfobundle_requestinfo_adminprocesscommnentrequestinfosubmit",
     * options={"expose"=true}
     * )
     * @Template("OfficeBrainRequestInfoBundle:RequestInfoAdmin:adminAddCommentRequestInfo.html.twig")
     * @Method({"POST"})
     */
    public function adminAddCommentRequestInfoSubmitAction()
    {
    	$this->initAction();
    	$this->form=$this->createForm(new CommentRequestInfoType(), new CommentRequestInfo() );
    	$this->form->handleRequest($this->request);
    	if ('POST' === $this->request->getMethod()){
			$requestId=$this->form->getData ()->requestInfoId;
    		if ($this->form->isValid()){
    			$result=$this->requestInfoManager->setCommentRequestInfo($this->form->getData());
    			switch ($result['status']) {	
    			case 'COMMENT_ADDED_SUCESSFULLY':
	    			if ($this->request->isXmlHttpRequest ()) {
	    				$this->responseArray['url']= $this->generateUrl('office_brain_requestinfobundle_requestinfo_adminlistcommentrequestinfo',array('request_info_id'=>$requestId));
	    				$this->responseArray['status']='success';
	    				$this->responseArray['success_message']=$this->get('translator')->trans('request_info.comment_added_successfully');
	    				return new JsonResponse ( $this->responseArray );
	    			}
    			break;
    			}
    		}
    		else if ($this->request->isXmlHttpRequest ()) {
    			return new JsonResponse ( $this->commonLibraryManager->prepareFormJsonError($this->form) );
    		}
    	}
    	return array (
    			'form' => $this->form->createView (),
    			'request_id'=>$requestId
    	);
    }
    
    /**
     * @author Employee Id: 4217
     * Description : Use to submit Commemt Request Info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("admin/{_locale}/list-comment-request-info/{request_info_id}",
     * name="office_brain_requestinfobundle_requestinfo_adminlistcommentrequestinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function adminListCommentRequestInfoAction()
    {
    	$this->initAction();
    	$requestInfoDetail=$this->requestInfoManager->getCommentRequestInfoList();    	
    	return array('data'=>$requestInfoDetail);
    }
    
    /**
     * @author Employee Id: 4248
     * Description : Use to dalete a record
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/delete-request-info/{request_info_id}",
     * name="office_brain_requestinfobundle_requestinfoadmin_deleterequestinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function adminDeleteRequestInfoAction()
    {
    	$this->initAction();
    	$requestInfoDetail=$this->requestInfoManager->deleteRequestInfoDetailById();
    	switch ($requestInfoDetail['status']) {
    		case 'ERROR_NOT_FOUND':
				$this->responseArray['error_message'] = $this->get('translator')->trans('admin.request_info.no_record');
    			break;
    		case 'SUCCESS':
			$data = $requestInfoDetail['data'];
			if ($this->erpIntegration && $this->erpIntegration['orderManagement'] == 1 && !empty($this->queueApiManager)) {
		    	$this->queueApiManager->mapQueue("remove", array('id' =>$this->request->get('request_info_id'), 'deleted_at' => $data->getDeletedAt()->format('Y-m-d H:i:s'), 'culture' => $data->getCulture()));
			}
			$this->responseArray['status'] = 'success';
			$this->responseArray['inquiry_list'] = 'inquiry';
			$this->responseArray['success_message'] = $this->get('translator')->trans('admin.request_info.remove_success_message');
    		break;
    	}
    	return new JsonResponse($this->responseArray);
    }
    
    /**
     * @author Employee Id: 4217
     * Description : Use to submit Commemt Request Info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("admin/{_locale}/guest-customer-detail/{user_id}/{user_type}",
     * name="office_brain_requestinfobundle_requestinfo_adminguestcustomerdetailinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function adminGuestCustomerDetailInfoAction()
    {
    	$this->initAction();
    	$this->responseArray['userDetail'] = $this->requestInfoManager->getUserDetail($this->request->get('user_id'),$this->request->get('user_type'));
    	return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4217
     * Description : Use to submit Commemt Request Info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("admin/{_locale}/customer-detail/{user_id}/{user_type}",
     * name="office_brain_requestinfobundle_requestinfo_admincustomerdetailinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function adminCustomerDetailInfoAction()
    {
    	$this->initAction();
    	$this->responseArray['userDetail'] = $this->requestInfoManager->getUserDetail($this->request->get('user_id'),$this->request->get('user_type'));
    	return $this->responseArray;
    }

    /**
     * @author Employee Id: 4217
     * Description : Use to submit Commemt Request Info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("admin/{_locale}/guest-customer/{user_id}/{user_type}",
     * name="office_brain_requestinfobundle_requestinfo_adminguestcustomerinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function adminGuestCustomerInfoAction()
    {
    	$this->initAction();
    	$this->responseArray['userDetail'] = $this->requestInfoManager->getUserDetail($this->request->get('user_id'),$this->request->get('user_type'));
    	return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4217
     * Description : Use to submit Commemt Request Info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("admin/{_locale}/customer/{user_id}/{user_type}",
     * name="office_brain_requestinfobundle_requestinfo_admincustomerinfo",
     * options={"expose"=true}
     * )
     * @Template()
     * @Method({"GET"})
     */
    public function adminCustomerInfoAction()
    {
    	$this->initAction();
    	$this->responseArray['userDetail'] = $this->requestInfoManager->getUserDetail($this->request->get('user_id'),$this->request->get('user_type'));
    	return $this->responseArray;
    }


    /**
     * @author Employee Id: 4248
     * Description : Use to submit guest user info form.
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/list-ask-question",
     * name="office_brain_requestinfobundle_requestinfoadmin_listaskquestion",
     * options={"expose"=true}
     * )
     * @Template("OfficeBrainRequestInfoBundle:RequestInfoAdmin:listaskquestion.html.twig")
     * @Method({"GET"})
     */
    public function listAskQuestionAction()
    { 
      $this->initAction();
      $this->responseArray['records_per_page'] = $this->recordsPerPage;
      $this->responseArray['page'] = $this->page;
      $this->responseArray['data'] = $this->requestInfoManager->getAdminAskQuestionList($this->page,$this->recordsPerPage);
      return $this->responseArray;
    }
    
}
