<?php
App::uses('AppController', 'Controller');
class BrandController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public function index(){
		//$this->gradPermission(1);
		$conditions = array('status'=>1);
		
		$order=$this->paginate['order'] = array('Brand.name' => 'asc');
        $querystring = $this->request->query;
		$this->set('querystring', $querystring);
		if(isset($this->request->query['export'])){   			
			$brands = $this->Brand->find('all', array('conditions' =>$conditions,'order'=>$order));	
		} else {
		    $brands = $this->paginate('Brand', $conditions); //pr($brands);
		} 
		$this->set('brands', $brands);
		
		if(isset($this->request->query['export'])){  				
			$this->set('filename', 'Brand');
			$this->render('export_brand','export_xls');
		}
		
	}
	public function add(){
		$this->loadModel('BrandGroup');
		//$this->gradPermission(1);
		if ($this->request->data){
			$this->request->data['Brand']['user_id'] = $this->Auth->user('id');
			$this->Brand->save($this->request->data);
			$this->Session->setFlash(__('Brand was added successfully'));
			$this->redirect('/brand');
		}
		$groups = $this->BrandGroup->find('list', array('fields'=>array('id', 'name'), 'order'=>array('name'=>'asc')));
		$this->set('groups', $groups);
	}
	
	public function edit($id=0){
		$this->loadModel('Audit');
		$userData = $this->Brand->read(null, $id);
		//$set_var=$this->gradPermission(1);
		if (!$id){
			$this->redirect('/brand');
		}
		
		if ($this->request->data){
			$this->Brand->save($this->request->data);
			$this->Audit->saveDeletedData($userData['Brand']['id'], 'Brand', $this->Auth->user('id') , serialize($userData),'Brand Has been updated', STATUS_EDIT);
			$this->Session->setFlash(__('Brand was updated successfully'));
			$this->redirect('/brand');
		}
		
		$this->Brand->id = $id;
		$this->request->data = $this->Brand->read();
		$this->loadModel('BrandGroup');
		$groups = $this->BrandGroup->find('list', array('fields'=>array('id', 'name'), 'order'=>array('name'=>'asc')));
		$this->set('groups', $groups);
		//if($set_var){
		$this->render('add');
		//}
	}
	public function delete($id=0){
		//$this->gradPermission(1);
		if (!$id){
			$this->redirect('/brand');
		}
		$this->Brand->id = $id;
		//$this->Brand->saveField('status', DELETED);;
		$this->Session->setFlash(__('Brand was deleted successfully'));
		$this->redirect('/brand');
	}
	
	
	public function groups($opt = '', $id = ''){
		//$this->gradPermission(3);
		$this->loadModel('BrandGroup');
		$this->set('title_for_layout', 'BrandGroup :: '.ucwords($opt));
		switch($opt){

			case 'add':
			case 'edit':			
		        //$this->roleBasePermission(array(SUPPORT));
				$this->_addgroup($id);
			break;
			case 'delete':
			//$this->gradPermission(2);
				if (!$this->request->is('post')) {
					throw new MethodNotAllowedException();
				}
				if(!$id){
					$this->redirect(array('controller'=>'brand', 'action'=>'groups'));
				}
				$this->BrandGroup->id = $id;
				$group = $this->BrandGroup->read();
				$data = array();
				$data['serialize'] = serialize($group);//pr($data['serialize']);die();
				$data['model_id'] = $group['BrandGroup']['id'];
				$this->BrandGroup->delete();
				$this->loadModel('Audit');
					$this->Audit->saveDeletedData($data['model_id'], 'BrandGroup', $this->Auth->user('id') , $data['serialize'],'BrandGroup Has been Deleted',STATUS_DELETE);
				$this->Session->setFlash(__('BrandGroup was deleted successfully'));
				$this->redirect(array('controller'=>'brand', 'action'=>'groups'));
				
			break;
			/* case 'view':
				$this->_viewGroup($id); */
			break;
			default:
				$this->_brandGroupList();
			break;
		}
	}
	function _brandGroupList(){
		$this->loadModel('BrandGroup');
		
		$conditions = array();
		if (!empty($this->request->query['q']) ){
			$conditions['BrandGroup.name LIKE'] =  '%'.$this->request->query['q'].'%';
		}
		
		$order=$this->paginate['order'] = array('BrandGroup.name' => 'asc');

		$groups = $this->paginate('BrandGroup', $conditions);


		$this->set(compact('offset','groups'));

	}

	function _addgroup($id = 0){
		$this->loadModel('Audit');
		$userData = $this->BrandGroup->read(null, $id);
		//$is_allow=$this->gradPermission(1);
		if ($this->request->data){ 
			if ($id){
				$conditions = array('LOWER(BrandGroup.name)'=>strtolower($this->request->data['BrandGroup']['name']), 'BrandGroup.id !=' => $id);
				$saveStatus = 'updated';
			}else{
				$conditions = array('LOWER(BrandGroup.name)'=>strtolower($this->request->data['BrandGroup']['name']));
				$saveStatus = 'added';
			}
			$is_exists = $this->BrandGroup->find('count', array('conditions'=>$conditions));
			if ($is_exists > 0){
				$this->Session->setFlash(__('"%s" already exists', $this->request->data['BrandGroup']['name']));
			}else{
				/* if($id){
					$check = $this->_region_user_customers($this->request->data);
				}else{
					$check = true;
				}
				if($check){ */
					if(@$this->request->data['data']){
						$this->request->data['Depot']['opening_stocks'] = serialize($this->request->data['data']['opening_stocks']);
						$this->request->data['Depot']['opening_data'] = serialize($this->request->data['data']);
					} 
					$this->BrandGroup->save($this->request->data);
					if ($id){
						$conditions = array('LOWER(BrandGroup.name)'=>strtolower($this->request->data['BrandGroup']['name']), 'BrandGroup.id !=' => $id);
						
						$this->Audit->saveDeletedData($userData['BrandGroup']['id'], 'BrandGroup', $this->Auth->user('id') , serialize($userData),'BrandGroup Has been updated', STATUS_EDIT);
					}
					$this->Session->setFlash(__('BrandGroup was %s successfully', $saveStatus));
					$this->redirect(array('controller'=>'brand', 'action'=>'groups'));
				/* }else{
					$this->set('continue', true);
				} */
				
			}
		}else if ($id){
			$this->BrandGroup->id = $id;
			$this->request->data = $this->BrandGroup->read();
			/* 
			if ($this->request->data['Depot']['opening_stocks']){
				$this->request->data['data']['opening_stocks'] = unserialize($this->request->data['Depot']['opening_stocks']);
				
			}
			if ($this->request->data['Depot']['opening_data']){
				$this->request->data['data'] = unserialize($this->request->data['Depot']['opening_data']);
			} */
		} 
		
		/* $regions = $this->Region->find('list', array('fields'=>array('id', 'title'), 'order'=>array('title'=>'asc')));
		$this->loadModel('State');
		$states = $this->State->find('list', array('fields'=>array('id', 'state'), 'order'=>array('state'=>'asc')));
		
		$this->loadModel('Brand');
		$brands = $this->Brand->find('list', array('fields'=>array('id', 'name'), 'order'=>array('name'=>'asc')));
		$this->set(compact('regions', 'states','brands')); */
		//if($is_allow){
			$this->render('addgroup');
		//}
	}
	
 }