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

	public function Options($model , $user, $param=null){ 
		$depot = ClassRegistry::init('Depot');
		$warehouse = ClassRegistry::init('Warehouse');
		$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($param['dv'])){
            $rIds = $divisionRegion->find('list', array('fields'=>array('region_id'),'conditions'=>array('division_id'=>$param['dv'])));

            $regions = $region->find('list', array('fields'=>array('id','title'),'conditions' => array('id' =>$rIds ), 'order'=>array('title'=>'asc')));
        }else 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 // DIVISIONMANAGER condition updated 3/12/24
		if(!empty($param['rg'])){
            $depots = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$param['rg'] ), 'order'=>array('title'=>'asc')));
        }elseif(!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']) && $user['role_id'] != DIVISIONMANAGER){
			$depots = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$user['region_id'] ), 'order'=>array('title'=>'asc')));
		}else if(!empty($user['division_id']) && $user['role_id'] == DIVISIONMANAGER){
			$rIds = $divisionRegion->find('list', array('fields'=>array('region_id'),'conditions'=>array('division_id'=>$user['division_id'])));
			$depots = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$rIds ), 'order'=>array('title'=>'asc')));
		}else{
			if(!empty($model['division_id'])){
				$rIds = $divisionRegion->find('list', array('fields'=>array('region_id'),'conditions'=>array('division_id'=>$model['division_id']))); //pr($rIds);
				
				$depots = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$rIds), 'order'=>array('title'=>'asc'))); 
				
			}else{
				$depots = $depot->find('list', array('fields'=>array('id','title'),'order'=>array('title'=>'asc')));
			}
		}
		
		// wareghouse listing
		if(!empty($param['rg'])){
            $warehouseList = $warehouse->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$param['rg'] ), 'order'=>array('title'=>'asc')));
        }else if(!empty($model['region_id'])){
			$warehouseList = $warehouse->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$model['region_id'] ), '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'])));
			$warehouseList = $warehouse->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$rIds ), 'order'=>array('title'=>'asc')));
		}else if(!empty($user['region_id'])){
			$warehouseList = $warehouse->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$user['region_id'] ), 'order'=>array('title'=>'asc')));
		}else{
			$warehouseList = $warehouse->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($param['dv']) && !empty($rIds)){
			$rcond['region_id'] = $rIds;
        }
		
		if(!empty($param['rg'])){
			$rcond['region_id'] = $param['rg'];
		}elseif(!empty($model['region_id'])){
			$rcond['region_id'] = $model['region_id'];
		}else if(!empty($user['region_id'])){
			$rcond['region_id'] = $user['region_id'];
		}
		
		if(!empty($param['dp'])){
			$rcond['depot_id'] = $param['dp'];
		}elseif(!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'];
		}*/
		
		if(!empty($param['us'])){
			$param['usr'] = $param['us'];
		}
		if (!empty($param['usr'])) {
			
			$UserRoute = ClassRegistry::init('UserRoute');
			
			//$rtIds = $UserRoute->find('list', array('conditions' => array('user_id' => $param['usr']),'fields' => array('route_id', 'user_id')));

			$userRouteCond['UserRoute.user_id'] = $param['usr'];

			$rtIds = $UserRoute->find('list', array('conditions' => $userRouteCond , 'fields' => array('route_ids', 'user_id')));
			
			if (!empty($rtIds)) {
				$rcond = array_merge($rcond, array('id' => array_keys($rtIds)));
			}
		}

		$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','warehouseList'));
	}
	
	public function Options2($model , $user, $param=NULL){
        $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($param['dv'])){
            $rIds = $divisionRegion->find('list', array('fields'=>array('region_id'),'conditions'=>array('division_id'=>$param['dv'])));

            $regions = $region->find('list', array('fields'=>array('id','title'),'conditions' => array('id' =>$rIds ), 'order'=>array('title'=>'asc')));
        }else 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($param['rg'])){
            $depoList = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$param['rg'] ), 'order'=>array('title'=>'asc')));
        }else if(!empty($model['region_id'])){
            $depoList = $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']) && $user['role_id'] != DIVISIONMANAGER){
            $depoList = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$user['region_id'] ), 'order'=>array('title'=>'asc')));
        }else if(!empty($param['dv']) && empty($param['rg']) ){
            $depoList = $depot->find('list', array('fields'=>array('id','title'),'conditions' => array('region_id' =>$rIds ), 'order'=>array('title'=>'asc')));
        }else{
            $depoList = $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('depoList','regions','lgAreas','states','routes','subdepots','divisions'));
    }
}