<?php

class Login_model extends CI_Model {

	public function cek_login()
	{
		if($this->router->class != null){
			if ($this->session->userdata('level_'.$this->session->userdata('nama_apps')) != null){		
				if ($this->session->userdata('class_'.$this->session->userdata('nama_apps')) != $this->router->class) {								
					redirect(base_url($this->session->userdata('class_'.$this->session->userdata('nama_apps'))));
				}
			} else redirect(base_url());
		} else if ($this->session->userdata('class_'.$this->session->userdata('nama_apps')) != null){	
			redirect(base_url($this->session->userdata('class_'.$this->session->userdata('nama_apps'))));
		}
	}

	public function cek_user($username, $password) 
	{	
		$this->db->select('du.*, 
			a_lu.value AS level_value, 
			a_lu.class, 
			a_lu.nav'
		);
		$this->db->from('data_user du');
		$this->db->join('asset_level_user a_lu', 'a_lu.id=du.level', 'left');	
		$this->db->where('username', $username);
		$this->db->where('password', $password);

		$query = $this->db->get()->row();

		if (!empty($query)) {

			$apps =  $this->db->get('pengaturan')->row();

			$prefix = base64_encode($_SERVER['HTTP_HOST'].''.$_SERVER['SCRIPT_NAME']);

			$data = [
				'prefix' => $prefix,
				'username_'.$prefix => $query->username,
				'nama_'.$prefix => $query->nama,
				'user_id_'.$prefix  => $query->id,
				'level_id_'.$prefix => $query->level,
				'level_value_'.$prefix => $query->level_value,
				'class_'.$prefix => $query->class,				
				'nav_'.$prefix => $query->nav,
				'level_id_utama_'.$prefix  => $query->level,
				'level_value_utama_'.$prefix => $query->level_value,
				'class_utama_'.$prefix  => $query->class,
				'nama_apps_'.$prefix => $apps->nama,
				'singkatan_'.$prefix => $apps->singkatan,
				'perusahaan_'.$prefix => $apps->perusahaan,
				'alamat_'.$prefix => $apps->alamat,
				'no_telp_'.$prefix => $apps->no_telp,
				'footer_'.$prefix => $apps->footer,
				'email_'.$prefix => $apps->email,
				'url_logo_kecil_'.$prefix => $apps->url_logo_kecil,
				'url_logo_besar_'.$prefix => $apps->url_logo_besar,
				'url_logo_header_'.$prefix => $apps->url_logo_header
			];
			$this->session->set_userdata($data); 
			return 'success';
		} else {
			return 'failed';
		} 
	}

	public function ganti_user() 
	{	
		$this->db->select('*');
		$this->db->from('asset_level_user');
		$this->db->where('id', $this->input->post('level_id'));

		$query = $this->db->get()->row();

		if($query) 
		{
			$apps =  $this->db->get('pengaturan')->row();

			$prefix = base64_encode($_SERVER['HTTP_HOST'].''.$_SERVER['SCRIPT_NAME']);

			$data = [
				'level_id_'.$prefix => $query->id,
				'level_value_'.$prefix => $query->value,
				'class_'.$prefix => $query->class,				
				'nav_'.$prefix => $query->nav
			];

			$this->session->set_userdata($data);
		} 

		return $this->get_notif('Ganti User');
	}

	public function logout()
	{
		$apps =  $this->db->get('pengaturan')->row();
		
		$prefix = base64_encode($_SERVER['HTTP_HOST'].''.$_SERVER['SCRIPT_NAME']);
		
		$data = [
			'prefix' => null,
			'username_'.$prefix => null,
			'nama_'.$prefix => null,
			'user_id_'.$prefix  => null,
			'level_id_'.$prefix => null,
			'level_value_'.$prefix => null,
			'class_'.$prefix => null,				
			'nav_'.$prefix => null,
			'level_id_utama_'.$prefix  => null,
			'level_value_utama_'.$prefix => null,
			'class_utama_'.$prefix  => null,
			'nama_apps_'.$prefix => null,
			'singkatan_'.$prefix => null,
			'perusahaan_'.$prefix => null,
			'alamat_'.$prefix => null,
			'no_telp_'.$prefix => null,
			'footer_'.$prefix => null,
			'email_'.$prefix => null,
			'url_logo_kecil_'.$prefix => null,
			'url_logo_besar_'.$prefix => null,
			'url_logo_header_'.$prefix => null
		];

		$this->session->unset_userdata($data);
		$this->session->sess_destroy();

		redirect(base_url());
	}

	//--------------------------------------------------------------------------------------------

	public function get_notif($name_function)
	{
		$eror = $this->db->error();

		if($eror['code'] == 0){
			$notif = array('eror'=>'success', 'pesan'=>$name_function.' berhasil');
		} else $notif = array('eror'=>'warning', 'pesan'=>"Terjadi kesalahan. Mohon hubungi (septiaputra@gmail.com)<br>$eror[code]: $eror[message]");	

		return $notif;		
	}

}