<?php
App::uses('Component', 'Controller');
Class OptionComponent extends Component{
	function initialize(Controller $controller) { $this->controller = $controller ; }

	public function Options($model , $user){
		$depot = ClassRegistry::init('Depot');
		$lga = ClassRegistry::init('LgArea');
		$region = ClassRegistry::init('Region');
		$state =  ClassRegistry::init('State');
		$route=  ClassRegistry::init('Route');
		$subdepot=  ClassRegistry::init('Subdepot');
		
		$division=  ClassRegistry::init('Division');
		
		$divisionRegion=  ClassRegistry::init('DivisionsRegion');
		
		
		// division listing
		if($user['Role']['grade'] < 3 || empty($user['division_id']) ){
			$divisions = $division->find('list', array('fields'=>array('id','title'), 'order'=>array('title'=>'asc')));
		}
		
		// region listing

		if(!empty($model['division_id'])){
			$rIds = $divisionRegion->find('list', array('fields'=>array('region_id'),'conditions'=>array('division_id'=>$model['division_id'])));
			
			$regions = $region->find('list', array('fields'=>array('id','title'),'conditions' => array('id' =>$rIds), 'order'=>array('title'=>'asc')));
			
		}else if(!empty($user['division_id'])){
			$rIds = $divisionRegion->find('list', array('fields'=>array('region_id'),'conditions'=>array('division_id'=>$user['division_id'])));
			
			$regions = $region->find('list', array('fields'=>array('id','title'),'conditions' => array('id' =>$rIds ), 'order'=>array('title'=>'asc')));
		}else{
			$regions = $region->find('list', array('fields'=>array('id','title'),'order'=>array('title'=>'asc')));
		}

		
		
		// depot listing
		if(!empty($model['region_id'])){
			$depots = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$model['region_id'] ), 'order'=>array('title'=>'asc')));
		}else if(!empty($user['region_id'])){
			$depots = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$user['region_id'] ), 'order'=>array('title'=>'asc')));
		}else{
			$depots = $depot->find('list', array('fields'=>array('id','title'),'order'=>array('title'=>'asc')));
		}

		/* // region listing
		if($user['Role']['grade'] < 3 || empty($user['region_id']) ){
			$regions = $region->find('list', array('fields'=>array('id','title'), 'order'=>array('title'=>'asc')));
		} */

		// lga listing
		if(!empty($model['state_id'])){
			$lgAreas = $lga->find('list', array('fields'=>array('id','name'), 'conditions'=>array('state_id'=>$model['state_id']), 'order'=>array('name'=>'asc')));
		}else if(!empty($user['state_id'])){
			$lgAreas = $lga->find('list', array('fields'=>array('id','name'), 'conditions'=>array('state_id'=>$user['state_id']), 'order'=>array('name'=>'asc')));
		}else{
			$lgAreas = $lga->find('list', array('fields'=>array('id','name'), 'order'=>array('name'=>'asc')));
		}

		// State listing 
		$states = $state->find('list', array('fields'=>array('id','state'),'order'=>array('state'=>'asc')));

		$rcond = array();
		if(!empty($model['region_id'])){
			$rcond['region_id'] = $model['region_id'];
		}else if(!empty($user['region_id'])){
			$rcond['region_id'] = $user['region_id'];
		}
		if(!empty($model['depot_id'])){
			$rcond['depot_id'] = $model['depot_id'];
		}else if(!empty($user['depot_id'])){
			$rcond['depot_id'] = $user['depot_id'];
		}
		/*if(!empty($user['region_id'])){
			$rcond['region_id'] = $user['region_id'];
		}
		if(!empty($user['depot_id'])){
			$rcond['depot_id'] = $user['depot_id'];
		}
		if(empty($user['region_id']) && !empty($model['region_id'])){
			$rcond['region_id'] = $model['region_id'];
		}
		if(empty($user['depot_id']) && !empty($model['depot_id'])){
			$rcond['depot_id'] = $model['depot_id'];
		}*/

		$routes = $route->find('list', array('conditions'=>$rcond, 'fields'=>array('id','title'), 'order'=>array('title'=>'asc')));
		
		$scond = array();
		if(!empty($model['region_id'])){
			$scond['region_id'] = $model['region_id'];
		}else if(!empty($user['region_id'])){
			$scond['region_id'] = $user['region_id'];
		}
		if(!empty($model['depot_id'])){
			$scond['depot_id'] = $model['depot_id'];
		}else if(!empty($user['depot_id'])){
			$scond['depot_id'] = $user['depot_id'];
		}
		$subdepots = $subdepot->find('list', array('conditions'=>$scond, 'fields'=>array('id','title'), 'order'=>array('title'=>'asc')));
		
		 $this->controller->set(compact('depots','regions','lgAreas','states','routes','subdepots','divisions'));
	}
}