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

class Login extends CI_Controller 
{
	public function __construct() 
	{
        parent::__construct();
		$this->load->model('M_admin');
    }
	
	public function index()
	{
		if($this->session->userdata('admin') == NULL )
		{
			$this->load->view('adminLogin');
		}
		else
		{
			redirect('Admin');
		}
	}
	
	public function login()
	{
		$email			= $this->input->post('email');
		$password		= md5($this->input->post('password'));
		$check			= $this->M_admin->login($email,$password);
		// print_r($check);die();
		if($check != NULL)
		{
			$sess_array = array(
								'id_admin'		=> $check->id_admin,
								'nama_admin'	=> $check->nama_admin,
								'role'			=> $check->role
								);		
			$this->session->set_userdata('admin', $sess_array);
			// echo "berhasil login";
			redirect('admin/Admin','refresh');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Email atau password yang Anda masukkan salah. Silakan Coba lagi!</p>
						</center>
					</div>');	
			redirect('admin/Login');		
		}
	}
	
	public function logout()
	{
		$this->session->unset_userdata('admin');
		$this->session->sess_destroy();
		redirect('admin/Login', 'refresh');
	}
}
