<?php

include_once('secure_area.php');

class Pd_admin extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('Admin_m');
		$this->load->model('Content_m');
	}

	public function index()
	{
		if($this->session->userdata('logged_in'))
		{
			redirect('admin/admin_controller/main');
		}
		else
		{
			$this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
			$this->form_validation->set_rules('password','Password','required|trim|max_length[50]|xss_clean');

			if($this->form_validation->run() == false)
			{
 				$this->load->view('admin/login');
 				//redirect('pd_admin');
			}
			else
			{
				//$this->session->sess_destroy();

				extract($_POST);
				
				$model = $this->Admin_m->cek_login($username,md5($password));


				if(!$model)
				{
					$this->session->set_flashdata('login_error',TRUE);
					$this->load->view('admin/login');
					//redirect('pd_admin');
					//echo $this->db->last_query();
				}
				else
				{
					/*
					$get= $this->Content_m->get_row_custom('global_user','username', $username);
					$this->session->set_userdata(array(
						'logged_in' => TRUE , 
						'userid' => $get->id_user ,
						'nama'=>$get->nama ,
						'id_global_hak_akses'=> array('1' ,'2', '2' ,'2' ,'2' ,'2' ,'2' ,'2' ),

					));
					*/
					
					$get= $this->Content_m->get_row_custom('global_user','username', $username);
					//GET ALL ID_GLOBAL_HAK_AKSES 
					$arr_id_global_hakakses = array();
					$arr_id_sp = array();
					$arr_id_sms = array();
					foreach ($this->Content_m->select_db('global_user_hakakses','id_global_user',$get->id_global_user)->result() as $key) {
						//if($key->id_global_hakakses != 3){
							array_push($arr_id_global_hakakses, $key->id_global_hakakses);
							array_push($arr_id_sp, $key->id_sp);
							array_push($arr_id_sms, $key->id_sms);
						//}
						
					}
					
					
					if (in_array(1, $arr_id_global_hakakses) || in_array(2, $arr_id_global_hakakses) ||  in_array(3, $arr_id_global_hakakses)) {
					   	$main_session =  array(
						"login_guest"=>FALSE,
						'logged_in' => TRUE , 
						'userid' => $get->id_user ,
						'id_global_hakakses' => $arr_id_global_hakakses,
						'id_sp' => $arr_id_sp,
						'id_sms' => $arr_id_sms,
						'filter_program'=>''
						);

						//echo "<pre>";
						//echo print_r($main_session);
						
						$this->session->set_userdata($main_session);
						redirect('admin/admin_controller/main');
					}
					else
					{
						$this->session->set_flashdata('prefillage_error',TRUE);
						redirect("daftar");	
					}

				}
			}	
		}	
	}

	public function logout()
	{
		$this->session->sess_destroy();
		redirect('');
	}

	
	public function check_room()
	{
		if($this->session->userdata('logged_in'))
		{
			$actual_link = "$_SERVER[REQUEST_URI]";
			$link = explode("/",$actual_link);
			$asd =  count($link);
		
			$data['actual_link'] = $link[$asd-1];
			
			$data['get_event'] = $this->M_home->get_event();
			$data['mainView'] = 'edit/v_edit_check_room';
			
			$this->load->view('admin/template',$data);
		}
		else
		{
			redirect('');
		}
	}

	


}