<?php
if(!defined('BASEPATH')) exit('Hacking Attempt : Keluar dari sistem..!!');

class Login extends MY_Controller
{
	var $model ='Login_model';
	var $data = array();
	
public function __construct()
{
	parent::__construct();
	$this->load->model('admin/'.$this->model);
	$this->load->library(array('form_validation','session'));
	$this->load->database();
	$this->load->helper('url');
	
	$this->data['msg']='';
}

public function index()
{
	$session = $this->session->userdata('isLogin');
if($session == FALSE)
{
	redirect(base_url('admin/login/login_form'));
}else
{
	redirect(base_url('admin/home/'));
}

}

public function login_form()

{
	
	$this->form_validation->set_rules('username', 'Username', 'required|trim');
	$this->form_validation->set_rules('password', 'Password', 'required|md5');
	$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
	if($this->form_validation->run()==FALSE)
	{
		$this->adminloginview('login',$this->data);
	}else
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		
		$cek = $this->Login_model->takeUser($username, $password, 1);
		
		if($cek <> 0)
		{
			$response = $this->Login_model->userData($username);
			$this->session->set_userdata('isLogin', TRUE);
			$this->session->set_userdata('username',$response->name);
			$this->session->set_userdata('email',$response->email);
			$this->session->set_userdata('level',$response->role);
			$this->session->set_userdata('avtar',$response->avtar);
			$this->session->set_userdata('systype',$response->role);			
			redirect(base_url('admin/dashboard'));
		}else
		{
			$this->data['msg']='<div class="alert alert-danger">Failed Login: Check your username and password!</div>';
			$this->adminloginview('login',$this->data);
		}
	}
}

public function logout()

{

$this->session->sess_destroy();

redirect(base_url('admin/login/login_form'));

}

}

?>
