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

class Login extends CI_Controller {

	public function index()
	{
		$this->load->library('form_validation');
		$this->load->view('master/login');
	}
	// Login Authentication
	public function authenticate() {
		$this->load->library('form_validation');
		$this->load->model('Master_model');

// 		$this->form_validation->set_error_delimiters('<p class="invalid-feedback">','</p>');
		$this->form_validation->set_rules('username', 'Username', 'trim|required');
		$this->form_validation->set_rules('password', 'Password', 'trim|required');

		if ($this->form_validation->run() == TRUE) {
			// Success
			$username = $this->input->post('username');
			$admin = $this->Master_model->getByUsername($username);
			if (!empty($admin)) {
				$password = $this->input->post('password');
				$password = md5($password);
			
				
				if ($password == $admin['password']) {
				   
    					$adminArray = $admin;
    					$adminArray['username'] = $admin['email'];
    					$this->session->set_userdata('admin',$adminArray);
    					redirect(base_url().'master/dashboard');
				   
				} else {
					$this->session->set_flashdata('msg','Either Username Or Password is Incorrect');
					redirect(base_url().'master/login/index');
				}
			} else {
				$this->session->set_flashdata('msg','Either Username Or Password is Incorrect');
				redirect(base_url().'master/login/index');
			}
		} else {
			// Form Error
			$this->load->view('master/login');
		}
	}
	// Logout Session Unset Process..
	function logout() {
		$this->session->unset_userdata('admin');
					$this->session->set_flashdata('success','You have logged out.');
		
		redirect(base_url().'master/login/index');
	}
}

/* End of file Login.php */
/* Location: ./application/controllers/master/Login.php */