<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Adm_role_assigment extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('role_assigment_model', 'assigment');
		$this->gparam['privilage'] = getPrivilage($this->gparam['controller']);
		$this->gparam['restrict'] = 'You Cannot Access This Menu';
    }
	
	public function index()
	{
		if ($this->gparam['privilage']->privilage_view == 'Y') {
			$gridopt = $this->input->get(array('psize', 'pnumber'));
			header('Content-Type: text/html');
			
			$create_button = '""';			
			$update_button = '';
			$delete_button = '';	
			
			if ($this->gparam['privilage']->privilage_create == 'Y') {	
				
				//create button
				$create_button = '<a href="javascript:void(0)"  class="btn btn-primary btn-xs" onclick="create_view();" >
									<i class="fa fa-lg fa-fw fa-plus"></i>Create
								  </a>';
			}
			
			if ($this->gparam['privilage']->privilage_update == 'Y') {
				$update_button = "<a tyle=\"margin:4px;\" class=\"btn btn-success btn-xs\" onclick=\"edit_view('+row.id+')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-lg fa-fw fa-edit\"></i>\
								  </a>";
			}				  
			
			if ($this->gparam['privilage']->privilage_delete == 'Y') {	
				$delete_button = "<a style=\"margin:4px;\" class=\"btn btn-danger btn-xs\" onclick=\"delete_data('+row.id+')\" href=\"javascript:void(0)\" group=\"\" data-toggle=\"tooltip\" title=\"Delete\">\
									<i class=\"fa fa-lg fa-fw fa-trash-o\"></i>\
								</a>"; 
			}
			
			// pnumber, psize
			$data = array(
					'controller'  	=> $this->gparam['controller'],
					'psize'			=> (empty($gridopt['psize'])?10:$gridopt['psize']),
					'pnumber'  		=> (empty($gridopt['pnumber'])?1:$gridopt['pnumber']),
					'create' 		=> $create_button,
					'update' 		=> $update_button,
					'delete'		=> $delete_button
			);
			
			$this->load->view('assigment_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}	
	}
	
	function form() {
		
		if ($this->gparam['privilage']->privilage_view == 'Y') {
			$data['controller'] = $this->gparam['controller'];				
			$data['action'] = base_url()."index.php/".$this->gparam['controller']."/crud";
			$id = $this->uri->segment(3);	
			$data_role = $this->assigment->get_role();
			$role_id = '';
			
			if (!empty($id)) {
				if ($this->gparam['privilage']->privilage_update == 'Y') {	
					//get value for edit
					$edit = $this->assigment->get_value_edit($id);
					$data_menu = $this->assigment->get_value_menu($edit->menu_id);
					$menu_id  = '<select name="menu_id" class="chosen-select" >';
					$menu_id .= '<option value="'.$data_menu->menu_id.'" selected>'.$data_menu->menu_name.'</option>';																
					$menu_id .=	'</select>';
					
					$role_id .= '<label class="select"><select name="role_id">';
				
					foreach ($data_role as $role) {
						if ($role['role_id'] == $edit->role_id) {
							$role_id .= '<option value="'.$role['role_id'].'" selected>'.$role['role_name'].'</option>';
						}
							
					}
					
					if ($edit->privilage_view == 'Y') {
						$data['view'] = 'checked';
						if ($edit->privilage_create == 'Y') {
							$data['create'] = 'checked';
						}
						
						if ($edit->privilage_update == 'Y') {
							$data['edit'] = 'checked';
						}
						
						if ($edit->privilage_delete == 'Y') {
							$data['delete'] = 'checked';
						}
						
						if ($edit->privilage_approve == 'Y') {
							$data['approve'] = 'checked';
						}
					}
					
					$data['id']		= $id;
					$data['header'] = 'Edit Role Assignment';
					$data['submit'] = 'update';	
				} else {
					echo $this->gparam['restrict']; die();
				}	
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {		
					$role_id .= '<label class="select"><select name="role_id" onchange="get_menu(this.value);"><option value="">--Select--</option>';			
					foreach ($data_role as $role) {
						$role_id .= '<option value="'.$role['role_id'].'">'.$role['role_name'].'</option>';
					}
					$menu_id = '<select name="menu_id" class="chosen-select"><option value="">--Select--</option></select>';
					$data['header'] = 'Add Role Assignment';
					$data['submit'] = 'create';
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}		
			$role_id .= '</select></label>';
			$data['role_id'] = $role_id;
			$data['menu_id'] = $menu_id;
			$this->load->view('crud_assigment_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}	
	
	function get_menu() {
	
		$role_id = $this->input->post('role_id');
		$get_menu = $this->assigment->get_menu($role_id);
		echo '<select name="menu_id" class="chosen-select"><option value="">--Select--</option>';								
		echo $get_menu;
		echo '</select></label>';
		echo '<script>
			$(".chosen-select").chosen();
		</script>';
		
	}
	
	
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			
			$insert = $this->assigment->insert_data();
			if ($insert) {
				$json = true;
			}
			
		} else if ($crud == 'update') {
			$update = $this->assigment->update_data();
			if ($update) {
				$json = true;			
			}
				
		}
		
		echo json_encode($json);
		
	}
	
	public function delete() {
		
		$delete = $this->assigment->delete_data();
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	}
	
	function load_data() {
		header('Content-Type: application/jsonp');
        $list = $this->assigment->get_list_data();
		
        echo json_encode($list);
	}
	
	
}
