<?php

namespace OfficeBrain\Bundle\PageBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\Bundle\CacheBundle\Document\RedisKey;

class AdminController extends Controller 
{
	/**
	 *
	 * @author Employee Id: 4074
	 *
	 * Desciption: Define member variables with thier default value
	 *
	 * @param NULL
	 *
	 * @return NULL
	 *
	 * @throws NULL
	 *
	 */
	public function __construct()
	{
		$this->request = null;
		$this->responseArray = array();
		$this->projectSetting = null;
		$this->userManager = null;
	}
	
	/**
	 *
	 * @author Employee Id: 4074
	 *
	 * Desciption: Unset member variables
	 *
	 * @param NULL
	 *
	 * @return NULL
	 *
	 * @throws NULL
	 *
	 */
	public function __destruct()
	{
		unset($this->request);
		unset($this->responseArray);
		unset($this->projectSetting);
		unset($this->userManager);
	}
	
	/**
	 *
	 * @author Employee Id: 4074
	 *
	 * 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->userManager = $this->get('office_brain_user_manager');
		$this->cmsContactUsManager = $this->get('office_brain_cms_contact_us_service_manager');
		$this->page = $this->request->get('page', '1');
		$this->recordsPerPage = $this->request->get('records_per_page', 10);
		$this->coreManager = $this->get('office_brain_core_manager');
	}
	
    /**
     *
     * @Route("/admin/",
     *  name="office_brain_page_admin_home_without_language",
     * )
     * 
     * @Route("/admin/{_locale}",
     * 	name="office_brain_page_admin_home",
     * )
     *
     * @Template("OfficeBrainPageBundle:Admin:index.html.twig")
     */
    public function indexAction()
    {    	
    	$userType = ($this->get('security.context')->getToken()->getUser()->getUserType());
    	if($userType=="distributor"){
    		return $this->redirect($this->generateUrl('office_brain_userbundle_user_my_account_info'));
    	}
		return array();
	}
	
    /**
     *
     * @Route("/admin/{_locale}/dashboard",
     * 	name="office_brain_page_admin_dashboard",
     * )
     *
     * @Template("OfficeBrainPageBundle:Admin:index.html.twig")
     */
    public function dashboardAction()
    { 	
    	$userType = ($this->get('security.context')->getToken()->getUser()->getUserType());
    	if($userType=="distributor"){
    		return $this->redirect($this->generateUrl('office_brain_userbundle_user_my_account_info'));
    	}
    	$this->initAction();
    	$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
    	$this->responseArray['instance_id'] = $this->projectSetting['instance_id'];
		return $this->responseArray;
	}

	/**
	 *
	 * @Route("/admin/{_locale}/user-management",
	 * 	name="office_brain_userbundle_adminuser_user_management",
	 * )
	 *
	 * @Template()
	 */
	public function userManagementAction()
	{
		$this->initAction();
		$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
		return $this->responseArray;
	}

	/**
	 *
	 * @Route("/admin/{_locale}/promotion-discount-management",
	 * 	name="office_brain_page_admin_promotion_discount_management",
	 * )
	 *
	 * @Template()
	 */
	public function promotionDiscountManagementAction()
	{
		$this->initAction();
		$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
		return $this->responseArray;
	}

	/**
	 *
	 * @Route("/admin/{_locale}/marketing-tool",
	 * 	name="office_brain_page_admin_promotion_marketing_tool",
	 * )
	 *
	 * @Template()
	 */
	public function marketingToolManagementAction()
	{
		$this->initAction();
		$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
		return $this->responseArray;
	}

	/**
	 *
	 * @Route("/admin/{_locale}/user-transaction",
	 * 	name="office_brain_page_admin_promotion_user_transaction",
	 * )
	 *
	 * @Template()
	 */
	public function userTransactionManagementAction()
	{
		$this->initAction();
		$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
		return $this->responseArray;
	}

	/**
	 *
	 * @Route("/admin/{_locale}/company-store",
	 * 	name="office_brain_page_admin_company_store",
	 * )
	 *
	 * @Template()
	 */
	public function companyStoreManagementAction()
	{
		$this->initAction();
		$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
		return $this->responseArray;
	}

	/**
	 *
	 * @Route("/admin/{_locale}/product-management",
	 * 	name="office_brain_page_admin_product_management",
	 * )
	 *
	 * @Template()
	 */
	public function productManagementAction()
	{
		$this->initAction();
		$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
		return $this->responseArray;
	}
	
	/**
	 *
	 * @Route("/admin/{_locale}/content-management",
	 * 	name="office_brain_page_admin_content_management",
	 * )
	 *
	 * @Template()
	 */
	public function contentManagementAction()
	{
		$this->initAction();
		$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
		return $this->responseArray;
	}
	
	/**
	 *
	 * @Route("/admin/{_locale}/seo-management",
	 * 	name="office_brain_page_admin_seo_management",
	 * )
	 *
	 * @Template()
	 */
	public function seoManagementAction()
	{
		$this->initAction();
		$this->responseArray['total_user_count'] = $this->userManager->getTotalUserCount();
		return $this->responseArray;
	}


	/**
	 *
	 * @Route("/admin/{_locale}/contact-us-list",
	 * 	name="office_brain_page_admin_contactUsAdminList",
	 * )
	 *
	 * @Template("OfficeBrainPageBundle:Admin:adminContactList.html.twig")
	 */
	public function contactUsAdminListAction()
	{
		$this->initAction();
    	$this->responseArray['records_per_page'] = $this->recordsPerPage;
    	$this->responseArray['page'] = $this->page;
    	$this->prepareSearchParameterArray();
    	$this->responseArray['data'] = $this->cmsContactUsManager->getContactAdminList($this->page,$this->recordsPerPage,$this->searchParameterArray,$this->searchKeyWordsArray);
    	return $this->responseArray;
	}


	private function prepareSearchParameterArray()
	{
        $this->searchParameterArray['start_date'] =  $this->request->get('start_date', null);
        $this->searchParameterArray['end_date'] =  $this->request->get('end_date', null);
        $jsDateFormat = $this->coreManager->getDateJsFormat();
        if($this->searchParameterArray['start_date'] != null){
            $startDate   =   \DateTime::createFromFormat($jsDateFormat,$this->searchParameterArray['start_date']);
            $this->searchParameterArray['start_date'] = $startDate->format('Y-m-d'); 
        }
        
        if($this->searchParameterArray['end_date'] != null){
            $endDate   =   \DateTime::createFromFormat($jsDateFormat,$this->searchParameterArray['end_date']);
            $this->searchParameterArray['end_date'] = $endDate->format('Y-m-d');
        }
		$this->option = $this->request->get('option', null);
		$this->keywords = $this->request->get('keyword', null);
		$this->searchKeyWordsArray[$this->option]=trim($this->keywords);
		 
	}


	/**
     * @author Employee Id: 4217
     * @param 1 : No paramater
     * @return type : Form view and data.
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/contact-details/{contact_id}",
     * name="office_brain_page_admin_contactadmindetail",
     * defaults={"order_id"=null},
     * options={"expose"=true}
     * )
     * @Template("OfficeBrainPageBundle:Admin:adminContactDetail.html.twig")
     * 
     */
    public function adminOrderDetailsAction()
    {
    	$this->initAction();
    	$this->responseArray['contact_id'] = $this->request->get('contact_id', null);
    	$this->responseArray['data'] = $this->cmsContactUsManager->getContactDetailById($this->responseArray['contact_id']);
    	
    	
    	return $this->responseArray; 
    }
	
}