<?php namespace App\Models\Admin;

class AdminModel{
	protected $db;
	protected $session;
	protected $project;
	
    public function __construct(){
        $this->db = \Config\Database::connect();
		$this->session = session();
		$this->project = config('Config\\Project');
    }
	
	protected function add_insert_log(&$data,$device=false){
		$user_id = $this->session->get($this->project->session_admin_user_id);
		$data['added_by'] = $data['last_updated_by'] = $user_id;
		$data['added_date'] = $data['last_updated_date'] = date('Y-m-d H:i:s');
		if($device){
			$data['added_by_device'] = $data['last_updated_by_device'] = 'Web App';
		}		
	}
	
	protected function add_update_log(&$data,$device=false){
		$user_id = $this->session->get($this->project->session_admin_user_id);
		$data['last_updated_by'] = $user_id;
		$data['last_updated_date'] = date('Y-m-d H:i:s');
		if($device){
			$data['last_updated_by_device'] = 'Web App';
		}
	}
	
	private function get_menu_list(){
		$user_id = $this->session->get($this->project->session_admin_user_id);
		$builder = $this->db->table('menu as m');
		$builder->select('m.*,a.authorization_id,is_list')
			->join('authorization as a',
					'a.menu_id=m.menu_id AND a.user_id='.$user_id,
					'left')
			->where('m.status','Active')
			->where('m.is_deleted','No')
			->orderBy('m.sequence','ASC');
		if($this->session->get('user_type')!="Developer"){
			$builder->where('m.user_type!=','Developer');
		}
		if( ! in_array($this->session->get('user_type'),['Developer','Admin'])){
			//$builder->where('a.authorization_id is NOT NULL');
		}
		$query = $builder->get();		
		return $query->getResultArray();
	}
	
	protected function build_menu_tree(array $elements, $parentId = 0) {
		$branch = array();

		foreach ($elements as $element) {
			if ($element['parent_menu_id'] == $parentId && (is_null($element['authorization_id']) || $element['is_list']=="Yes")) {
				$children = $this->build_menu_tree($elements, $element['menu_id']);
				if ($children) {
					$element['children'] = $children;
				}
				$branch[] = $element;
			}
		}

		return $branch;
	}
	
	public function get_menu_dtl(){
		$main_menu_list = $this->get_menu_list();
		$menu_dtl['active_link'] = $this->get_active_link();
		
		$menu_dtl['active_link_path'] = $this->get_active_link_path($main_menu_list, $menu_dtl['active_link']['menu_id']);
		$menu_dtl['tree'] = $this->build_menu_tree($main_menu_list,0);
		return $menu_dtl;
	}
	
	public function get_active_link(){
		$controller = get_current_controller();
		$builder = $this->db->table('menu');
		$query = $builder->where('controller',$controller)->get();
		return $query->getRowArray();
	}
	
	public function get_active_link_path($menu_arr, $active_menu_id, $key='menu_id' ){
		$out = array();
		foreach ($menu_arr as $menu){
			if ($menu[$key] == $active_menu_id){
				$out = $this->get_active_link_path($menu_arr, $menu['parent_menu_id'],'menu_id');
				$out[]=$menu;
			}
		}
		return $out;
	}
	
	public function allowed($role,$menu_id){
		$builder = $this->table('user');
		$query = 
		$is_action_authorize = $this->common->getOneRecField($action,'authorization','where user_id='.$this->session->userdata('apple_adminusr').'  AND menu_id='.$menu_id.'');  
		if($this->session->userdata('user_type')!='Admin'){
			if($is_action_authorize[$action]!='Yes'){				
				redirect(get_admin_path('secure/home'));
			}
		}  
	}
	
	public function get_user_roles(){
		$builder = $this->db->table('authorization');
		$query = $builder->select('menu_id,is_add,is_edit,is_list')
					->where('status','Active')
					->where('is_deleted','No')
					->where('user_id',$this->session->get($this->project->session_admin_user_id))
					->orderBy('menu_id','ASC')
					->get();
		return $query->getResultArray();
	}
}	