<?php
class Dashboard extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		if($this->session->userdata('logged_in') != true){
			redirect('login');
		}
	}
	
	public function index()
	{
		$data['content'] = "dashboard";
		$this->load->view('include/template',$data);
	}
	
	public function rti()
	{
		$chk = $this->def_model->chk_permission(array('ad.user_id'=>$this->session->userdata('userid')));
		if(in_array('rti',$chk))
		{
			if($this->uri->segment(3)){
				$data['info'] = $this->def_model->join_two_row('kp_rti rt','kp_user us','rt.user_id=us.user_id',array('rt.user_id'=>$this->uri->segment(3)));
				$data['chat'] = $this->def_model->get_where('kp_rti_chat',array('rti_id'=>$this->uri->segment(3)));
			}
			
			if($this->input->post('save'))
			{
				$this->form_validation->set_rules('status','Status','required');
				$this->form_validation->set_rules('priority','Priority','required');
				$this->form_validation->set_rules('ans_query','Answer','required');
				$this->form_validation->set_rules('rti_id','RTI','required');
				
				if($this->form_validation->run() == true)
				{
					if($this->input->post('status') == "reject"){
					$insert = array(
									'status' => $this->input->post('status'),
									'reject_reason_user' => $this->input->post('rej_user'),
									'reject_reason_comission' => $this->input->post('rej_comission'),
									'priority' => '',
								);	
					}else{
						$insert = array(
									'status' => $this->input->post('status'),
									'priority' => $this->input->post('priority'),
									'send_back' => $this->input->post('snd_user'),
									'send_comission' => $this->input->post('snd_comission')
								);
					}
					
					$query = $this->def_model->update_data('kp_rti',$insert,array('rti_id'=>$this->input->post('rti_id')));
					
					$insert_2 = array(
										'rti_id'  => $this->input->post('rti_id'),
										'user_id' => $this->input->post('user_id'),
										'dep_id'  => $this->input->post('dep_id'),
										'sender'  => 'department',
										'recever' => 'user',
										'subject' => $this->input->post('subject'),
										'query'   => $this->input->post('ans_query')
									);
									
					$query1 = $this->def_model->insert_data('kp_rti_chat',$insert_2);
					
					if($query && $query1){
						$data['sucess'] = "Answer has been posted to User";
					}
				}
			}
			$data['query'] = $this->def_model->join_two_result('kp_rti rt','kp_rti_chat ch','rt.rti_id=ch.rti_id','rt.rti_id');
			$data['content'] = "rti";
			$this->load->view('include/template',$data);
		}else{
			redirect('dashboard/eror'); 
		}
	}
	
	public function group()
	{
		$chk = $this->def_model->chk_permission(array('ad.user_id'=>$this->session->userdata('userid')));
		if(in_array('group',$chk))
		{
			if($this->uri->segment(3) == "show"){
				$data['info'] = $this->def_model->single_rec('kp_group',array('group_id'=>$this->uri->segment(4)));
			}
			
			if($this->uri->segment(3) == "delete"){
				$del = $this->def_model->del_rec('kp_group',array('group_id'=>$this->uri->segment(4)));
			}
			
			if($this->input->post('save') && !$this->input->post('group_id'))
			{
				$this->form_validation->set_rules('name','Group Name','required');
				$this->form_validation->set_rules('desc','Description','required');
				
				if($this->form_validation->run() == true)
				{
					
					$insert = array(
								'group_name' => $this->input->post('name'),
								'description' => $this->input->post('desc')
							);
									
					$query = $this->def_model->insert_data('kp_group',$insert);
					
					if($query){
						$data['sucess'] = "Group has been Created";
					}
				}
				
			}
			// update group
			if($this->input->post('save') && $this->input->post('group_id'))
			{
				$this->form_validation->set_rules('name','Group Name','required');
				$this->form_validation->set_rules('desc','Description','required');
				
				if($this->form_validation->run() == true)
				{
					
					$insert = array(
								'group_name' => $this->input->post('name'),
								'description' => $this->input->post('desc')
							);
									
					$query = $this->def_model->update_data('kp_group',$insert,array('group_id'=>$this->input->post('group_id')));
					
					if($query){
						$data['sucess'] = "Group has been Updated";
					}
				}
				
			}
			$data['query'] = $this->def_model->get_where('kp_group');
			
			$data['content'] = "group";
			$this->load->view('include/template',$data);
		}else{
			redirect('dashboard/eror'); 
			}
	}
	
	public function eror()
	{
		$data['query'] = $this->def_model->get_single('kp_admin' , array('user_id'=>$this->session->userdata('userid')) , 'name');
		$data['content'] = "eror";
		$this->load->view('include/template',$data);
	}
	
	public function user()
	{
		$chk = $this->def_model->chk_permission(array('ad.user_id'=>$this->session->userdata('userid')));
		if(in_array('user',$chk))
		{
			if($this->uri->segment(3) == "show"){
				$data['info'] = $this->def_model->single_rec('kp_admin',array('user_id'=>$this->uri->segment(4)));
			}
			
			if($this->uri->segment(3) == "delete"){
				$del = $this->def_model->del_rec('kp_admin',array('user_id'=>$this->uri->segment(4)));
			}
			
			if($this->input->post('save') && !$this->input->post('user_id'))
			{
				$this->form_validation->set_rules('name','User Name','required');
				$this->form_validation->set_rules('fname','Father Name','required');
				$this->form_validation->set_rules('group','Group','required');
				$this->form_validation->set_rules('email','Email','required');
				$this->form_validation->set_rules('password','Password','required');
				
				if($this->form_validation->run() == true)
				{
					
					$insert = array(
								'name' => $this->input->post('name'),
								'group_id' => $this->input->post('group'),
								'father_name' => $this->input->post('fname'),
								'email' => $this->input->post('email'),
								'password' => $this->input->post('password')
							);
									
					$query = $this->def_model->insert_data('kp_admin',$insert);
					
					if($query){
						$data['sucess'] = "User has been Created";
					}
				}
				
			}
			// update user
			if($this->input->post('save') && $this->input->post('user_id'))
			{
				$this->form_validation->set_rules('name','User Name','required');
				$this->form_validation->set_rules('fname','Father Name','required');
				$this->form_validation->set_rules('group','Group','required');
				$this->form_validation->set_rules('email','Email','required');
				$this->form_validation->set_rules('password','Password','required');
				
				if($this->form_validation->run() == true)
				{
					
					$insert = array(
								'name' => $this->input->post('name'),
								'group_id' => $this->input->post('group'),
								'father_name' => $this->input->post('fname'),
								'email' => $this->input->post('email'),
								'password' => $this->input->post('password')
							);
									
					$query = $this->def_model->update_data('kp_admin',$insert,array('user_id'=>$this->input->post('user_id')));
					
					if($query){
						$data['sucess'] = "User has been Updated";
					}
				}
			}
			$data['group'] = $this->def_model->get_where('kp_group');
			$data['query'] = $this->def_model->join_two_result('kp_admin ad','kp_group gr','ad.group_id=gr.group_id');
			
			$data['content'] = "user";
			$this->load->view('include/template',$data);
		}else{
			redirect('dashboard/eror'); 
			}
	}
	
	public function privileges()
	{
		$chk = $this->def_model->chk_permission(array('ad.user_id'=>$this->session->userdata('userid')));
		if(in_array('privileges',$chk))
		{
			if($this->uri->segment(3) == "show"){
				$data['info'] = $this->def_model->single_rec('kp_privileges',array('privilege_id'=>$this->uri->segment(4)));
			}
			
			if($this->uri->segment(3) == "delete"){
				$del = $this->def_model->del_rec('kp_privileges',array('privilege_id'=>$this->uri->segment(4)));
			}
			
			if($this->input->post('save') && !$this->input->post('privilege_id'))
			{
				$this->form_validation->set_rules('name','Privilege Name','required');
				$this->form_validation->set_rules('fname','File Name','required');
				$this->form_validation->set_rules('desc','Description','required');
				
				if($this->form_validation->run() == true)
				{
					
					$insert = array(
								'privilege_name' => $this->input->post('name'),
								'file_name' => $this->input->post('fname'),
								'description' => $this->input->post('desc')
							);
									
					$query = $this->def_model->insert_data('kp_privileges',$insert);
					
					if($query){
						$data['sucess'] = "Privilege has been Created";
					}
				}
				
			}
			// update prevelegies
			if($this->input->post('save') && $this->input->post('privilege_id'))
			{
				$this->form_validation->set_rules('name','Privilege Name','required');
				$this->form_validation->set_rules('fname','File Name','required');
				$this->form_validation->set_rules('desc','Description','required');
				
				if($this->form_validation->run() == true)
				{
					
					$insert = array(
								'privilege_name' => $this->input->post('name'),
								'file_name' => $this->input->post('fname'),
								'description' => $this->input->post('desc')
							);
									
					$query = $this->def_model->update_data('kp_privileges',$insert,array('privilege_id'=>$this->input->post('privilege_id')));
					
					if($query){
						$data['sucess'] = "privilege has been Updated";
					}
				}
			}
			$data['query'] = $this->def_model->get_where('kp_privileges');
			
			$data['content'] = "privileges";
			$this->load->view('include/template',$data);
		}else{
			redirect('dashboard/eror'); 
			}
	}
	
	public function assgine_privileges($gr_id)
	{
		$chk = $this->def_model->chk_permission(array('ad.user_id'=>$this->session->userdata('userid')));
		if(in_array('assgine_privileges',$chk))
		{
			if($this->input->post('save'))
			{
				$this->def_model->del_rec('kp_assigne_privilege_group',array('group_id'=>$this->input->post('group_id')));
				
				foreach($this->input->post('chk') as $pre)
				{
					$this->def_model->insert_data('kp_assigne_privilege_group',array('privilege_id'=>$pre,'group_id'=>$this->input->post('group_id')));
				}
				
				redirect('dashboard/group');
			}
			$data['gr_id'] = $gr_id;
			$data['all'] = $this->def_model->get_where('kp_privileges');
			$chk = $this->def_model->join_two_result('kp_group gr','kp_assigne_privilege_group apg','gr.group_id=apg.group_id','',array('gr.group_id'=>$gr_id));
					
			$pre_aray = array();
			foreach($chk as $row)
			{
				$pre_aray[$row->privilege_id] = $row->privilege_id;
			}
			
			//print_r($pre_aray);
			
			$data['query'] = $pre_aray;
			$data['content'] = "assgine_privileges";
			$this->load->view('include/template',$data);
		}else{
			redirect('dashboard/eror'); 
			}
	}
}
?>