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

class User_Groups extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('User_Model');
		$this->load->model('Common_Model');
		$this->load->model('User_Group_Model');
		$this->load->model('Sequerty_Model');
		
	}

    var $main_menu_name = "settings";
	var $sub_menu_name = "user_groups";

	public function index()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
        $this->load->view('user_groups',$data);
	}

	public function permissions()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$user_group_id=$this->uri->segment('3');
		
		$data['user_group_id']=$user_group_id;
		$data['user_group_permission_page_list'] = $this->Common_Model->list_menu();
		$data['user_group_group_info']=$this->User_Group_Model->get_user_group_info_by_id($user_group_id);
		
		//echo "<pre>";
		//print_r($data['user_group_permission_page_list']);
		//echo "</pre>";
// 		exit();
		//get group name by id
		
        $this->load->view('user_groups_permissions',$data);
	}
		
	public function save_user_group_permissions()
	{
		//print_r($_REQUEST);
		$user_group_id=$this->input->post('user_group_id');
		$data['user_group_permission_page_list'] = $this->Common_Model->list_menu();
		$this->Common_Model->reset_group_permission($user_group_id);
		
	    $status = 1;
	    $validation = "Done!";
		
		foreach ($data['user_group_permission_page_list'] as $row){
			$rowname_view='row_view_'.$row['menu_id'];
			//echo $rowname_view."\n";
			//echo $this->input->post($rowname_view);
			//$rowname_add='row_add_'.$row['user_group_permission_page_name'];
			//$rowname_edit='row_edit_'.$row['user_group_permission_page_name'];
			//$rowname_delete='row_delete_'.$row['user_group_permission_page_name'];
				$where=array(
					'menu_id' => $row['menu_id'],
					'group_id'=>$user_group_id,
					'gp_id'=>1		
				);
				$has_permission =($this->input->post($rowname_view) == 1)? 1:0;
				
				$perm_data = array(
                    "has_permission" => $has_permission
                );
				
				$result = $this->Common_Model->update_rule($where,$perm_data);
				if(!$result){
				    $status = 0;
				    $validation = "error!";
				    break;
				}
		}
		$st = array('status' =>$status,'validation' =>$validation);
        echo json_encode($st);
	}	

public function get_list_user_group($value='')
	{
	        $values = $this->User_Group_Model->get_all_user_group_list();
	        $data = array();

	        if (!empty($values)) {
	            foreach ($values as $users) {
					
	            $row = array();
	                $row[] = $users->user_group_id;
	                $row[] = $users->user_group_name;					
					$actionTxtDisble='';
					$actionTxtEnable='';
					$actionTxtUpdate='';
					$actionTxtDelete='';
					$actionTxtUpdate='<a data-toggle="modal" href="'.base_url('system_settings/permissions').'/'.$users->user_group_id.'" class="btn btn-xs btn-blue tooltips" data-placement="top" data-original-title="Edit 
Group Permissions"><i class="glyphicon fa fa-tasks"></i></a> &nbsp;';
					
				
				$row[]=$actionTxtUpdate;
	
	                $data[] = $row;
	            }


	            $output = array('data' =>$data);
	            echo json_encode($output);
	        }else{
	            $output = array('data' =>'');
	            echo json_encode($output);

	        }
	   }
	   
	   function save_user_group()
	{
	    $user_group_name=$this->input->post('user_group_name');
	    
		$this->load->library('form_validation'); //form validation lib
		$this->form_validation->set_rules('user_group_name', 'User Group Name', 'required|is_unique[customer.cus_code]');
		
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else{
        	$data=array(
        		'user_group_name'=>$user_group_name,
        		'user_group_status' => 1
        	);
    		
    		$lastid=$this->User_Group_Model->save_user_group($data);
    		
    		if($lastid) {
    		    //  add rules start
    		  $menu= $this->Common_Model->list_menu();
              $gpt = $this->Common_Model->get_permission_types();
              $ugp = $this->Common_Model->get_user_groups();
            
                foreach($menu as $mnu){
                    foreach($gpt as $ugpt){
                        $prm = 0;
                        $rule = array(
                            'menu_id' => $mnu['menu_id'],
                            'group_id' => $lastid,
                            'gp_id' => $ugpt['usrgp_permission_id'],
                            'has_permission' => $prm
                        );
                        $this->Common_Model->add_rule($rule);
                    }
                }
    		  // end add rules
    		    echo json_encode(array('id'=>$lastid,'status' =>1));
    		} else {
                echo json_encode(array('status'=>'0'));
    		}
        }
	}
	

}