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

class Main extends MY_Controller {

	public $data = array();

	public function __construct() {
		parent::__construct();

		if($_SERVER['HTTP_HOST'] !== $this->globalHttpHost) {
			redirect('http://'.$_SERVER['HTTP_HOST'].'/error');	
		}

		if($this->session->userdata('IsLogin')) {
			redirect($this->globalUrl.'dashboard');	
		}
		$this->load->library('form_validation');
		$this->load->model('login_model');
	}

	public function index()
	{
		if($this->input->post('login') == 'login') {

			// Do validation on client side 
			$this->form_validation->set_error_delimiters('<font color="red">', '</font>');
			$validation = $this->form_validation->run('login');

			
			if($validation) {

				$userInfo = $this->login_model->login_user();
				
				if($userInfo) {

					$userSession = array(
						'ID' => $userInfo->ID,
						'Level' => $userInfo->Level,
						'IsLogin' => true,
						'Username' => $userInfo->Username
						);

					$this->session->set_userdata($userSession);
					redirect($this->globalUrl.'dashboard');
					
				} else {
					$this->data['error'] = "Invalid account!";
				}
			}
		}

		$this->load->view('login',$this->data);	
		
	}
}
