<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->library('Loginfunctions');
		//if already logged redirect to home page
		if($this->loginfunctions->checkloggedin())
		{
			redirect('Admin_home/');
		}
		else
		{	$this->load->helper('security');
			$this->load->library('form_validation');
		}
	}
	public function index()
	{
		$this->login();
	}
	// login page
	public function login()
	{
		//check if submit button clicked
		if($this->input->post('login'))
		{	
		
	
		
	
		
		
			$this->form_validation->set_rules('username', 'Username', 'required|trim');
			$this->form_validation->set_rules('password', 'Password', 'trim|required');
			//if there are form validation errors
			if ($this->form_validation->run() === FALSE)
			{
				$this->_login_form();
			}
			else
			{
				//if user exist
				if( $this->loginfunctions->confirmuser( $this->input->post('username'),$this->input->post('password') ) )
				{
					$this->loginfunctions->createsessions( $this->input->post('username'),$this->input->post('password') );
					// redirect to home page
					redirect('Admin_home/');
				}
				else
				{
					$this->loginfunctions->clearsessionscookies();
					$this->_login_form('Incorrect Username or Password.');
				}
			}
		//if($this->input->post('login'))
		
		
		}
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			$this->_login_form();
		}
	}
	private function _login_form($error ='')
	{
		$data = array(
				'page_name' => 'Login Page' ,
				'error' => $error 
				);
		$this->load->view('admin/index_header',$data);
		$this->load->view('admin/login_inc',$data);
		$this->load->view('admin/footer_indx');	
	}	
}//end class Admin